Search in sources :

Example 11 with RuntimeShapeInspector

use of org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector in project druid by druid-io.

the class QueryableIndexCursorSequenceBuilderTest method testTimeSearch.

@Test
public void testTimeSearch() {
    final int[] values = new int[] { 0, 1, 1, 1, 1, 1, 1, 1, 5, 7, 10 };
    final NumericColumn column = new NumericColumn() {

        @Override
        public int length() {
            return values.length;
        }

        @Override
        public long getLongSingleValueRow(int rowNum) {
            return values[rowNum];
        }

        @Override
        public void close() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ColumnValueSelector<?> makeColumnValueSelector(ReadableOffset offset) {
            throw new UnsupportedOperationException();
        }
    };
    Assert.assertEquals(0, QueryableIndexCursorSequenceBuilder.timeSearch(column, 0, 0, values.length));
    Assert.assertEquals(2, QueryableIndexCursorSequenceBuilder.timeSearch(column, 0, 2, values.length));
    Assert.assertEquals(0, QueryableIndexCursorSequenceBuilder.timeSearch(column, 0, 0, values.length / 2));
    Assert.assertEquals(1, QueryableIndexCursorSequenceBuilder.timeSearch(column, 1, 0, values.length));
    Assert.assertEquals(2, QueryableIndexCursorSequenceBuilder.timeSearch(column, 1, 2, values.length));
    Assert.assertEquals(1, QueryableIndexCursorSequenceBuilder.timeSearch(column, 1, 0, values.length / 2));
    Assert.assertEquals(1, QueryableIndexCursorSequenceBuilder.timeSearch(column, 1, 1, 8));
    Assert.assertEquals(8, QueryableIndexCursorSequenceBuilder.timeSearch(column, 2, 0, values.length));
    Assert.assertEquals(10, QueryableIndexCursorSequenceBuilder.timeSearch(column, 10, 0, values.length));
    Assert.assertEquals(11, QueryableIndexCursorSequenceBuilder.timeSearch(column, 15, 0, values.length));
}
Also used : NumericColumn(org.apache.druid.segment.column.NumericColumn) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector) ReadableOffset(org.apache.druid.segment.data.ReadableOffset) Test(org.junit.Test)

Example 12 with RuntimeShapeInspector

use of org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector 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 13 with RuntimeShapeInspector

use of org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector in project druid by druid-io.

the class FilteredAggregatorTest method makeColumnSelector.

private ColumnSelectorFactory makeColumnSelector(final TestFloatColumnSelector selector) {
    return new ColumnSelectorFactory() {

        @Override
        public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec) {
            final String dimensionName = dimensionSpec.getDimension();
            if ("dim".equals(dimensionName)) {
                return dimensionSpec.decorate(new AbstractDimensionSelector() {

                    @Override
                    public IndexedInts getRow() {
                        SingleIndexedInt row = new SingleIndexedInt();
                        if (selector.getIndex() % 3 == 2) {
                            row.setValue(1);
                        } else {
                            row.setValue(0);
                        }
                        return row;
                    }

                    @Override
                    public ValueMatcher makeValueMatcher(String value) {
                        return DimensionSelectorUtils.makeValueMatcherGeneric(this, value);
                    }

                    @Override
                    public ValueMatcher makeValueMatcher(Predicate<String> predicate) {
                        return DimensionSelectorUtils.makeValueMatcherGeneric(this, predicate);
                    }

                    @Override
                    public int getValueCardinality() {
                        return 2;
                    }

                    @Override
                    public String lookupName(int id) {
                        switch(id) {
                            case 0:
                                return "a";
                            case 1:
                                return "b";
                            default:
                                throw new IllegalArgumentException();
                        }
                    }

                    @Override
                    public boolean nameLookupPossibleInAdvance() {
                        return true;
                    }

                    @Nullable
                    @Override
                    public IdLookup idLookup() {
                        return new IdLookup() {

                            @Override
                            public int lookupId(String name) {
                                switch(name) {
                                    case "a":
                                        return 0;
                                    case "b":
                                        return 1;
                                    default:
                                        throw new IllegalArgumentException();
                                }
                            }
                        };
                    }

                    @Override
                    public Class classOfObject() {
                        return Object.class;
                    }

                    @Override
                    public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                    // Don't care about runtime shape in tests
                    }
                });
            } else {
                throw new UnsupportedOperationException();
            }
        }

        @Override
        public ColumnValueSelector<?> makeColumnValueSelector(String columnName) {
            if ("value".equals(columnName)) {
                return selector;
            } else {
                throw new UnsupportedOperationException();
            }
        }

        @Override
        public ColumnCapabilities getColumnCapabilities(String columnName) {
            ColumnCapabilitiesImpl caps;
            if ("value".equals(columnName)) {
                caps = new ColumnCapabilitiesImpl();
                caps.setType(ColumnType.FLOAT);
                caps.setDictionaryEncoded(false);
                caps.setHasBitmapIndexes(false);
            } else {
                caps = new ColumnCapabilitiesImpl();
                caps.setType(ColumnType.STRING);
                caps.setDictionaryEncoded(true);
                caps.setHasBitmapIndexes(true);
            }
            return caps;
        }
    };
}
Also used : DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) SingleIndexedInt(org.apache.druid.segment.data.SingleIndexedInt) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector) IdLookup(org.apache.druid.segment.IdLookup) AbstractDimensionSelector(org.apache.druid.segment.AbstractDimensionSelector) IndexedInts(org.apache.druid.segment.data.IndexedInts) Nullable(javax.annotation.Nullable) ColumnCapabilitiesImpl(org.apache.druid.segment.column.ColumnCapabilitiesImpl)

