Search in sources :

Example 1 with ComplexMetricExtractor

use of org.apache.druid.segment.serde.ComplexMetricExtractor 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 2 with ComplexMetricExtractor

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

the class DoublesSketchComplexMetricSerdeTest method testExtractorOnDecimalNumber.

@Test
public void testExtractorOnDecimalNumber() {
    final DoublesSketchComplexMetricSerde serde = new DoublesSketchComplexMetricSerde();
    final ComplexMetricExtractor extractor = serde.getExtractor();
    final DoublesSketch sketch = (DoublesSketch) extractor.extractValue(new MapBasedInputRow(0L, ImmutableList.of(), ImmutableMap.of("foo", "3.1")), "foo");
    Assert.assertEquals(1, sketch.getRetainedItems());
    Assert.assertEquals(3.1d, sketch.getMaxValue(), 0.01d);
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) ComplexMetricExtractor(org.apache.druid.segment.serde.ComplexMetricExtractor) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) Test(org.junit.Test)

Example 3 with ComplexMetricExtractor

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

the class DoublesSketchComplexMetricSerdeTest method testExtractorOnNegativeNumber.

@Test
public void testExtractorOnNegativeNumber() {
    final DoublesSketchComplexMetricSerde serde = new DoublesSketchComplexMetricSerde();
    final ComplexMetricExtractor extractor = serde.getExtractor();
    final DoublesSketch sketch = (DoublesSketch) extractor.extractValue(new MapBasedInputRow(0L, ImmutableList.of(), ImmutableMap.of("foo", "-133")), "foo");
    Assert.assertEquals(1, sketch.getRetainedItems());
    Assert.assertEquals(-133d, sketch.getMaxValue(), 0.01d);
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) ComplexMetricExtractor(org.apache.druid.segment.serde.ComplexMetricExtractor) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) Test(org.junit.Test)

Example 4 with ComplexMetricExtractor

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

the class DoublesSketchComplexMetricSerdeTest method testExtractorOnEmptyString.

@Test
public void testExtractorOnEmptyString() {
    final DoublesSketchComplexMetricSerde serde = new DoublesSketchComplexMetricSerde();
    final ComplexMetricExtractor extractor = serde.getExtractor();
    final DoublesSketch sketch = (DoublesSketch) extractor.extractValue(new MapBasedInputRow(0L, ImmutableList.of(), ImmutableMap.of("foo", "")), "foo");
    Assert.assertEquals(0, sketch.getRetainedItems());
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) ComplexMetricExtractor(org.apache.druid.segment.serde.ComplexMetricExtractor) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) Test(org.junit.Test)

Example 5 with ComplexMetricExtractor

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

the class DoublesSketchComplexMetricSerdeTest method testExtractorOnLeadingDecimalNumber.

@Test
public void testExtractorOnLeadingDecimalNumber() {
    final DoublesSketchComplexMetricSerde serde = new DoublesSketchComplexMetricSerde();
    final ComplexMetricExtractor extractor = serde.getExtractor();
    final DoublesSketch sketch = (DoublesSketch) extractor.extractValue(new MapBasedInputRow(0L, ImmutableList.of(), ImmutableMap.of("foo", ".1")), "foo");
    Assert.assertEquals(1, sketch.getRetainedItems());
    Assert.assertEquals(0.1d, sketch.getMaxValue(), 0.01d);
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) ComplexMetricExtractor(org.apache.druid.segment.serde.ComplexMetricExtractor) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) Test(org.junit.Test)

Aggregations

ComplexMetricExtractor (org.apache.druid.segment.serde.ComplexMetricExtractor)8 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)7 Test (org.junit.Test)6 DoublesSketch (org.apache.datasketches.quantiles.DoublesSketch)5 InputRow (org.apache.druid.data.input.InputRow)2 HashMap (java.util.HashMap)1 HyperLogLogCollector (org.apache.druid.hll.HyperLogLogCollector)1 ISE (org.apache.druid.java.util.common.ISE)1 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)1 RuntimeShapeInspector (org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)1 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)1 ColumnValueSelector (org.apache.druid.segment.ColumnValueSelector)1 NilColumnValueSelector (org.apache.druid.segment.NilColumnValueSelector)1 RowBasedColumnSelectorFactory (org.apache.druid.segment.RowBasedColumnSelectorFactory)1 ComplexMetricSerde (org.apache.druid.segment.serde.ComplexMetricSerde)1