Search in sources :

Example 71 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class MacroTestBase method eval.

/**
 * Evalutes {@code expr} using our macro.
 *
 * @param expression expression to evalute
 * @param bindings   bindings for evaluation
 *
 * @throws AssertionError if {@link ExprMacroTable.ExprMacro#apply} is not called on our macro during parsing
 */
protected ExprEval<?> eval(final String expression, final Expr.ObjectBinding bindings) {
    // WrappedExprMacro allows us to confirm that our ExprMacro was actually called.
    class WrappedExprMacro implements ExprMacroTable.ExprMacro {

        private final AtomicLong calls = new AtomicLong();

        @Override
        public String name() {
            return macro.name();
        }

        @Override
        public Expr apply(List<Expr> args) {
            calls.incrementAndGet();
            return macro.apply(args);
        }
    }
    final WrappedExprMacro wrappedMacro = new WrappedExprMacro();
    final GuiceExprMacroTable macroTable = new GuiceExprMacroTable(ImmutableSet.of(wrappedMacro));
    final Expr expr = Parser.parse(expression, macroTable);
    Assert.assertTrue("Calls made to macro.apply", wrappedMacro.calls.get() > 0);
    return expr.eval(bindings);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Expr(org.apache.druid.math.expr.Expr) List(java.util.List)

Example 72 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class TimestampShiftMacroTest method testDynamicExpression.

@Test
public void testDynamicExpression() {
    // step parameter is not a literal expression
    Expr expr = apply(ImmutableList.of(ExprEval.of(timestamp.getMillis()).toExpr(), ExprEval.of("P1Y").toExpr(), new NotLiteralExpr("step"), ExprEval.of("America/Los_Angeles").toExpr()));
    final int step = 3;
    Assert.assertEquals(timestamp.toDateTime(DateTimes.inferTzFromString("America/Los_Angeles")).withPeriodAdded(Years.ONE, step).getMillis(), expr.eval(new Expr.ObjectBinding() {

        @Nullable
        @Override
        public ExpressionType getType(String name) {
            return null;
        }

        @Nullable
        @Override
        public Object get(String name) {
            if ("step".equals(name)) {
                return step;
            } else {
                throw new IAE("Invalid bindings");
            }
        }
    }).asLong());
}
Also used : Expr(org.apache.druid.math.expr.Expr) IAE(org.apache.druid.java.util.common.IAE) ExpressionType(org.apache.druid.math.expr.ExpressionType) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 73 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class TimestampShiftMacroTest method testPositiveStep.

@Test
public void testPositiveStep() {
    int step = 5;
    Expr expr = apply(ImmutableList.of(ExprEval.of(timestamp.getMillis()).toExpr(), ExprEval.of("P1M").toExpr(), ExprEval.of(step).toExpr()));
    Assert.assertEquals(timestamp.withPeriodAdded(Months.ONE, step).getMillis(), expr.eval(InputBindings.nilBindings()).asLong());
}
Also used : Expr(org.apache.druid.math.expr.Expr) Test(org.junit.Test)

Example 74 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class VectorExpressionsSanityTest method testTimeShiftFn.

@Test
public void testTimeShiftFn() {
    int step = 1;
    Expr parsed = TIMESTAMP_SHIFT_EXPR_MACRO.apply(ImmutableList.of(ExprEval.of(DATE_TIME.getMillis()).toExpr(), ExprEval.of("P1M").toExpr(), ExprEval.of(step).toExpr()));
    testExpression("time_shift(l1, 'P1M', 1)", parsed, types);
}
Also used : Expr(org.apache.druid.math.expr.Expr) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) VectorExprSanityTest(org.apache.druid.math.expr.VectorExprSanityTest)

Example 75 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class IPv4AddressStringifyExprMacroTest method testInvalidArgType.

@Test
public void testInvalidArgType() {
    Expr longArray = ExprEval.ofLongArray(new Long[] { 1L, 2L }).toExpr();
    Assert.assertNull(eval(longArray));
}
Also used : Expr(org.apache.druid.math.expr.Expr) Test(org.junit.Test)

Aggregations

Expr (org.apache.druid.math.expr.Expr)104 Test (org.junit.Test)58 ExprEval (org.apache.druid.math.expr.ExprEval)18 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)17 IAE (org.apache.druid.java.util.common.IAE)14 ExpressionType (org.apache.druid.math.expr.ExpressionType)8 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)7 ArrayList (java.util.ArrayList)6 Nullable (javax.annotation.Nullable)6 HashSet (java.util.HashSet)5 List (java.util.List)4 HyperLogLogCollector (org.apache.druid.hll.HyperLogLogCollector)4 BloomKFilter (org.apache.druid.query.filter.BloomKFilter)4 InDimFilter (org.apache.druid.query.filter.InDimFilter)4 RexNode (org.apache.calcite.rex.RexNode)3 Filter (org.apache.druid.query.filter.Filter)3 VirtualColumn (org.apache.druid.segment.VirtualColumn)3 FalseFilter (org.apache.druid.segment.filter.FalseFilter)3 OrFilter (org.apache.druid.segment.filter.OrFilter)3 SelectorFilter (org.apache.druid.segment.filter.SelectorFilter)3