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);
}
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());
}
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());
}
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());
}
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);
}
}
Aggregations