Search in sources :

Example 16 with ExprEval

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);
}
Also used : ExprEval(org.apache.druid.math.expr.ExprEval) Expr(org.apache.druid.math.expr.Expr) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 17 with ExprEval

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);
}
Also used : ExprEval(org.apache.druid.math.expr.ExprEval) Expr(org.apache.druid.math.expr.Expr) HyperLogLogCollector(org.apache.druid.hll.HyperLogLogCollector) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 18 with ExprEval

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();
}
Also used : ExprEval(org.apache.druid.math.expr.ExprEval) Expr(org.apache.druid.math.expr.Expr)

Example 19 with ExprEval

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);
}
Also used : ExprEval(org.apache.druid.math.expr.ExprEval) Expr(org.apache.druid.math.expr.Expr) IAE(org.apache.druid.java.util.common.IAE) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTimeZone(org.joda.time.DateTimeZone)

Aggregations

ExprEval (org.apache.druid.math.expr.ExprEval)19 Expr (org.apache.druid.math.expr.Expr)17 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)11 Test (org.junit.Test)11 HyperLogLogCollector (org.apache.druid.hll.HyperLogLogCollector)4 BloomKFilter (org.apache.druid.query.filter.BloomKFilter)4 IAE (org.apache.druid.java.util.common.IAE)2 BigDecimal (java.math.BigDecimal)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexExecutor (org.apache.calcite.rex.RexExecutor)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1 SubnetUtils (org.apache.commons.net.util.SubnetUtils)1 DateTimes (org.apache.druid.java.util.common.DateTimes)1 ExprType (org.apache.druid.math.expr.ExprType)1 InputBindings (org.apache.druid.math.expr.InputBindings)1