use of org.apache.druid.math.expr.ExprEval in project druid by druid-io.
the class HyperUniqueExpressionsTest method testEstimateRound.
@Test
public void testEstimateRound() {
Expr expr = Parser.parse("hyper_unique_round_estimate(hyper_unique_add(1.234, hyper_unique()))", MACRO_TABLE);
ExprEval eval = expr.eval(inputBindings);
Assert.assertEquals(ExpressionType.LONG, eval.type());
Assert.assertEquals(1L, eval.asLong(), 0.01);
}
use of org.apache.druid.math.expr.ExprEval in project druid by druid-io.
the class HyperUniqueExpressionsTest method testLong.
@Test
public void testLong() {
Expr expr = Parser.parse("hyper_unique_add(1234, hyper_unique())", MACRO_TABLE);
ExprEval eval = expr.eval(inputBindings);
Assert.assertEquals(HyperUniqueExpressions.TYPE, eval.type());
Assert.assertTrue(eval.value() instanceof HyperLogLogCollector);
Assert.assertEquals(1.0, ((HyperLogLogCollector) eval.value()).estimateCardinality(), 0.01);
expr = Parser.parse("hyper_unique_add(1234, hyper_unique_add(5678, hyper_unique()))", MACRO_TABLE);
eval = expr.eval(inputBindings);
Assert.assertEquals(HyperUniqueExpressions.TYPE, eval.type());
Assert.assertTrue(eval.value() instanceof HyperLogLogCollector);
Assert.assertEquals(2.0, ((HyperLogLogCollector) eval.value()).estimateCardinality(), 0.01);
expr = Parser.parse("hyper_unique_add(long, hyper_unique())", MACRO_TABLE);
eval = expr.eval(inputBindings);
Assert.assertEquals(HyperUniqueExpressions.TYPE, eval.type());
Assert.assertTrue(eval.value() instanceof HyperLogLogCollector);
Assert.assertEquals(1.0, ((HyperLogLogCollector) eval.value()).estimateCardinality(), 0.01);
expr = Parser.parse("hyper_unique_add(nullLong, hyper_unique())", MACRO_TABLE);
eval = expr.eval(inputBindings);
Assert.assertEquals(HyperUniqueExpressions.TYPE, eval.type());
Assert.assertTrue(eval.value() instanceof HyperLogLogCollector);
Assert.assertEquals(NullHandling.replaceWithDefault() ? 1.0 : 0.0, ((HyperLogLogCollector) eval.value()).estimateCardinality(), 0.01);
}
use of org.apache.druid.math.expr.ExprEval in project druid by druid-io.
the class IPv4AddressMatchExprMacroTest method eval.
private boolean eval(Expr... args) {
Expr expr = apply(Arrays.asList(args));
ExprEval eval = expr.eval(InputBindings.nilBindings());
return eval.asBoolean();
}
use of org.apache.druid.math.expr.ExprEval in project druid by druid-io.
the class TimestampFormatExprMacro method apply.
@Override
public Expr apply(final List<Expr> args) {
if (args.size() < 1 || args.size() > 3) {
throw new IAE("Function[%s] must have 1 to 3 arguments", name());
}
final Expr arg = args.get(0);
final String formatString;
final DateTimeZone timeZone;
if (args.size() > 1) {
Preconditions.checkArgument(args.get(1).isLiteral(), "Function[%s] format arg must be a literal", name());
formatString = (String) args.get(1).getLiteralValue();
} else {
formatString = null;
}
if (args.size() > 2) {
timeZone = ExprUtils.toTimeZone(args.get(2));
} else {
timeZone = DateTimeZone.UTC;
}
final DateTimeFormatter formatter = formatString == null ? ISODateTimeFormat.dateTime().withZone(timeZone) : DateTimeFormat.forPattern(formatString).withZone(timeZone);
class TimestampFormatExpr extends ExprMacroTable.BaseScalarUnivariateMacroFunctionExpr {
private TimestampFormatExpr(Expr arg) {
super(FN_NAME, arg);
}
@Nonnull
@Override
public ExprEval eval(final ObjectBinding bindings) {
ExprEval eval = arg.eval(bindings);
if (eval.isNumericNull()) {
// Return null if the argument if null.
return ExprEval.of(null);
}
return ExprEval.of(formatter.print(arg.eval(bindings).asLong()));
}
@Override
public Expr visit(Shuttle shuttle) {
return shuttle.visit(apply(shuttle.visitAll(args)));
}
@Nullable
@Override
public ExpressionType getOutputType(InputBindingInspector inspector) {
return ExpressionType.STRING;
}
@Override
public String stringify() {
if (args.size() > 2) {
return StringUtils.format("%s(%s, %s, %s)", FN_NAME, arg.stringify(), args.get(1).stringify(), args.get(2).stringify());
}
if (args.size() > 1) {
return StringUtils.format("%s(%s, %s)", FN_NAME, arg.stringify(), args.get(1).stringify());
}
return super.stringify();
}
}
return new TimestampFormatExpr(arg);
}
Aggregations