Search in sources :

Example 56 with IndexedInts

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

the class AlwaysTwoVectorizedVirtualColumn method makeMultiValueVectorDimensionSelector.

@Override
public MultiValueDimensionVectorSelector makeMultiValueVectorDimensionSelector(DimensionSpec dimensionSpec, VectorColumnSelectorFactory factory) {
    Assert.assertEquals(outputName, dimensionSpec.getOutputName());
    final IndexedInts[] rowVector = new IndexedInts[factory.getReadableVectorInspector().getMaxVectorSize()];
    Arrays.fill(rowVector, new ArrayBasedIndexedInts(new int[] { 0, 0 }));
    return new MultiValueDimensionVectorSelector() {

        private final VectorSizeInspector inspector = factory.getReadableVectorInspector();

        @Override
        public IndexedInts[] getRowVector() {
            return rowVector;
        }

        @Override
        public int getValueCardinality() {
            return dictionaryEncoded ? 1 : CARDINALITY_UNKNOWN;
        }

        @Nullable
        @Override
        public String lookupName(int id) {
            return "2";
        }

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

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

        @Override
        public int getMaxVectorSize() {
            return inspector.getMaxVectorSize();
        }

        @Override
        public int getCurrentVectorSize() {
            return inspector.getCurrentVectorSize();
        }
    };
}
Also used : MultiValueDimensionVectorSelector(org.apache.druid.segment.vector.MultiValueDimensionVectorSelector) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) IndexedInts(org.apache.druid.segment.data.IndexedInts) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) VectorSizeInspector(org.apache.druid.segment.vector.VectorSizeInspector)

Example 57 with IndexedInts

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

the class ArrayOfDoublesSketchBuildBufferAggregator method aggregate.

@Override
public void aggregate(final ByteBuffer buf, final int position) {
    for (int i = 0; i < valueSelectors.length; i++) {
        if (valueSelectors[i].isNull()) {
            return;
        } else {
            values[i] = valueSelectors[i].getDouble();
        }
    }
    // Wrapping memory and ArrayOfDoublesSketch is inexpensive compared to sketch operations.
    // Maintaining a cache of wrapped objects per buffer position like in Theta sketch aggregator
    // might might be considered, but it would increase complexity including relocate() support.
    final WritableMemory mem = WritableMemory.writableWrap(buf, ByteOrder.LITTLE_ENDIAN);
    final WritableMemory region = mem.writableRegion(position, maxIntermediateSize);
    final ArrayOfDoublesUpdatableSketch sketch = ArrayOfDoublesSketches.wrapUpdatableSketch(region);
    final IndexedInts keys = keySelector.getRow();
    if (canLookupUtf8) {
        for (int i = 0, keysSize = keys.size(); i < keysSize; i++) {
            final ByteBuffer key;
            if (canCacheById) {
                key = (ByteBuffer) stringCache.computeIfAbsent(keys.get(i), keySelector::lookupNameUtf8);
            } else {
                key = keySelector.lookupNameUtf8(keys.get(i));
            }
            if (key != null) {
                byte[] bytes = new byte[key.remaining()];
                key.mark();
                key.get(bytes);
                key.reset();
                sketch.update(bytes, values);
            }
        }
    } else {
        for (int i = 0, keysSize = keys.size(); i < keysSize; i++) {
            final String key;
            if (canCacheById) {
                key = (String) stringCache.computeIfAbsent(keys.get(i), keySelector::lookupName);
            } else {
                key = keySelector.lookupName(keys.get(i));
            }
            sketch.update(key, values);
        }
    }
}
Also used : ArrayOfDoublesUpdatableSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch) IndexedInts(org.apache.druid.segment.data.IndexedInts) WritableMemory(org.apache.datasketches.memory.WritableMemory) ByteBuffer(java.nio.ByteBuffer)

Example 58 with IndexedInts

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

the class StringAnyVectorAggregator method aggregate.

@Override
public void aggregate(ByteBuffer buf, int position, int startRow, int endRow) {
    if (buf.getInt(position) == NOT_FOUND_FLAG_VALUE && startRow < endRow) {
        if (multiValueSelector != null) {
            final IndexedInts[] rows = multiValueSelector.getRowVector();
            if (startRow < rows.length) {
                IndexedInts row = rows[startRow];
                @Nullable String foundValue = row.size() == 0 ? null : multiValueSelector.lookupName(row.get(0));
                putValue(buf, position, foundValue);
            }
        } else if (singleValueSelector != null) {
            final int[] rows = singleValueSelector.getRowVector();
            if (startRow < rows.length) {
                int row = rows[startRow];
                @Nullable String foundValue = singleValueSelector.lookupName(row);
                putValue(buf, position, foundValue);
            }
        }
    }
}
Also used : IndexedInts(org.apache.druid.segment.data.IndexedInts) Nullable(javax.annotation.Nullable)

