Search in sources :

Example 11 with ColumnValueSelector

use of org.apache.druid.segment.ColumnValueSelector in project druid by druid-io.

the class IncrementalIndex method makeColumnSelectorFactory.

/**
 * Column selector used at ingestion time for inputs to aggregators.
 *
 * @param agg                       the aggregator
 * @param in                        ingestion-time input row supplier
 * @param deserializeComplexMetrics whether complex objects should be deserialized by a {@link ComplexMetricExtractor}
 *
 * @return column selector factory
 */
public static ColumnSelectorFactory makeColumnSelectorFactory(final VirtualColumns virtualColumns, final AggregatorFactory agg, final Supplier<InputRow> in, final boolean deserializeComplexMetrics) {
    // we use RowSignature.empty() because ColumnInspector here should be the InputRow schema, not the
    // IncrementalIndex schema, because we are reading values from the InputRow
    final RowBasedColumnSelectorFactory<InputRow> baseSelectorFactory = RowBasedColumnSelectorFactory.create(RowAdapters.standardRow(), in::get, RowSignature.empty(), true);
    class IncrementalIndexInputRowColumnSelectorFactory implements ColumnSelectorFactory {

        @Override
        public ColumnValueSelector<?> makeColumnValueSelector(final String column) {
            final boolean isComplexMetric = agg.getIntermediateType().is(ValueType.COMPLEX);
            final ColumnValueSelector selector = baseSelectorFactory.makeColumnValueSelector(column);
            if (!isComplexMetric || !deserializeComplexMetrics) {
                return selector;
            } else {
                // Wrap selector in a special one that uses ComplexMetricSerde to modify incoming objects.
                // For complex aggregators that read from multiple columns, we wrap all of them. This is not ideal but it
                // has worked so far.
                final String complexTypeName = agg.getIntermediateType().getComplexTypeName();
                final ComplexMetricSerde serde = ComplexMetrics.getSerdeForType(complexTypeName);
                if (serde == null) {
                    throw new ISE("Don't know how to handle type[%s]", complexTypeName);
                }
                final ComplexMetricExtractor extractor = serde.getExtractor();
                return new ColumnValueSelector() {

                    @Override
                    public boolean isNull() {
                        return selector.isNull();
                    }

                    @Override
                    public long getLong() {
                        return selector.getLong();
                    }

                    @Override
                    public float getFloat() {
                        return selector.getFloat();
                    }

                    @Override
                    public double getDouble() {
                        return selector.getDouble();
                    }

                    @Override
                    public Class classOfObject() {
                        return extractor.extractedClass();
                    }

                    @Nullable
                    @Override
                    public Object getObject() {
                        // Here is where the magic happens: read from "in" directly, don't go through the normal "selector".
                        return extractor.extractValue(in.get(), column, agg);
                    }

                    @Override
                    public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                        inspector.visit("in", in);
                        inspector.visit("selector", selector);
                        inspector.visit("extractor", extractor);
                    }
                };
            }
        }

        @Override
        public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec) {
            return baseSelectorFactory.makeDimensionSelector(dimensionSpec);
        }

        @Nullable
        @Override
        public ColumnCapabilities getColumnCapabilities(String columnName) {
            return baseSelectorFactory.getColumnCapabilities(columnName);
        }
    }
    return virtualColumns.wrap(new IncrementalIndexInputRowColumnSelectorFactory());
}
Also used : DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ComplexMetricSerde(org.apache.druid.segment.serde.ComplexMetricSerde) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) RowBasedColumnSelectorFactory(org.apache.druid.segment.RowBasedColumnSelectorFactory) ComplexMetricExtractor(org.apache.druid.segment.serde.ComplexMetricExtractor) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector) InputRow(org.apache.druid.data.input.InputRow) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) ISE(org.apache.druid.java.util.common.ISE) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) NilColumnValueSelector(org.apache.druid.segment.NilColumnValueSelector)

Example 12 with ColumnValueSelector

use of org.apache.druid.segment.ColumnValueSelector in project druid by druid-io.

the class HllSketchAggregatorFactory method makeAggregateCombiner.

@Override
public AggregateCombiner makeAggregateCombiner() {
    return new ObjectAggregateCombiner<HllSketch>() {

        private final Union union = new Union(lgK);

        @Override
        public void reset(final ColumnValueSelector selector) {
            union.reset();
            fold(selector);
        }

        @Override
        public void fold(final ColumnValueSelector selector) {
            final HllSketch sketch = (HllSketch) selector.getObject();
            union.update(sketch);
        }

        @Nullable
        @Override
        public HllSketch getObject() {
            return union.getResult(tgtHllType);
        }

        @Override
        public Class<HllSketch> classOfObject() {
            return HllSketch.class;
        }
    };
}
Also used : HllSketch(org.apache.datasketches.hll.HllSketch) ObjectAggregateCombiner(org.apache.druid.query.aggregation.ObjectAggregateCombiner) Union(org.apache.datasketches.hll.Union) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector)

