Search in sources :

Example 6 with RuntimeShapeInspector

use of io.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();
            final ExtractionFn extractionFn = dimensionSpec.getExtractionFn();
            if (dimensionName.equals("dim")) {
                return dimensionSpec.decorate(new DimensionSelector() {

                    @Override
                    public IndexedInts getRow() {
                        if (selector.getIndex() % 3 == 2) {
                            return ArrayBasedIndexedInts.of(new int[] { 1 });
                        } else {
                            return ArrayBasedIndexedInts.of(new int[] { 0 });
                        }
                    }

                    @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 void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                    }
                });
            } else {
                throw new UnsupportedOperationException();
            }
        }

        @Override
        public LongColumnSelector makeLongColumnSelector(String columnName) {
            throw new UnsupportedOperationException();
        }

        @Override
        public FloatColumnSelector makeFloatColumnSelector(String columnName) {
            if (columnName.equals("value")) {
                return selector;
            } else {
                throw new UnsupportedOperationException();
            }
        }

        @Override
        public ObjectColumnSelector makeObjectColumnSelector(String columnName) {
            throw new UnsupportedOperationException();
        }

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

Example 7 with RuntimeShapeInspector

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

the class RowBasedColumnSelectorFactory method makeDimensionSelectorUndecorated.

private DimensionSelector makeDimensionSelectorUndecorated(DimensionSpec dimensionSpec) {
    final String dimension = dimensionSpec.getDimension();
    final ExtractionFn extractionFn = dimensionSpec.getExtractionFn();
    if (Column.TIME_COLUMN_NAME.equals(dimensionSpec.getDimension())) {
        if (extractionFn == null) {
            throw new UnsupportedOperationException("time dimension must provide an extraction function");
        }
        return new DimensionSelector() {

            @Override
            public IndexedInts getRow() {
                return ZeroIndexedInts.instance();
            }

            @Override
            public ValueMatcher makeValueMatcher(final String value) {
                return new ValueMatcher() {

                    @Override
                    public boolean matches() {
                        String rowValue = extractionFn.apply(row.get().getTimestampFromEpoch());
                        return Objects.equals(rowValue, value);
                    }
                };
            }

            @Override
            public ValueMatcher makeValueMatcher(final Predicate<String> predicate) {
                return new ValueMatcher() {

                    @Override
                    public boolean matches() {
                        String rowValue = extractionFn.apply(row.get().getTimestampFromEpoch());
                        return predicate.apply(rowValue);
                    }
                };
            }

            @Override
            public int getValueCardinality() {
                return DimensionSelector.CARDINALITY_UNKNOWN;
            }

            @Override
            public String lookupName(int id) {
                return extractionFn.apply(row.get().getTimestampFromEpoch());
            }

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

            @Nullable
            @Override
            public IdLookup idLookup() {
                return null;
            }

            @Override
            public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                inspector.visit("row", row);
                inspector.visit("extractionFn", extractionFn);
            }
        };
    } else {
        return new DimensionSelector() {

            @Override
            public IndexedInts getRow() {
                final List<String> dimensionValues = row.get().getDimension(dimension);
                return RangeIndexedInts.create(dimensionValues != null ? dimensionValues.size() : 0);
            }

            @Override
            public ValueMatcher makeValueMatcher(final String value) {
                if (extractionFn == null) {
                    return new ValueMatcher() {

                        @Override
                        public boolean matches() {
                            final List<String> dimensionValues = row.get().getDimension(dimension);
                            if (dimensionValues == null || dimensionValues.isEmpty()) {
                                return value == null;
                            }
                            for (String dimensionValue : dimensionValues) {
                                if (Objects.equals(Strings.emptyToNull(dimensionValue), value)) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                } else {
                    return new ValueMatcher() {

                        @Override
                        public boolean matches() {
                            final List<String> dimensionValues = row.get().getDimension(dimension);
                            if (dimensionValues == null || dimensionValues.isEmpty()) {
                                return value == null;
                            }
                            for (String dimensionValue : dimensionValues) {
                                if (Objects.equals(extractionFn.apply(Strings.emptyToNull(dimensionValue)), value)) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                }
            }

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

                        @Override
                        public boolean matches() {
                            final List<String> dimensionValues = row.get().getDimension(dimension);
                            if (dimensionValues == null || dimensionValues.isEmpty()) {
                                return matchNull;
                            }
                            for (String dimensionValue : dimensionValues) {
                                if (predicate.apply(Strings.emptyToNull(dimensionValue))) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                } else {
                    return new ValueMatcher() {

                        @Override
                        public boolean matches() {
                            final List<String> dimensionValues = row.get().getDimension(dimension);
                            if (dimensionValues == null || dimensionValues.isEmpty()) {
                                return matchNull;
                            }
                            for (String dimensionValue : dimensionValues) {
                                if (predicate.apply(extractionFn.apply(Strings.emptyToNull(dimensionValue)))) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                }
            }

            @Override
            public int getValueCardinality() {
                return DimensionSelector.CARDINALITY_UNKNOWN;
            }

            @Override
            public String lookupName(int id) {
                final String value = Strings.emptyToNull(row.get().getDimension(dimension).get(id));
                return extractionFn == null ? value : extractionFn.apply(value);
            }

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

            @Nullable
            @Override
            public IdLookup idLookup() {
                return null;
            }

            @Override
            public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                inspector.visit("row", row);
                inspector.visit("extractionFn", extractionFn);
            }
        };
    }
}
Also used : ExtractionFn(io.druid.query.extraction.ExtractionFn) DimensionSelector(io.druid.segment.DimensionSelector) ValueMatcher(io.druid.query.filter.ValueMatcher) RuntimeShapeInspector(io.druid.query.monomorphicprocessing.RuntimeShapeInspector) Predicate(com.google.common.base.Predicate)

Example 8 with RuntimeShapeInspector

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

the class DoubleFirstAggregatorFactory method getCombiningFactory.

@Override
public AggregatorFactory getCombiningFactory() {
    return new DoubleFirstAggregatorFactory(name, name) {

        @Override
        public Aggregator factorize(ColumnSelectorFactory metricFactory) {
            final ObjectColumnSelector selector = metricFactory.makeObjectColumnSelector(name);
            return new DoubleFirstAggregator(name, null, null) {

                @Override
                public void aggregate() {
                    SerializablePair<Long, Double> pair = (SerializablePair<Long, Double>) selector.get();
                    if (pair.lhs < firstTime) {
                        firstTime = pair.lhs;
                        firstValue = pair.rhs;
                    }
                }
            };
        }

        @Override
        public BufferAggregator factorizeBuffered(ColumnSelectorFactory metricFactory) {
            final ObjectColumnSelector selector = metricFactory.makeObjectColumnSelector(name);
            return new DoubleFirstBufferAggregator(null, null) {

                @Override
                public void aggregate(ByteBuffer buf, int position) {
                    SerializablePair<Long, Double> pair = (SerializablePair<Long, Double>) selector.get();
                    long firstTime = buf.getLong(position);
                    if (pair.lhs < firstTime) {
                        buf.putLong(position, pair.lhs);
                        buf.putDouble(position + Longs.BYTES, pair.rhs);
                    }
                }

                @Override
                public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                    inspector.visit("selector", selector);
                }
            };
        }
    };
}
Also used : SerializablePair(io.druid.collections.SerializablePair) ColumnSelectorFactory(io.druid.segment.ColumnSelectorFactory) RuntimeShapeInspector(io.druid.query.monomorphicprocessing.RuntimeShapeInspector) ByteBuffer(java.nio.ByteBuffer) ObjectColumnSelector(io.druid.segment.ObjectColumnSelector)

Aggregations

RuntimeShapeInspector (io.druid.query.monomorphicprocessing.RuntimeShapeInspector)8 ColumnSelectorFactory (io.druid.segment.ColumnSelectorFactory)5 ObjectColumnSelector (io.druid.segment.ObjectColumnSelector)5 SerializablePair (io.druid.collections.SerializablePair)4 ExtractionFn (io.druid.query.extraction.ExtractionFn)4 ValueMatcher (io.druid.query.filter.ValueMatcher)4 ByteBuffer (java.nio.ByteBuffer)4 DimensionSelector (io.druid.segment.DimensionSelector)3 Predicate (com.google.common.base.Predicate)2 DimensionSpec (io.druid.query.dimension.DimensionSpec)2 BooleanValueMatcher (io.druid.segment.filter.BooleanValueMatcher)2 Nullable (javax.annotation.Nullable)2 JavaScriptExtractionFn (io.druid.query.extraction.JavaScriptExtractionFn)1 Cursor (io.druid.segment.Cursor)1 DimensionIndexer (io.druid.segment.DimensionIndexer)1 FloatColumnSelector (io.druid.segment.FloatColumnSelector)1 FloatWrappingDimensionSelector (io.druid.segment.FloatWrappingDimensionSelector)1 IdLookup (io.druid.segment.IdLookup)1 LongColumnSelector (io.druid.segment.LongColumnSelector)1 LongWrappingDimensionSelector (io.druid.segment.LongWrappingDimensionSelector)1