Search in sources :

Example 1 with RuntimeShapeInspector

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

the class LongFirstAggregatorFactory method getCombiningFactory.

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

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

                @Override
                public void aggregate() {
                    SerializablePair<Long, Long> pair = (SerializablePair<Long, Long>) 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 LongFirstBufferAggregator(null, null) {

                @Override
                public void aggregate(ByteBuffer buf, int position) {
                    SerializablePair<Long, Long> pair = (SerializablePair<Long, Long>) selector.get();
                    long firstTime = buf.getLong(position);
                    if (pair.lhs < firstTime) {
                        buf.putLong(position, pair.lhs);
                        buf.putLong(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)

Example 2 with RuntimeShapeInspector

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

the class DoubleLastAggregatorFactory method getCombiningFactory.

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

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

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

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

                @Override
                public void aggregate(ByteBuffer buf, int position) {
                    SerializablePair<Long, Double> pair = (SerializablePair<Long, Double>) selector.get();
                    long lastTime = buf.getLong(position);
                    if (pair.lhs >= lastTime) {
                        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)

Example 3 with RuntimeShapeInspector

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

the class LongLastAggregatorFactory method getCombiningFactory.

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

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

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

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

                @Override
                public void aggregate(ByteBuffer buf, int position) {
                    SerializablePair<Long, Long> pair = (SerializablePair<Long, Long>) selector.get();
                    long lastTime = buf.getLong(position);
                    if (pair.lhs >= lastTime) {
                        buf.putLong(position, pair.lhs);
                        buf.putLong(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)

Example 4 with RuntimeShapeInspector

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

the class StringDimensionIndexer method makeDimensionSelector.

@Override
public DimensionSelector makeDimensionSelector(final DimensionSpec spec, final IncrementalIndexStorageAdapter.EntryHolder currEntry, final IncrementalIndex.DimensionDesc desc) {
    final ExtractionFn extractionFn = spec.getExtractionFn();
    final int dimIndex = desc.getIndex();
    final int maxId = getCardinality();
    class IndexerDimensionSelector implements DimensionSelector, IdLookup {

        @Override
        public IndexedInts getRow() {
            final Object[] dims = currEntry.getKey().getDims();
            int[] indices;
            if (dimIndex < dims.length) {
                indices = (int[]) dims[dimIndex];
            } else {
                indices = null;
            }
            int[] row = null;
            int rowSize = 0;
            if (indices == null || indices.length == 0) {
                final int nullId = getEncodedValue(null, false);
                if (nullId > -1) {
                    if (nullId < maxId) {
                        row = new int[] { nullId };
                        rowSize = 1;
                    } else {
                        // Choose to use ArrayBasedIndexedInts later, instead of EmptyIndexedInts, for monomorphism
                        row = IntArrays.EMPTY_ARRAY;
                        rowSize = 0;
                    }
                }
            }
            if (row == null && indices != null && indices.length > 0) {
                row = new int[indices.length];
                for (int id : indices) {
                    if (id < maxId) {
                        row[rowSize++] = id;
                    }
                }
            }
            return ArrayBasedIndexedInts.of(row, rowSize);
        }

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

                        @Override
                        public boolean matches() {
                            Object[] dims = currEntry.getKey().getDims();
                            if (dimIndex >= dims.length) {
                                return value == null;
                            }
                            int[] dimsInt = (int[]) dims[dimIndex];
                            if (dimsInt == null || dimsInt.length == 0) {
                                return value == null;
                            }
                            for (int id : dimsInt) {
                                if (id == valueId) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                } else {
                    return BooleanValueMatcher.of(false);
                }
            } else {
                // Employ precomputed BitSet optimization
                return makeValueMatcher(Predicates.equalTo(value));
            }
        }

        @Override
        public ValueMatcher makeValueMatcher(final Predicate<String> predicate) {
            final BitSet predicateMatchingValueIds = DimensionSelectorUtils.makePredicateMatchingSet(this, predicate);
            final boolean matchNull = predicate.apply(null);
            return new ValueMatcher() {

                @Override
                public boolean matches() {
                    Object[] dims = currEntry.getKey().getDims();
                    if (dimIndex >= dims.length) {
                        return matchNull;
                    }
                    int[] dimsInt = (int[]) dims[dimIndex];
                    if (dimsInt == null || dimsInt.length == 0) {
                        return matchNull;
                    }
                    for (int id : dimsInt) {
                        if (predicateMatchingValueIds.get(id)) {
                            return true;
                        }
                    }
                    return false;
                }
            };
        }

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

        @Override
        public String lookupName(int id) {
            final String strValue = getActualValue(id, false);
            return extractionFn == null ? strValue : extractionFn.apply(strValue);
        }

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

        @Nullable
        @Override
        public IdLookup idLookup() {
            return extractionFn == null ? this : null;
        }

        @Override
        public int lookupId(String name) {
            if (extractionFn != null) {
                throw new UnsupportedOperationException("cannot perform lookup when applying an extraction function");
            }
            return getEncodedValue(name, false);
        }

        @Override
        public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
            inspector.visit("currEntry", currEntry);
        }
    }
    return new IndexerDimensionSelector();
}
Also used : BooleanValueMatcher(io.druid.segment.filter.BooleanValueMatcher) ValueMatcher(io.druid.query.filter.ValueMatcher) BitSet(java.util.BitSet) RuntimeShapeInspector(io.druid.query.monomorphicprocessing.RuntimeShapeInspector) Predicate(com.google.common.base.Predicate) ExtractionFn(io.druid.query.extraction.ExtractionFn)

Example 5 with RuntimeShapeInspector

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

the class IncrementalIndexStorageAdapter method makeCursors.

@Override
public Sequence<Cursor> makeCursors(final Filter filter, final Interval interval, final VirtualColumns virtualColumns, final Granularity gran, final boolean descending) {
    if (index.isEmpty()) {
        return Sequences.empty();
    }
    Interval actualIntervalTmp = interval;
    final Interval dataInterval = new Interval(getMinTime().getMillis(), gran.bucketEnd(getMaxTime()).getMillis());
    if (!actualIntervalTmp.overlaps(dataInterval)) {
        return Sequences.empty();
    }
    if (actualIntervalTmp.getStart().isBefore(dataInterval.getStart())) {
        actualIntervalTmp = actualIntervalTmp.withStart(dataInterval.getStart());
    }
    if (actualIntervalTmp.getEnd().isAfter(dataInterval.getEnd())) {
        actualIntervalTmp = actualIntervalTmp.withEnd(dataInterval.getEnd());
    }
    final Interval actualInterval = actualIntervalTmp;
    Iterable<Interval> iterable = gran.getIterable(actualInterval);
    if (descending) {
        iterable = Lists.reverse(ImmutableList.copyOf(iterable));
    }
    return Sequences.map(Sequences.simple(iterable), new Function<Interval, Cursor>() {

        EntryHolder currEntry = new EntryHolder();

        @Override
        public Cursor apply(@Nullable final Interval interval) {
            final long timeStart = Math.max(interval.getStartMillis(), actualInterval.getStartMillis());
            return new Cursor() {

                private final ValueMatcher filterMatcher = makeFilterMatcher(filter, this);

                private Iterator<Map.Entry<IncrementalIndex.TimeAndDims, Integer>> baseIter;

                private Iterable<Map.Entry<IncrementalIndex.TimeAndDims, Integer>> cursorIterable;

                private boolean emptyRange;

                final DateTime time;

                int numAdvanced = -1;

                boolean done;

                {
                    cursorIterable = index.getFacts().timeRangeIterable(descending, timeStart, Math.min(actualInterval.getEndMillis(), gran.increment(interval.getStart()).getMillis()));
                    emptyRange = !cursorIterable.iterator().hasNext();
                    time = gran.toDateTime(interval.getStartMillis());
                    reset();
                }

                @Override
                public DateTime getTime() {
                    return time;
                }

                @Override
                public void advance() {
                    if (!baseIter.hasNext()) {
                        done = true;
                        return;
                    }
                    while (baseIter.hasNext()) {
                        BaseQuery.checkInterrupted();
                        currEntry.set(baseIter.next());
                        if (filterMatcher.matches()) {
                            return;
                        }
                    }
                    if (!filterMatcher.matches()) {
                        done = true;
                    }
                }

                @Override
                public void advanceUninterruptibly() {
                    if (!baseIter.hasNext()) {
                        done = true;
                        return;
                    }
                    while (baseIter.hasNext()) {
                        if (Thread.currentThread().isInterrupted()) {
                            return;
                        }
                        currEntry.set(baseIter.next());
                        if (filterMatcher.matches()) {
                            return;
                        }
                    }
                    if (!filterMatcher.matches()) {
                        done = true;
                    }
                }

                @Override
                public void advanceTo(int offset) {
                    int count = 0;
                    while (count < offset && !isDone()) {
                        advance();
                        count++;
                    }
                }

                @Override
                public boolean isDone() {
                    return done;
                }

                @Override
                public boolean isDoneOrInterrupted() {
                    return isDone() || Thread.currentThread().isInterrupted();
                }

                @Override
                public void reset() {
                    baseIter = cursorIterable.iterator();
                    if (numAdvanced == -1) {
                        numAdvanced = 0;
                    } else {
                        Iterators.advance(baseIter, numAdvanced);
                    }
                    BaseQuery.checkInterrupted();
                    boolean foundMatched = false;
                    while (baseIter.hasNext()) {
                        currEntry.set(baseIter.next());
                        if (filterMatcher.matches()) {
                            foundMatched = true;
                            break;
                        }
                        numAdvanced++;
                    }
                    done = !foundMatched && (emptyRange || !baseIter.hasNext());
                }

                @Override
                public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec) {
                    if (virtualColumns.exists(dimensionSpec.getDimension())) {
                        return virtualColumns.makeDimensionSelector(dimensionSpec, this);
                    }
                    return dimensionSpec.decorate(makeDimensionSelectorUndecorated(dimensionSpec));
                }

                private DimensionSelector makeDimensionSelectorUndecorated(DimensionSpec dimensionSpec) {
                    final String dimension = dimensionSpec.getDimension();
                    final ExtractionFn extractionFn = dimensionSpec.getExtractionFn();
                    if (dimension.equals(Column.TIME_COLUMN_NAME)) {
                        DimensionSelector selector = new SingleScanTimeDimSelector(makeLongColumnSelector(dimension), extractionFn, descending);
                        return selector;
                    }
                    final IncrementalIndex.DimensionDesc dimensionDesc = index.getDimension(dimensionSpec.getDimension());
                    if (dimensionDesc == null) {
                        // not a dimension, column may be a metric
                        ColumnCapabilities capabilities = getColumnCapabilities(dimension);
                        if (capabilities == null) {
                            return NullDimensionSelector.instance();
                        }
                        if (capabilities.getType() == ValueType.LONG) {
                            return new LongWrappingDimensionSelector(makeLongColumnSelector(dimension), extractionFn);
                        }
                        if (capabilities.getType() == ValueType.FLOAT) {
                            return new FloatWrappingDimensionSelector(makeFloatColumnSelector(dimension), extractionFn);
                        }
                        // if we can't wrap the base column, just return a column of all nulls
                        return NullDimensionSelector.instance();
                    } else {
                        final DimensionIndexer indexer = dimensionDesc.getIndexer();
                        return indexer.makeDimensionSelector(dimensionSpec, currEntry, dimensionDesc);
                    }
                }

                @Override
                public FloatColumnSelector makeFloatColumnSelector(String columnName) {
                    if (virtualColumns.exists(columnName)) {
                        return virtualColumns.makeFloatColumnSelector(columnName, this);
                    }
                    final Integer dimIndex = index.getDimensionIndex(columnName);
                    if (dimIndex != null) {
                        final IncrementalIndex.DimensionDesc dimensionDesc = index.getDimension(columnName);
                        final DimensionIndexer indexer = dimensionDesc.getIndexer();
                        return indexer.makeFloatColumnSelector(currEntry, dimensionDesc);
                    }
                    final Integer metricIndexInt = index.getMetricIndex(columnName);
                    if (metricIndexInt == null) {
                        return ZeroFloatColumnSelector.instance();
                    }
                    final int metricIndex = metricIndexInt;
                    return new FloatColumnSelector() {

                        @Override
                        public float get() {
                            return index.getMetricFloatValue(currEntry.getValue(), metricIndex);
                        }

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

                @Override
                public LongColumnSelector makeLongColumnSelector(String columnName) {
                    if (virtualColumns.exists(columnName)) {
                        return virtualColumns.makeLongColumnSelector(columnName, this);
                    }
                    if (columnName.equals(Column.TIME_COLUMN_NAME)) {
                        class TimeLongColumnSelector implements LongColumnSelector {

                            @Override
                            public long get() {
                                return currEntry.getKey().getTimestamp();
                            }

                            @Override
                            public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                            }
                        }
                        return new TimeLongColumnSelector();
                    }
                    final Integer dimIndex = index.getDimensionIndex(columnName);
                    if (dimIndex != null) {
                        final IncrementalIndex.DimensionDesc dimensionDesc = index.getDimension(columnName);
                        final DimensionIndexer indexer = dimensionDesc.getIndexer();
                        return indexer.makeLongColumnSelector(currEntry, dimensionDesc);
                    }
                    final Integer metricIndexInt = index.getMetricIndex(columnName);
                    if (metricIndexInt == null) {
                        return ZeroLongColumnSelector.instance();
                    }
                    final int metricIndex = metricIndexInt;
                    return new LongColumnSelector() {

                        @Override
                        public long get() {
                            return index.getMetricLongValue(currEntry.getValue(), metricIndex);
                        }

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

                @Override
                public ObjectColumnSelector makeObjectColumnSelector(String column) {
                    if (virtualColumns.exists(column)) {
                        return virtualColumns.makeObjectColumnSelector(column, this);
                    }
                    if (column.equals(Column.TIME_COLUMN_NAME)) {
                        return new ObjectColumnSelector<Long>() {

                            @Override
                            public Class classOfObject() {
                                return Long.TYPE;
                            }

                            @Override
                            public Long get() {
                                return currEntry.getKey().getTimestamp();
                            }
                        };
                    }
                    final Integer metricIndexInt = index.getMetricIndex(column);
                    if (metricIndexInt != null) {
                        final int metricIndex = metricIndexInt;
                        final Class classOfObject = index.getMetricClass(column);
                        return new ObjectColumnSelector() {

                            @Override
                            public Class classOfObject() {
                                return classOfObject;
                            }

                            @Override
                            public Object get() {
                                return index.getMetricObjectValue(currEntry.getValue(), metricIndex);
                            }
                        };
                    }
                    IncrementalIndex.DimensionDesc dimensionDesc = index.getDimension(column);
                    if (dimensionDesc == null) {
                        return null;
                    } else {
                        final int dimensionIndex = dimensionDesc.getIndex();
                        final DimensionIndexer indexer = dimensionDesc.getIndexer();
                        return new ObjectColumnSelector<Object>() {

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

                            @Override
                            public Object get() {
                                IncrementalIndex.TimeAndDims key = currEntry.getKey();
                                if (key == null) {
                                    return null;
                                }
                                Object[] dims = key.getDims();
                                if (dimensionIndex >= dims.length) {
                                    return null;
                                }
                                return indexer.convertUnsortedEncodedKeyComponentToActualArrayOrList(dims[dimensionIndex], DimensionIndexer.ARRAY);
                            }
                        };
                    }
                }

                @Nullable
                @Override
                public ColumnCapabilities getColumnCapabilities(String columnName) {
                    if (virtualColumns.exists(columnName)) {
                        return virtualColumns.getColumnCapabilities(columnName);
                    }
                    return index.getCapabilities(columnName);
                }
            };
        }
    });
}
Also used : DimensionSpec(io.druid.query.dimension.DimensionSpec) SingleScanTimeDimSelector(io.druid.segment.SingleScanTimeDimSelector) RuntimeShapeInspector(io.druid.query.monomorphicprocessing.RuntimeShapeInspector) Cursor(io.druid.segment.Cursor) DateTime(org.joda.time.DateTime) ColumnCapabilities(io.druid.segment.column.ColumnCapabilities) DimensionIndexer(io.druid.segment.DimensionIndexer) ZeroLongColumnSelector(io.druid.segment.ZeroLongColumnSelector) LongColumnSelector(io.druid.segment.LongColumnSelector) ObjectColumnSelector(io.druid.segment.ObjectColumnSelector) LongWrappingDimensionSelector(io.druid.segment.LongWrappingDimensionSelector) NullDimensionSelector(io.druid.segment.NullDimensionSelector) DimensionSelector(io.druid.segment.DimensionSelector) FloatWrappingDimensionSelector(io.druid.segment.FloatWrappingDimensionSelector) BooleanValueMatcher(io.druid.segment.filter.BooleanValueMatcher) ValueMatcher(io.druid.query.filter.ValueMatcher) ZeroFloatColumnSelector(io.druid.segment.ZeroFloatColumnSelector) FloatColumnSelector(io.druid.segment.FloatColumnSelector) LongWrappingDimensionSelector(io.druid.segment.LongWrappingDimensionSelector) FloatWrappingDimensionSelector(io.druid.segment.FloatWrappingDimensionSelector) ExtractionFn(io.druid.query.extraction.ExtractionFn) Nullable(javax.annotation.Nullable) Interval(org.joda.time.Interval)

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