Search in sources :

Example 21 with ExpressionType

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

the class VectorComparisonProcessors method makeBooleanProcessor.

public static <T> ExprVectorProcessor<T> makeBooleanProcessor(Expr.VectorInputBindingInspector inspector, Expr left, Expr right, Supplier<LongOutStringsInFunctionVectorProcessor> longOutStringsInFunctionVectorProcessor, Supplier<LongOutLongsInFunctionVectorValueProcessor> longOutLongsInProcessor, Supplier<LongOutLongDoubleInFunctionVectorValueProcessor> longOutLongDoubleInProcessor, Supplier<LongOutDoubleLongInFunctionVectorValueProcessor> longOutDoubleLongInProcessor, Supplier<LongOutDoublesInFunctionVectorValueProcessor> longOutDoublesInProcessor) {
    final ExpressionType leftType = left.getOutputType(inspector);
    final ExpressionType rightType = right.getOutputType(inspector);
    ExprVectorProcessor<?> processor = null;
    if (Types.is(leftType, ExprType.STRING)) {
        if (Types.isNullOr(rightType, ExprType.STRING)) {
            processor = longOutStringsInFunctionVectorProcessor.get();
        } else {
            processor = longOutDoublesInProcessor.get();
        }
    } else if (Types.is(rightType, ExprType.STRING)) {
        if (leftType == null) {
            processor = longOutStringsInFunctionVectorProcessor.get();
        } else {
            processor = longOutDoublesInProcessor.get();
        }
    } else if (leftType == null) {
        if (Types.isNullOr(rightType, ExprType.STRING)) {
            processor = longOutStringsInFunctionVectorProcessor.get();
        }
    } else if (leftType.is(ExprType.DOUBLE) || Types.is(rightType, ExprType.DOUBLE)) {
        processor = longOutDoublesInProcessor.get();
    }
    if (processor != null) {
        return (ExprVectorProcessor<T>) processor;
    }
    // fall through to normal math processor logic
    return VectorMathProcessors.makeLongMathProcessor(inspector, left, right, longOutLongsInProcessor, longOutLongDoubleInProcessor, longOutDoubleLongInProcessor, longOutDoublesInProcessor);
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 22 with ExpressionType

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

the class VectorProcessors method makeSymmetricalProcessor.

/**
 * Make a 2 argument, symmetrical processor where both argments must be the same input type and produce the same
 * output type
 *    long, long      -> long
 *    double, double  -> double
 *    string, string  -> string
 */
public static <T> ExprVectorProcessor<T> makeSymmetricalProcessor(Expr.VectorInputBindingInspector inspector, Expr left, Expr right, Supplier<ExprVectorProcessor<?>> longProcessor, Supplier<ExprVectorProcessor<?>> doubleProcessor, Supplier<ExprVectorProcessor<?>> stringProcessor) {
    final ExpressionType leftType = left.getOutputType(inspector);
    if (leftType == null) {
        return right.buildVectorized(inspector);
    }
    Preconditions.checkArgument(inspector.areSameTypes(left, right));
    ExprVectorProcessor<?> processor = null;
    if (Types.is(leftType, ExprType.STRING)) {
        processor = stringProcessor.get();
    } else if (Types.is(leftType, ExprType.LONG)) {
        processor = longProcessor.get();
    } else if (Types.is(leftType, ExprType.DOUBLE)) {
        processor = doubleProcessor.get();
    }
    if (processor == null) {
        throw Exprs.cannotVectorize();
    }
    return (ExprVectorProcessor<T>) processor;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 23 with ExpressionType

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

the class VectorProcessors method isNull.

public static <T> ExprVectorProcessor<T> isNull(Expr.VectorInputBindingInspector inspector, Expr expr) {
    final ExpressionType type = expr.getOutputType(inspector);
    if (type == null) {
        return constant(1L, inspector.getMaxVectorSize());
    }
    final long[] outputValues = new long[inspector.getMaxVectorSize()];
    ExprVectorProcessor<?> processor = null;
    if (Types.is(type, ExprType.STRING)) {
        final ExprVectorProcessor<String[]> input = expr.buildVectorized(inspector);
        processor = new ExprVectorProcessor<long[]>() {

            @Override
            public ExprEvalVector<long[]> evalVector(Expr.VectorInputBinding bindings) {
                final ExprEvalVector<String[]> inputEval = input.evalVector(bindings);
                final int currentSize = bindings.getCurrentVectorSize();
                final String[] values = inputEval.values();
                for (int i = 0; i < currentSize; i++) {
                    if (values[i] == null) {
                        outputValues[i] = 1L;
                    } else {
                        outputValues[i] = 0L;
                    }
                }
                return new ExprEvalLongVector(outputValues, null);
            }

            @Override
            public ExpressionType getOutputType() {
                return ExpressionType.LONG;
            }
        };
    } else if (Types.is(type, ExprType.LONG)) {
        final ExprVectorProcessor<long[]> input = expr.buildVectorized(inspector);
        processor = new ExprVectorProcessor<long[]>() {

            @Override
            public ExprEvalVector<long[]> evalVector(Expr.VectorInputBinding bindings) {
                final ExprEvalVector<long[]> inputEval = input.evalVector(bindings);
                final int currentSize = bindings.getCurrentVectorSize();
                final boolean[] nulls = inputEval.getNullVector();
                if (nulls == null) {
                    Arrays.fill(outputValues, 0L);
                } else {
                    for (int i = 0; i < currentSize; i++) {
                        if (nulls[i]) {
                            outputValues[i] = 1L;
                        } else {
                            outputValues[i] = 0L;
                        }
                    }
                }
                return new ExprEvalLongVector(outputValues, null);
            }

            @Override
            public ExpressionType getOutputType() {
                return ExpressionType.LONG;
            }
        };
    } else if (Types.is(type, ExprType.DOUBLE)) {
        final ExprVectorProcessor<double[]> input = expr.buildVectorized(inspector);
        processor = new ExprVectorProcessor<long[]>() {

            @Override
            public ExprEvalVector<long[]> evalVector(Expr.VectorInputBinding bindings) {
                final ExprEvalVector<double[]> inputEval = input.evalVector(bindings);
                final int currentSize = bindings.getCurrentVectorSize();
                final boolean[] nulls = inputEval.getNullVector();
                if (nulls == null) {
                    Arrays.fill(outputValues, 0L);
                } else {
                    for (int i = 0; i < currentSize; i++) {
                        if (nulls[i]) {
                            outputValues[i] = 1L;
                        } else {
                            outputValues[i] = 0L;
                        }
                    }
                }
                return new ExprEvalLongVector(outputValues, null);
            }

            @Override
            public ExpressionType getOutputType() {
                return ExpressionType.LONG;
            }
        };
    }
    if (processor == null) {
        throw Exprs.cannotVectorize();
    }
    return (ExprVectorProcessor<T>) processor;
}
Also used : Expr(org.apache.druid.math.expr.Expr) ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 24 with ExpressionType

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

the class VectorMathProcessors method makeMathProcessor.

/**
 * Make a 2 argument, math processor with the following type rules
 *    long, long      -> long
 *    long, double    -> double
 *    double, long    -> double
 *    double, double  -> double
 */
public static <T> ExprVectorProcessor<T> makeMathProcessor(Expr.VectorInputBindingInspector inspector, Expr left, Expr right, Supplier<LongOutLongsInFunctionVectorValueProcessor> longOutLongsInProcessor, Supplier<DoubleOutLongDoubleInFunctionVectorValueProcessor> doubleOutLongDoubleInProcessor, Supplier<DoubleOutDoubleLongInFunctionVectorValueProcessor> doubleOutDoubleLongInProcessor, Supplier<DoubleOutDoublesInFunctionVectorValueProcessor> doubleOutDoublesInProcessor) {
    final ExpressionType leftType = left.getOutputType(inspector);
    final ExpressionType rightType = right.getOutputType(inspector);
    ExprVectorProcessor<?> processor = null;
    if (Types.is(leftType, ExprType.LONG)) {
        if (Types.isNullOr(rightType, ExprType.LONG)) {
            processor = longOutLongsInProcessor.get();
        } else if (rightType.anyOf(ExprType.STRING, ExprType.DOUBLE)) {
            processor = doubleOutLongDoubleInProcessor.get();
        }
    } else if (Types.is(leftType, ExprType.DOUBLE)) {
        if (Types.is(rightType, ExprType.LONG)) {
            processor = doubleOutDoubleLongInProcessor.get();
        } else if (Types.isNullOrAnyOf(rightType, ExprType.STRING, ExprType.DOUBLE)) {
            processor = doubleOutDoublesInProcessor.get();
        }
    } else if (leftType == null) {
        if (Types.is(rightType, ExprType.LONG)) {
            processor = longOutLongsInProcessor.get();
        } else if (Types.is(rightType, ExprType.DOUBLE)) {
            processor = doubleOutLongDoubleInProcessor.get();
        } else if (rightType == null) {
            processor = longOutLongsInProcessor.get();
        }
    } else if (leftType.is(ExprType.STRING)) {
        if (Types.is(rightType, ExprType.LONG)) {
            processor = longOutLongsInProcessor.get();
        } else if (Types.is(rightType, ExprType.DOUBLE)) {
            processor = doubleOutLongDoubleInProcessor.get();
        }
    }
    if (processor == null) {
        throw Exprs.cannotVectorize(StringUtils.format("%s %s", leftType, rightType));
    }
    return (ExprVectorProcessor<T>) processor;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 25 with ExpressionType

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

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