Example 13 with ColumnValueSelector

use of org.apache.druid.segment.ColumnValueSelector in project druid by druid-io.

the class DoublesSketchAggregatorFactory method makeAggregateCombiner.

@Override
public AggregateCombiner makeAggregateCombiner() {
    return new ObjectAggregateCombiner<DoublesSketch>() {

        private final DoublesUnion union = DoublesUnion.builder().setMaxK(k).build();

        @Override
        public void reset(final ColumnValueSelector selector) {
            union.reset();
            fold(selector);
        }

        @Override
        public void fold(final ColumnValueSelector selector) {
            final DoublesSketch sketch = (DoublesSketch) selector.getObject();
            union.update(sketch);
        }

        @Nullable
        @Override
        public DoublesSketch getObject() {
            return union.getResult();
        }

        @Override
        public Class<DoublesSketch> classOfObject() {
            return DoublesSketch.class;
        }
    };
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) DoublesUnion(org.apache.datasketches.quantiles.DoublesUnion) ObjectAggregateCombiner(org.apache.druid.query.aggregation.ObjectAggregateCombiner) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) NilColumnValueSelector(org.apache.druid.segment.NilColumnValueSelector) BaseDoubleColumnValueSelector(org.apache.druid.segment.BaseDoubleColumnValueSelector)

Example 14 with ColumnValueSelector

use of org.apache.druid.segment.ColumnValueSelector in project druid by druid-io.

the class ExpressionSelectorBenchmark method strlenUsingExpressionAsLong.

@Benchmark
public void strlenUsingExpressionAsLong(Blackhole blackhole) {
    final Sequence<Cursor> cursors = new QueryableIndexStorageAdapter(index).makeCursors(null, index.getDataInterval(), VirtualColumns.create(ImmutableList.of(new ExpressionVirtualColumn("v", "strlen(s)", ColumnType.STRING, TestExprMacroTable.INSTANCE))), Granularities.ALL, false, null);
    final List<?> results = cursors.map(cursor -> {
        final ColumnValueSelector selector = cursor.getColumnSelectorFactory().makeColumnValueSelector("v");
        consumeLong(cursor, selector, blackhole);
        return null;
    }).toList();
    blackhole.consume(results);
}
Also used : BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Measurement(org.openjdk.jmh.annotations.Measurement) Intervals(org.apache.druid.java.util.common.Intervals) SegmentGenerator(org.apache.druid.segment.generator.SegmentGenerator) Blackhole(org.openjdk.jmh.infra.Blackhole) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) Scope(org.openjdk.jmh.annotations.Scope) Warmup(org.openjdk.jmh.annotations.Warmup) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ColumnHolder(org.apache.druid.segment.column.ColumnHolder) ImmutableList(com.google.common.collect.ImmutableList) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) DimensionSelector(org.apache.druid.segment.DimensionSelector) TearDown(org.openjdk.jmh.annotations.TearDown) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) Sequence(org.apache.druid.java.util.common.guava.Sequence) Setup(org.openjdk.jmh.annotations.Setup) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) VirtualColumns(org.apache.druid.segment.VirtualColumns) GeneratorColumnSchema(org.apache.druid.segment.generator.GeneratorColumnSchema) Mode(org.openjdk.jmh.annotations.Mode) Closer(org.apache.druid.java.util.common.io.Closer) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) Param(org.openjdk.jmh.annotations.Param) QueryableIndex(org.apache.druid.segment.QueryableIndex) GeneratorSchemaInfo(org.apache.druid.segment.generator.GeneratorSchemaInfo) ValueType(org.apache.druid.segment.column.ValueType) TestExprMacroTable(org.apache.druid.query.expression.TestExprMacroTable) State(org.openjdk.jmh.annotations.State) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) Benchmark(org.openjdk.jmh.annotations.Benchmark) Granularities(org.apache.druid.java.util.common.granularity.Granularities) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Cursor(org.apache.druid.segment.Cursor) NullHandling(org.apache.druid.common.config.NullHandling) LinearShardSpec(org.apache.druid.timeline.partition.LinearShardSpec) DataSegment(org.apache.druid.timeline.DataSegment) Level(org.openjdk.jmh.annotations.Level) ColumnType(org.apache.druid.segment.column.ColumnType) Fork(org.openjdk.jmh.annotations.Fork) BitSet(java.util.BitSet) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) Cursor(org.apache.druid.segment.Cursor) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 15 with ColumnValueSelector

