Search in sources :

Example 1 with Expr

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

the class RowBasedExpressionColumnValueSelector method getObject.

@Override
public ExprEval getObject() {
    // check to find any arrays for this row
    List<String> arrayBindings = unknownColumns.stream().filter(this::isBindingArray).collect(Collectors.toList());
    // eliminate anything that will never be an array
    if (ignoredColumns.size() > 0) {
        unknownColumns.removeAll(ignoredColumns);
        ignoredColumns.clear();
    }
    // base expression, we keep a cache of transformed expressions to minimize extra work
    if (arrayBindings.size() > 0) {
        final int key = arrayBindings.hashCode();
        if (transformedCache.containsKey(key)) {
            return transformedCache.get(key).eval(bindings);
        }
        Expr transformed = Parser.applyUnappliedBindings(expression, baseBindingAnalysis, arrayBindings);
        transformedCache.put(key, transformed);
        return transformed.eval(bindings);
    }
    // no arrays for this row, evaluate base expression
    return expression.eval(bindings);
}
Also used : Expr(org.apache.druid.math.expr.Expr)

Example 2 with Expr

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

the class TimestampShiftMacroTest method testPeriodDay.

@Test
public void testPeriodDay() {
    Expr expr = apply(ImmutableList.of(ExprEval.of(timestamp.getMillis()).toExpr(), ExprEval.of("P1D").toExpr(), ExprEval.of(1).toExpr()));
    Assert.assertEquals(timestamp.withPeriodAdded(Days.ONE, 1).getMillis(), expr.eval(InputBindings.nilBindings()).asLong());
}
Also used : Expr(org.apache.druid.math.expr.Expr) Test(org.junit.Test)

Example 3 with Expr

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

the class TimestampShiftMacroTest method testNegativeStep.

@Test
public void testNegativeStep() {
    int step = -3;
    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 4 with Expr

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

the class LookupExprMacroTest method assertExpr.

private void assertExpr(final String expression, final Object expectedResult) {
    final Expr expr = Parser.parse(expression, LookupEnabledTestExprMacroTable.INSTANCE);
    Assert.assertEquals(expression, expectedResult, expr.eval(BINDINGS).value());
    final Expr exprNotFlattened = Parser.parse(expression, LookupEnabledTestExprMacroTable.INSTANCE, false);
    final Expr roundTripNotFlattened = Parser.parse(exprNotFlattened.stringify(), LookupEnabledTestExprMacroTable.INSTANCE);
    Assert.assertEquals(exprNotFlattened.stringify(), expectedResult, roundTripNotFlattened.eval(BINDINGS).value());
    final Expr roundTrip = Parser.parse(expr.stringify(), LookupEnabledTestExprMacroTable.INSTANCE);
    Assert.assertEquals(exprNotFlattened.stringify(), expectedResult, roundTrip.eval(BINDINGS).value());
}
Also used : Expr(org.apache.druid.math.expr.Expr)

Example 5 with Expr

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

the class LookupExprMacroTest method testCacheKeyChangesWhenLookupChanges.

@Test
public void testCacheKeyChangesWhenLookupChanges() {
    final String expression = "lookup(x, 'lookyloo')";
    final Expr expr = Parser.parse(expression, LookupEnabledTestExprMacroTable.INSTANCE);
    final Expr exprSameLookup = Parser.parse(expression, LookupEnabledTestExprMacroTable.INSTANCE);
    final Expr exprChangedLookup = Parser.parse(expression, new ExprMacroTable(LookupEnabledTestExprMacroTable.makeTestMacros(ImmutableMap.of("x", "y", "a", "b"))));
    // same should have same cache key
    Assert.assertArrayEquals(expr.getCacheKey(), exprSameLookup.getCacheKey());
    // different should not have same key
    final byte[] exprBytes = expr.getCacheKey();
    final byte[] expr2Bytes = exprChangedLookup.getCacheKey();
    if (exprBytes.length == expr2Bytes.length) {
        // only check for equality if lengths are equal
        boolean allEqual = true;
        for (int i = 0; i < exprBytes.length; i++) {
            allEqual = allEqual && (exprBytes[i] == expr2Bytes[i]);
        }
        Assert.assertFalse(allEqual);
    }
}
Also used : Expr(org.apache.druid.math.expr.Expr) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) 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