Example 59 with IndexedInts

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

the class StringCardinalityAggregatorColumnSelectorStrategy method hashRow.

@Override
public void hashRow(DimensionSelector dimSelector, Hasher hasher) {
    final IndexedInts row = dimSelector.getRow();
    final int size = row.size();
    // nothing to add to hasher if size == 0, only handle size == 1 and size != 0 cases.
    if (size == 1) {
        final String value = dimSelector.lookupName(row.get(0));
        if (NullHandling.replaceWithDefault() || value != null) {
            hasher.putUnencodedChars(nullToSpecial(value));
        }
    } else if (size != 0) {
        boolean hasNonNullValue = false;
        final String[] values = new String[size];
        for (int i = 0; i < size; ++i) {
            final String value = dimSelector.lookupName(row.get(i));
            // A special value for null in case null handling is configured to use empty string for null.
            if (NullHandling.sqlCompatible() && !hasNonNullValue && value != null) {
                hasNonNullValue = true;
            }
            values[i] = nullToSpecial(value);
        }
        // A special value for null in case null handling is configured to use empty string for null.
        if (NullHandling.replaceWithDefault() || hasNonNullValue) {
            // Values need to be sorted to ensure consistent multi-value ordering across different segments
            Arrays.sort(values);
            for (int i = 0; i < size; ++i) {
                if (i != 0) {
                    hasher.putChar(CARDINALITY_AGG_SEPARATOR);
                }
                hasher.putUnencodedChars(values[i]);
            }
        }
    }
}
Also used : IndexedInts(org.apache.druid.segment.data.IndexedInts)

Example 60 with IndexedInts

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

the class MultiValueStringCardinalityVectorProcessor method aggregate.

@Override
public void aggregate(ByteBuffer buf, int numRows, int[] positions, @Nullable int[] rows, int positionOffset) {
    // Save position, limit and restore later instead of allocating a new ByteBuffer object
    final int oldPosition = buf.position();
    final int oldLimit = buf.limit();
    try {
        final IndexedInts[] vector = selector.getRowVector();
        for (int i = 0; i < numRows; i++) {
            final IndexedInts ids = vector[rows != null ? rows[i] : i];
            final int sz = ids.size();
            for (int j = 0; j < sz; j++) {
                final String s = selector.lookupName(ids.get(j));
                if (NullHandling.replaceWithDefault() || s != null) {
                    final int position = positions[i] + positionOffset;
                    buf.limit(position + HyperLogLogCollector.getLatestNumBytesForDenseStorage());
                    buf.position(position);
                    final HyperLogLogCollector collector = HyperLogLogCollector.makeCollector(buf);
                    StringCardinalityAggregatorColumnSelectorStrategy.addStringToCollector(collector, s);
                }
            }
        }
    } finally {
        buf.limit(oldLimit);
        buf.position(oldPosition);
    }
}
Also used : IndexedInts(org.apache.druid.segment.data.IndexedInts) HyperLogLogCollector(org.apache.druid.hll.HyperLogLogCollector)

Aggregations

IndexedInts (org.apache.druid.segment.data.IndexedInts)63 DimensionSelector (org.apache.druid.segment.DimensionSelector)22 ValueMatcher (org.apache.druid.query.filter.ValueMatcher)14 Test (org.junit.Test)13 RuntimeShapeInspector (org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)12 ArrayBasedIndexedInts (org.apache.druid.segment.data.ArrayBasedIndexedInts)12 Cursor (org.apache.druid.segment.Cursor)10 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)8 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)7 Predicate (com.google.common.base.Predicate)6 ByteBuffer (java.nio.ByteBuffer)6 Nullable (javax.annotation.Nullable)6 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)6 BooleanValueMatcher (org.apache.druid.segment.filter.BooleanValueMatcher)6 List (java.util.List)5 Predicates (com.google.common.base.Predicates)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Arrays (java.util.Arrays)4 NullHandling (org.apache.druid.common.config.NullHandling)4 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)4