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