use of io.trino.array.ObjectBigArray in project trino by trinodb.
the class MultimapAggregationFunction method output.
public static void output(Type keyType, BlockPositionEqual keyEqual, BlockPositionHashCode keyHashCode, Type valueType, MultimapAggregationState state, BlockBuilder out) {
if (state.isEmpty()) {
out.appendNull();
} else {
// TODO: Avoid copy value block associated with the same key by using strategy similar to multimap_from_entries
ObjectBigArray<BlockBuilder> valueArrayBlockBuilders = new ObjectBigArray<>();
valueArrayBlockBuilders.ensureCapacity(state.getEntryCount());
BlockBuilder distinctKeyBlockBuilder = keyType.createBlockBuilder(null, state.getEntryCount(), expectedValueSize(keyType, 100));
TypedSet keySet = createEqualityTypedSet(keyType, keyEqual, keyHashCode, state.getEntryCount(), NAME);
state.forEach((key, value, keyValueIndex) -> {
// Merge values of the same key into an array
if (keySet.add(key, keyValueIndex)) {
keyType.appendTo(key, keyValueIndex, distinctKeyBlockBuilder);
BlockBuilder valueArrayBuilder = valueType.createBlockBuilder(null, 10, expectedValueSize(valueType, EXPECTED_ENTRY_SIZE));
valueArrayBlockBuilders.set(keySet.positionOf(key, keyValueIndex), valueArrayBuilder);
}
valueType.appendTo(value, keyValueIndex, valueArrayBlockBuilders.get(keySet.positionOf(key, keyValueIndex)));
});
// Write keys and value arrays into one Block
Type valueArrayType = new ArrayType(valueType);
BlockBuilder multimapBlockBuilder = out.beginBlockEntry();
for (int i = 0; i < distinctKeyBlockBuilder.getPositionCount(); i++) {
keyType.appendTo(distinctKeyBlockBuilder, i, multimapBlockBuilder);
valueArrayType.writeObject(multimapBlockBuilder, valueArrayBlockBuilders.get(i).build());
}
out.closeEntry();
}
}
Aggregations