Search in sources :

Example 16 with ExpressionType

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

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

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

the class VectorProcessors method not.

public static <T> ExprVectorProcessor<T> not(Expr.VectorInputBindingInspector inspector, Expr expr) {
    final ExpressionType inputType = expr.getOutputType(inspector);
    final int maxVectorSize = inspector.getMaxVectorSize();
    ExprVectorProcessor<?> processor = null;
    if (Types.is(inputType, ExprType.STRING)) {
        processor = new LongOutStringInFunctionVectorProcessor(expr.buildVectorized(inspector), maxVectorSize) {

            @Override
            public void processIndex(String[] strings, long[] longs, boolean[] outputNulls, int i) {
                outputNulls[i] = strings[i] == null;
                if (!outputNulls[i]) {
                    longs[i] = Evals.asLong(!Evals.asBoolean(strings[i]));
                }
            }
        };
    } else if (Types.is(inputType, ExprType.LONG)) {
        processor = new LongOutLongInFunctionVectorValueProcessor(expr.buildVectorized(inspector), maxVectorSize) {

            @Override
            public long apply(long input) {
                return Evals.asLong(!Evals.asBoolean(input));
            }
        };
    } else if (Types.is(inputType, ExprType.DOUBLE)) {
        if (!ExpressionProcessing.useStrictBooleans()) {
            processor = new DoubleOutDoubleInFunctionVectorValueProcessor(expr.buildVectorized(inspector), maxVectorSize) {

                @Override
                public double apply(double input) {
                    return Evals.asDouble(!Evals.asBoolean(input));
                }
            };
        } else {
            processor = new LongOutDoubleInFunctionVectorValueProcessor(expr.buildVectorized(inspector), maxVectorSize) {

                @Override
                public long apply(double input) {
                    return Evals.asLong(!Evals.asBoolean(input));
                }
            };
        }
    }
    if (processor == null) {
        throw Exprs.cannotVectorize();
    }
    return (ExprVectorProcessor<T>) processor;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 18 with ExpressionType

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

the class VectorMathProcessors method bitwiseConvertDoubleToLongBits.

public static <T> ExprVectorProcessor<T> bitwiseConvertDoubleToLongBits(Expr.VectorInputBindingInspector inspector, Expr arg) {
    final ExpressionType inputType = arg.getOutputType(inspector);
    ExprVectorProcessor<?> processor = null;
    if (Types.is(inputType, ExprType.LONG)) {
        processor = new LongOutLongInFunctionVectorValueProcessor(arg.buildVectorized(inspector), inspector.getMaxVectorSize()) {

            @Override
            public long apply(long input) {
                return Double.doubleToLongBits(input);
            }
        };
    } else if (Types.is(inputType, ExprType.DOUBLE)) {
        processor = new LongOutDoubleInFunctionVectorValueProcessor(arg.buildVectorized(inspector), inspector.getMaxVectorSize()) {

            @Override
            public long apply(double input) {
                return Double.doubleToLongBits(input);
            }
        };
    }
    if (processor == null) {
        throw Exprs.cannotVectorize();
    }
    return (ExprVectorProcessor<T>) processor;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 19 with ExpressionType

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

the class VectorMathProcessors method bitwiseConvertLongBitsToDouble.

public static <T> ExprVectorProcessor<T> bitwiseConvertLongBitsToDouble(Expr.VectorInputBindingInspector inspector, Expr arg) {
    final ExpressionType inputType = arg.getOutputType(inspector);
    ExprVectorProcessor<?> processor = null;
    if (Types.is(inputType, ExprType.LONG)) {
        processor = new DoubleOutLongInFunctionVectorValueProcessor(arg.buildVectorized(inspector), inspector.getMaxVectorSize()) {

            @Override
            public double apply(long input) {
                return Double.longBitsToDouble(input);
            }
        };
    } else if (Types.is(inputType, ExprType.DOUBLE)) {
        processor = new DoubleOutDoubleInFunctionVectorValueProcessor(arg.buildVectorized(inspector), inspector.getMaxVectorSize()) {

            @Override
            public double apply(double input) {
                return Double.longBitsToDouble((long) input);
            }
        };
    }
    if (processor == null) {
        throw Exprs.cannotVectorize();
    }
    return (ExprVectorProcessor<T>) processor;
}
Also used : ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 20 with ExpressionType

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

the class VectorComparisonProcessors method makeComparisonProcessor.

@Deprecated
public static <T> ExprVectorProcessor<T> makeComparisonProcessor(Expr.VectorInputBindingInspector inspector, Expr left, Expr right, Supplier<LongOutStringsInFunctionVectorProcessor> longOutStringsInFunctionVectorProcessor, Supplier<LongOutLongsInFunctionVectorValueProcessor> longOutLongsInProcessor, Supplier<DoubleOutLongDoubleInFunctionVectorValueProcessor> doubleOutLongDoubleInProcessor, Supplier<DoubleOutDoubleLongInFunctionVectorValueProcessor> doubleOutDoubleLongInProcessor, Supplier<DoubleOutDoublesInFunctionVectorValueProcessor> doubleOutDoublesInProcessor) {
    assert !ExpressionProcessing.useStrictBooleans();
    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 = doubleOutDoublesInProcessor.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 = doubleOutDoublesInProcessor.get();
    }
    if (processor != null) {
        return (ExprVectorProcessor<T>) processor;
    }
    // fall through to normal math processor logic
    return VectorMathProcessors.makeMathProcessor(inspector, left, right, longOutLongsInProcessor, doubleOutLongDoubleInProcessor, doubleOutDoubleLongInProcessor, doubleOutDoublesInProcessor);
}
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