use of com.facebook.presto.common.array.ObjectBigArray in project presto by prestodb.
the class MultimapAggregationFunction method output.
public static void output(Type keyType, 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 = new TypedSet(keyType, state.getEntryCount(), MultimapAggregationFunction.NAME);
state.forEach((key, value, keyValueIndex) -> {
// Merge values of the same key into an array
if (!keySet.contains(key, keyValueIndex)) {
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();
}
}
use of com.facebook.presto.common.array.ObjectBigArray in project presto by prestodb.
the class TestGroupedTopNBuilder method assertBuilderSize.
/**
* Assert the retained size in Bytes of {@param builder} with
* {@param groupByHash},
* a list of {@param types} of the input pages,
* a list of how many positions ({@param pagePositions}) of each page, and
* a list of how many rows ({}@param rowCounts}) of each group.
* Currently we do not assert the size of emptyPageReferenceSlots and assume the queue is always with INITIAL_CAPACITY = 4.
*/
private static void assertBuilderSize(GroupByHash groupByHash, List<Type> types, List<Integer> pagePositions, List<Integer> rowCounts, long actualSizeInBytes) {
ObjectBigArray<Object> pageReferences = new ObjectBigArray<>();
pageReferences.ensureCapacity(pagePositions.size());
long pageReferencesSizeInBytes = pageReferences.sizeOf();
ObjectBigArray<Object> groupedRows = new ObjectBigArray<>();
groupedRows.ensureCapacity(rowCounts.size());
long groupedRowsSizeInBytes = groupedRows.sizeOf();
int emptySlots = 4;
long emptyPageReferenceSlotsSizeInBytes = INT_FIFO_QUEUE_SIZE + sizeOf(new int[emptySlots]);
// build fake pages to get the real retained sizes
RowPagesBuilder rowPagesBuilder = rowPagesBuilder(types);
for (int pagePosition : pagePositions) {
if (pagePosition > 0) {
rowPagesBuilder.addSequencePage(pagePosition, new int[types.size()]);
}
}
long referencedPagesSizeInBytes = 0;
for (Page page : rowPagesBuilder.build()) {
// each page reference is with two arrays and a page
referencedPagesSizeInBytes += PAGE_REFERENCE_INSTANCE_SIZE + page.getRetainedSizeInBytes() + sizeOf(new Object[page.getPositionCount()]);
}
long rowHeapsSizeInBytes = 0;
for (int count : rowCounts) {
if (count > 0) {
rowHeapsSizeInBytes += new TestRowHeap(count).getEstimatedSizeInBytes();
}
}
long expectedSizeInBytes = INSTANCE_SIZE + groupByHash.getEstimatedSize() + referencedPagesSizeInBytes + rowHeapsSizeInBytes + pageReferencesSizeInBytes + groupedRowsSizeInBytes + emptyPageReferenceSlotsSizeInBytes;
assertEquals(actualSizeInBytes, expectedSizeInBytes);
}
Aggregations