Search in sources :

Example 51 with Expr

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

the class BloomFilterExpressionsTest method testCreate.

@Test
public void testCreate() {
    Expr expr = Parser.parse("bloom_filter(100)", macroTable);
    ExprEval eval = expr.eval(inputBindings);
    Assert.assertEquals(BloomFilterExpressions.BLOOM_FILTER_TYPE, eval.type());
    Assert.assertTrue(eval.value() instanceof BloomKFilter);
    Assert.assertEquals(1024, ((BloomKFilter) eval.value()).getBitSize());
}
Also used : ExprEval(org.apache.druid.math.expr.ExprEval) Expr(org.apache.druid.math.expr.Expr) BloomKFilter(org.apache.druid.query.filter.BloomKFilter) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 52 with Expr

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

the class BloomFilterExpressionsTest method testFilter.

@Test
public void testFilter() {
    Expr expr = Parser.parse("bloom_filter_test(1.234, bloom_filter_add(1.234, bloomy))", macroTable);
    ExprEval eval = expr.eval(inputBindings);
    Assert.assertEquals(ExpressionType.LONG, eval.type());
    Assert.assertTrue(eval.asBoolean());
    expr = Parser.parse("bloom_filter_test(1234, bloom_filter_add(1234, bloomy))", macroTable);
    eval = expr.eval(inputBindings);
    Assert.assertTrue(eval.asBoolean());
    expr = Parser.parse("bloom_filter_test('foo', bloom_filter_add('foo', bloomy))", macroTable);
    eval = expr.eval(inputBindings);
    Assert.assertTrue(eval.asBoolean());
    expr = Parser.parse("bloom_filter_test('bar', bloom_filter_add('foo', bloomy))", macroTable);
    eval = expr.eval(inputBindings);
    Assert.assertFalse(eval.asBoolean());
    expr = Parser.parse("bloom_filter_test(1234, bloom_filter_add('foo', bloomy))", macroTable);
    eval = expr.eval(inputBindings);
    Assert.assertFalse(eval.asBoolean());
    expr = Parser.parse("bloom_filter_test(1.23, bloom_filter_add('foo', bloomy))", macroTable);
    eval = expr.eval(inputBindings);
    Assert.assertFalse(eval.asBoolean());
    expr = Parser.parse("bloom_filter_test(1234, bloom_filter_add(1234, bloom_filter(100)))", macroTable);
    eval = expr.eval(inputBindings);
    Assert.assertTrue(eval.asBoolean());
    expr = Parser.parse("bloom_filter_test(4321, bloom_filter_add(1234, bloom_filter(100)))", macroTable);
    eval = expr.eval(inputBindings);
    Assert.assertFalse(eval.asBoolean());
    expr = Parser.parse("bloom_filter_test(4321, bloom_filter_add(bloom_filter_add(1234, bloom_filter(100)), bloom_filter_add(4321, bloom_filter(100))))", macroTable);
    eval = expr.eval(inputBindings);
    Assert.assertTrue(eval.asBoolean());
}
Also used : ExprEval(org.apache.druid.math.expr.ExprEval) Expr(org.apache.druid.math.expr.Expr) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 53 with Expr

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

the class ExpressionVectorSelectorBenchmark method setup.

@Setup(Level.Trial)
public void setup() {
    this.closer = Closer.create();
    final GeneratorSchemaInfo schemaInfo = GeneratorBasicSchemas.SCHEMA_MAP.get("expression-testbench");
    final DataSegment dataSegment = DataSegment.builder().dataSource("foo").interval(schemaInfo.getDataInterval()).version("1").shardSpec(new LinearShardSpec(0)).size(0).build();
    final SegmentGenerator segmentGenerator = closer.register(new SegmentGenerator());
    this.index = closer.register(segmentGenerator.generate(dataSegment, schemaInfo, Granularities.HOUR, rowsPerSegment));
    Expr parsed = Parser.parse(expression, ExprMacroTable.nil());
    outputType = parsed.getOutputType(new ColumnInspector() {

        @Nullable
        @Override
        public ColumnCapabilities getColumnCapabilities(String column) {
            return QueryableIndexStorageAdapter.getColumnCapabilities(index, column);
        }
    });
    checkSanity();
}
Also used : SegmentGenerator(org.apache.druid.segment.generator.SegmentGenerator) Expr(org.apache.druid.math.expr.Expr) LinearShardSpec(org.apache.druid.timeline.partition.LinearShardSpec) GeneratorSchemaInfo(org.apache.druid.segment.generator.GeneratorSchemaInfo) ColumnInspector(org.apache.druid.segment.ColumnInspector) DataSegment(org.apache.druid.timeline.DataSegment) Setup(org.openjdk.jmh.annotations.Setup)

Example 54 with Expr

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

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)

Example 55 with Expr

use of org.apache.druid.math.expr.Expr 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)

Aggregations

Expr (org.apache.druid.math.expr.Expr)104 Test (org.junit.Test)58 ExprEval (org.apache.druid.math.expr.ExprEval)18 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)17 IAE (org.apache.druid.java.util.common.IAE)14 ExpressionType (org.apache.druid.math.expr.ExpressionType)8 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)7 ArrayList (java.util.ArrayList)6 Nullable (javax.annotation.Nullable)6 HashSet (java.util.HashSet)5 List (java.util.List)4 HyperLogLogCollector (org.apache.druid.hll.HyperLogLogCollector)4 BloomKFilter (org.apache.druid.query.filter.BloomKFilter)4 InDimFilter (org.apache.druid.query.filter.InDimFilter)4 RexNode (org.apache.calcite.rex.RexNode)3 Filter (org.apache.druid.query.filter.Filter)3 VirtualColumn (org.apache.druid.segment.VirtualColumn)3 FalseFilter (org.apache.druid.segment.filter.FalseFilter)3 OrFilter (org.apache.druid.segment.filter.OrFilter)3 SelectorFilter (org.apache.druid.segment.filter.SelectorFilter)3