Search in sources :

Example 41 with ExpressionType

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

the class VectorMathProcessors method makeDoubleMathProcessor.

/**
 * Make a 1 argument math processor with the following type rules
 *    long    -> double
 *    double  -> double
 */
public static <T> ExprVectorProcessor<T> makeDoubleMathProcessor(Expr.VectorInputBindingInspector inspector, Expr arg, Supplier<DoubleOutLongInFunctionVectorValueProcessor> doubleOutLongInSupplier, Supplier<DoubleOutDoubleInFunctionVectorValueProcessor> doubleOutDoubleInSupplier) {
    final ExpressionType inputType = arg.getOutputType(inspector);
    ExprVectorProcessor<?> processor = null;
    if (inputType != null) {
        if (inputType.is(ExprType.LONG)) {
            processor = doubleOutLongInSupplier.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 42 with ExpressionType

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

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 43 with ExpressionType

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

the class VectorMathProcessors method makeDoubleMathProcessor.

/**
 * Make a 2 argument, math processor with the following type rules
 *    long, long      -> double
 *    long, double    -> double
 *    double, long    -> double
 *    double, double  -> double
 */
public static <T> ExprVectorProcessor<T> makeDoubleMathProcessor(Expr.VectorInputBindingInspector inspector, Expr left, Expr right, Supplier<DoubleOutLongsInFunctionVectorValueProcessor> doubleOutLongsInProcessor, 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.is(rightType, ExprType.LONG)) {
            processor = doubleOutLongsInProcessor.get();
        } else if (Types.isNullOr(rightType, ExprType.DOUBLE)) {
            processor = doubleOutLongDoubleInProcessor.get();
        }
    } else if (Types.is(leftType, ExprType.DOUBLE)) {
        if (Types.is(rightType, ExprType.LONG)) {
            processor = doubleOutDoubleLongInProcessor.get();
        } else if (Types.isNullOr(rightType, ExprType.DOUBLE)) {
            processor = doubleOutDoublesInProcessor.get();
        }
    } else if (leftType == null) {
        if (Types.is(rightType, ExprType.LONG)) {
            processor = doubleOutDoubleLongInProcessor.get();
        } else if (Types.is(rightType, ExprType.DOUBLE)) {
            processor = doubleOutDoublesInProcessor.get();
        }
    }
    if (processor == null) {
        throw Exprs.cannotVectorize();
    }
    return (ExprVectorProcessor<T>) processor;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 44 with ExpressionType

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

the class VectorMathProcessors method makeLongMathProcessor.

/**
 * Make a 2 argument, math processor with the following type rules
 *    long, long      -> long
 *    long, double    -> long
 *    double, long    -> long
 *    double, double  -> long
 */
public static <T> ExprVectorProcessor<T> makeLongMathProcessor(Expr.VectorInputBindingInspector inspector, Expr left, Expr right, 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.LONG)) {
        if (Types.isNullOr(rightType, ExprType.LONG)) {
            processor = longOutLongsInProcessor.get();
        } else if (rightType.is(ExprType.DOUBLE)) {
            processor = longOutLongDoubleInProcessor.get();
        }
    } else if (Types.is(leftType, ExprType.DOUBLE)) {
        if (Types.is(rightType, ExprType.LONG)) {
            processor = longOutDoubleLongInProcessor.get();
        } else if (Types.isNullOr(rightType, ExprType.DOUBLE)) {
            processor = longOutDoublesInProcessor.get();
        }
    } else if (leftType == null) {
        if (Types.is(rightType, ExprType.LONG)) {
            processor = longOutLongsInProcessor.get();
        } else if (Types.is(rightType, ExprType.DOUBLE)) {
            processor = longOutDoublesInProcessor.get();
        }
    }
    if (processor == null) {
        throw Exprs.cannotVectorize();
    }
    return (ExprVectorProcessor<T>) processor;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 45 with ExpressionType

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

the class VectorProcessors method isNotNull.

public static <T> ExprVectorProcessor<T> isNotNull(Expr.VectorInputBindingInspector inspector, Expr expr) {
    final ExpressionType type = expr.getOutputType(inspector);
    if (type == null) {
        return constant(0L, 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] = 0L;
                    } else {
                        outputValues[i] = 1L;
                    }
                }
                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, 1L);
                } else {
                    for (int i = 0; i < currentSize; i++) {
                        if (nulls[i]) {
                            outputValues[i] = 0L;
                        } else {
                            outputValues[i] = 1L;
                        }
                    }
                }
                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, 1L);
                } else {
                    for (int i = 0; i < currentSize; i++) {
                        if (nulls[i]) {
                            outputValues[i] = 0L;
                        } else {
                            outputValues[i] = 1L;
                        }
                    }
                }
                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)

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