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();
}
};
}
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);
}
}
}
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);
}
}
}
}
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]);
}
}
}
}
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);
}
}
Aggregations