Example 14 with RuntimeShapeInspector

use of org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector in project druid by druid-io.

the class ForwardingFilteredDimensionSelector method makeValueMatcher.

@Override
public ValueMatcher makeValueMatcher(final String value) {
    IdLookup idLookup = idLookup();
    if (idLookup != null) {
        final int valueId = idLookup.lookupId(value);
        if (valueId >= 0 || value == null) {
            return new ValueMatcher() {

                @Override
                public boolean matches() {
                    final IndexedInts baseRow = selector.getRow();
                    final int baseRowSize = baseRow.size();
                    boolean nullRow = true;
                    for (int i = 0; i < baseRowSize; i++) {
                        int forwardedValue = idMapping.getForwardedId(baseRow.get(i));
                        if (forwardedValue >= 0) {
                            // valueId is -1, we don't want to return true from matches().
                            if (forwardedValue == valueId) {
                                return true;
                            }
                            nullRow = false;
                        }
                    }
                    // null should match empty rows in multi-value columns
                    return nullRow && value == null;
                }

                @Override
                public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                    inspector.visit("selector", selector);
                }
            };
        } else {
            return BooleanValueMatcher.of(false);
        }
    } else {
        // Employ precomputed BitSet optimization
        return makeValueMatcher(Predicates.equalTo(value));
    }
}
Also used : IdLookup(org.apache.druid.segment.IdLookup) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) BooleanValueMatcher(org.apache.druid.segment.filter.BooleanValueMatcher) IndexedInts(org.apache.druid.segment.data.IndexedInts) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)

Example 15 with RuntimeShapeInspector

use of org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector in project druid by druid-io.

the class PredicateFilteredDimensionSelector method makeValueMatcher.

@Override
public ValueMatcher makeValueMatcher(final Predicate<String> matcherPredicate) {
    final boolean matchNull = predicate.apply(null);
    return new ValueMatcher() {

        @Override
        public boolean matches() {
            final IndexedInts baseRow = selector.getRow();
            final int baseRowSize = baseRow.size();
            boolean nullRow = true;
            for (int i = 0; i < baseRowSize; ++i) {
                String rowValue = lookupName(baseRow.get(i));
                if (predicate.apply(rowValue)) {
                    if (matcherPredicate.apply(rowValue)) {
                        return true;
                    }
                    nullRow = false;
                }
            }
            // null should match empty rows in multi-value columns
            return nullRow && matchNull;
        }

        @Override
        public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
            // PredicateFilteredDimensionSelector.this inspects selector and predicate as well.
            inspector.visit("selector", PredicateFilteredDimensionSelector.this);
            inspector.visit("matcherPredicate", matcherPredicate);
        }
    };
}
Also used : ValueMatcher(org.apache.druid.query.filter.ValueMatcher) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) IndexedInts(org.apache.druid.segment.data.IndexedInts) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)

Aggregations

RuntimeShapeInspector (org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)26 ValueMatcher (org.apache.druid.query.filter.ValueMatcher)19 IndexedInts (org.apache.druid.segment.data.IndexedInts)11 BooleanValueMatcher (org.apache.druid.segment.filter.BooleanValueMatcher)8 Nullable (javax.annotation.Nullable)7 Predicate (com.google.common.base.Predicate)5 ArrayBasedIndexedInts (org.apache.druid.segment.data.ArrayBasedIndexedInts)5 BitSet (java.util.BitSet)4 IdLookup (org.apache.druid.segment.IdLookup)4 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)3 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)3 ColumnHolder (org.apache.druid.segment.column.ColumnHolder)3 Test (org.junit.Test)3 InputRow (org.apache.druid.data.input.InputRow)2 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)2 ISE (org.apache.druid.java.util.common.ISE)2 ExtractionFn (org.apache.druid.query.extraction.ExtractionFn)2 AbstractDimensionSelector (org.apache.druid.segment.AbstractDimensionSelector)2 ColumnValueSelector (org.apache.druid.segment.ColumnValueSelector)2 RowBasedColumnSelectorFactory (org.apache.druid.segment.RowBasedColumnSelectorFactory)2