Search in sources :

Example 36 with ExpressionType

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);
    }
}
Also used : Expr(org.apache.druid.math.expr.Expr) UOE(org.apache.druid.java.util.common.UOE) ExpressionType(org.apache.druid.math.expr.ExpressionType) DruidPredicateFactory(org.apache.druid.query.filter.DruidPredicateFactory)

Example 37 with ExpressionType

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());
}
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 38 with ExpressionType

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);
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 39 with ExpressionType

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;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 40 with ExpressionType

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;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Aggregations

ExpressionType (org.apache.druid.math.expr.ExpressionType)46 Expr (org.apache.druid.math.expr.Expr)18 Nullable (javax.annotation.Nullable)6 ColumnCapabilities (org.apache.druid.segment.column.ColumnCapabilities)6 ArrayList (java.util.ArrayList)4 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)4 ColumnValueSelector (org.apache.druid.segment.ColumnValueSelector)4 ColumnType (org.apache.druid.segment.column.ColumnType)4 VectorCursor (org.apache.druid.segment.vector.VectorCursor)4 VectorObjectSelector (org.apache.druid.segment.vector.VectorObjectSelector)4 VectorValueSelector (org.apache.druid.segment.vector.VectorValueSelector)4 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 IAE (org.apache.druid.java.util.common.IAE)2 ISE (org.apache.druid.java.util.common.ISE)2