use of org.apache.druid.math.expr.ExpressionType in project druid by apache.
the class ExpressionFilter method makeVectorMatcher.
@Override
public VectorValueMatcher makeVectorMatcher(VectorColumnSelectorFactory factory) {
final Expr theExpr = expr.get();
DruidPredicateFactory predicateFactory = new DruidPredicateFactory() {
@Override
public Predicate<String> makeStringPredicate() {
return Evals::asBoolean;
}
@Override
public DruidLongPredicate makeLongPredicate() {
return Evals::asBoolean;
}
@Override
public DruidFloatPredicate makeFloatPredicate() {
return Evals::asBoolean;
}
@Override
public DruidDoublePredicate makeDoublePredicate() {
return Evals::asBoolean;
}
// The hashcode and equals are to make SubclassesMustOverrideEqualsAndHashCodeTest stop complaining..
// DruidPredicateFactory currently doesn't really need equals or hashcode since 'toString' method that is actually
// called when testing equality of DimensionPredicateFilter, so it's the truly required method, but that seems
// a bit strange. DimensionPredicateFilter should probably be reworked to use equals from DruidPredicateFactory
// instead of using toString.
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
};
final ExpressionType outputType = theExpr.getOutputType(factory);
// effectively constant
if (outputType == null) {
// false matcher
if (NullHandling.sqlCompatible()) {
return BooleanVectorValueMatcher.of(factory.getReadableVectorInspector(), false);
}
// or not.
return BooleanVectorValueMatcher.of(factory.getReadableVectorInspector(), theExpr.eval(InputBindings.nilBindings()).asBoolean());
}
// if we got here, we really have to evaluate the expressions to match
switch(outputType.getType()) {
case LONG:
return VectorValueMatcherColumnProcessorFactory.instance().makeLongProcessor(ColumnCapabilitiesImpl.createSimpleNumericColumnCapabilities(ColumnType.LONG), ExpressionVectorSelectors.makeVectorValueSelector(factory, theExpr)).makeMatcher(predicateFactory);
case DOUBLE:
return VectorValueMatcherColumnProcessorFactory.instance().makeDoubleProcessor(ColumnCapabilitiesImpl.createSimpleNumericColumnCapabilities(ColumnType.DOUBLE), ExpressionVectorSelectors.makeVectorValueSelector(factory, theExpr)).makeMatcher(predicateFactory);
case STRING:
return VectorValueMatcherColumnProcessorFactory.instance().makeObjectProcessor(ColumnCapabilitiesImpl.createSimpleSingleValueStringColumnCapabilities(), ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr)).makeMatcher(predicateFactory);
default:
throw new UOE("Vectorized expression matchers not implemented for type: [%s]", outputType);
}
}
use of org.apache.druid.math.expr.ExpressionType in project druid by apache.
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.ExpressionType in project druid by apache.
the class VectorMathProcessors method doublePower.
public static <T> ExprVectorProcessor<T> doublePower(Expr.VectorInputBindingInspector inspector, Expr left, Expr right) {
final ExpressionType leftType = left.getOutputType(inspector);
final ExpressionType rightType = right.getOutputType(inspector);
BivariateFunctionVectorValueProcessor<?, ?, ?> processor = null;
if ((Types.is(leftType, ExprType.LONG) && Types.isNullOr(rightType, ExprType.LONG)) || (leftType == null && Types.is(rightType, ExprType.LONG))) {
processor = new DoubleOutLongsInFunctionVectorValueProcessor(left.buildVectorized(inspector), right.buildVectorized(inspector), inspector.getMaxVectorSize()) {
@Override
public double apply(long left, long right) {
return Math.pow(left, right);
}
};
}
if (processor != null) {
return (ExprVectorProcessor<T>) processor;
}
return power(inspector, left, right);
}
use of org.apache.druid.math.expr.ExpressionType in project druid by apache.
the class VectorMathProcessors method makeMathProcessor.
/**
* Make a 1 argument math processor with the following type rules
* long -> long
* double -> double
*/
public static <T> ExprVectorProcessor<T> makeMathProcessor(Expr.VectorInputBindingInspector inspector, Expr arg, Supplier<LongOutLongInFunctionVectorValueProcessor> longOutLongInSupplier, Supplier<DoubleOutDoubleInFunctionVectorValueProcessor> doubleOutDoubleInSupplier) {
final ExpressionType inputType = arg.getOutputType(inspector);
ExprVectorProcessor<?> processor = null;
if (inputType != null) {
if (inputType.is(ExprType.LONG)) {
processor = longOutLongInSupplier.get();
} else if (inputType.is(ExprType.DOUBLE)) {
processor = doubleOutDoubleInSupplier.get();
}
}
if (processor == null) {
throw Exprs.cannotVectorize();
}
return (ExprVectorProcessor<T>) processor;
}
use of org.apache.druid.math.expr.ExpressionType in project druid by apache.
the class VectorMathProcessors method makeLongMathProcessor.
/**
* Make a 1 argument math processor with the following type rules
* long -> long
* double -> long
*/
public static <T> ExprVectorProcessor<T> makeLongMathProcessor(Expr.VectorInputBindingInspector inspector, Expr arg, Supplier<LongOutLongInFunctionVectorValueProcessor> longOutLongInSupplier, Supplier<LongOutDoubleInFunctionVectorValueProcessor> longOutDoubleInSupplier) {
final ExpressionType inputType = arg.getOutputType(inspector);
ExprVectorProcessor<?> processor = null;
if (inputType != null) {
if (inputType.is(ExprType.LONG)) {
processor = longOutLongInSupplier.get();
} else if (inputType.is(ExprType.DOUBLE)) {
processor = longOutDoubleInSupplier.get();
}
}
if (processor == null) {
throw Exprs.cannotVectorize();
}
return (ExprVectorProcessor<T>) processor;
}
Aggregations