use of org.apache.druid.segment.ColumnValueSelector in project druid by druid-io.

the class ExpressionSelectorBenchmark method stringConcatAndCompareOnLong.

@Benchmark
public void stringConcatAndCompareOnLong(Blackhole blackhole) {
    final Sequence<Cursor> cursors = new QueryableIndexStorageAdapter(index).makeCursors(null, index.getDataInterval(), VirtualColumns.create(ImmutableList.of(new ExpressionVirtualColumn("v", "concat(n, ' is my favorite number') == '3 is my favorite number'", ColumnType.LONG, TestExprMacroTable.INSTANCE))), Granularities.ALL, false, null);
    final List<?> results = cursors.map(cursor -> {
        final ColumnValueSelector selector = cursor.getColumnSelectorFactory().makeColumnValueSelector("v");
        consumeLong(cursor, selector, blackhole);
        return null;
    }).toList();
    blackhole.consume(results);
}
Also used : BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Measurement(org.openjdk.jmh.annotations.Measurement) Intervals(org.apache.druid.java.util.common.Intervals) SegmentGenerator(org.apache.druid.segment.generator.SegmentGenerator) Blackhole(org.openjdk.jmh.infra.Blackhole) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) Scope(org.openjdk.jmh.annotations.Scope) Warmup(org.openjdk.jmh.annotations.Warmup) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ColumnHolder(org.apache.druid.segment.column.ColumnHolder) ImmutableList(com.google.common.collect.ImmutableList) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) DimensionSelector(org.apache.druid.segment.DimensionSelector) TearDown(org.openjdk.jmh.annotations.TearDown) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) Sequence(org.apache.druid.java.util.common.guava.Sequence) Setup(org.openjdk.jmh.annotations.Setup) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) VirtualColumns(org.apache.druid.segment.VirtualColumns) GeneratorColumnSchema(org.apache.druid.segment.generator.GeneratorColumnSchema) Mode(org.openjdk.jmh.annotations.Mode) Closer(org.apache.druid.java.util.common.io.Closer) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) Param(org.openjdk.jmh.annotations.Param) QueryableIndex(org.apache.druid.segment.QueryableIndex) GeneratorSchemaInfo(org.apache.druid.segment.generator.GeneratorSchemaInfo) ValueType(org.apache.druid.segment.column.ValueType) TestExprMacroTable(org.apache.druid.query.expression.TestExprMacroTable) State(org.openjdk.jmh.annotations.State) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) Benchmark(org.openjdk.jmh.annotations.Benchmark) Granularities(org.apache.druid.java.util.common.granularity.Granularities) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Cursor(org.apache.druid.segment.Cursor) NullHandling(org.apache.druid.common.config.NullHandling) LinearShardSpec(org.apache.druid.timeline.partition.LinearShardSpec) DataSegment(org.apache.druid.timeline.DataSegment) Level(org.openjdk.jmh.annotations.Level) ColumnType(org.apache.druid.segment.column.ColumnType) Fork(org.openjdk.jmh.annotations.Fork) BitSet(java.util.BitSet) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) Cursor(org.apache.druid.segment.Cursor) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Aggregations

ColumnValueSelector (org.apache.druid.segment.ColumnValueSelector)32 Test (org.junit.Test)18 Cursor (org.apache.druid.segment.Cursor)13 QueryableIndexStorageAdapter (org.apache.druid.segment.QueryableIndexStorageAdapter)12 ImmutableList (com.google.common.collect.ImmutableList)9 TestExprMacroTable (org.apache.druid.query.expression.TestExprMacroTable)9 ResultRow (org.apache.druid.query.groupby.ResultRow)9 VirtualColumns (org.apache.druid.segment.VirtualColumns)9 List (java.util.List)8 NullHandling (org.apache.druid.common.config.NullHandling)8 Granularities (org.apache.druid.java.util.common.granularity.Granularities)8 Sequence (org.apache.druid.java.util.common.guava.Sequence)8 Closer (org.apache.druid.java.util.common.io.Closer)8 QueryableIndex (org.apache.druid.segment.QueryableIndex)8 GeneratorSchemaInfo (org.apache.druid.segment.generator.GeneratorSchemaInfo)8 SegmentGenerator (org.apache.druid.segment.generator.SegmentGenerator)8 DataSegment (org.apache.druid.timeline.DataSegment)8 LinearShardSpec (org.apache.druid.timeline.partition.LinearShardSpec)8 TimeUnit (java.util.concurrent.TimeUnit)7 Expr (org.apache.druid.math.expr.Expr)7