Search in sources :

Example 1 with ObjectBigArray

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();
    }
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) TypedSet(com.facebook.presto.operator.aggregation.TypedSet) ObjectBigArray(com.facebook.presto.common.array.ObjectBigArray) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 2 with ObjectBigArray

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);
}
Also used : RowPagesBuilder(com.facebook.presto.RowPagesBuilder) Page(com.facebook.presto.common.Page) ObjectBigArray(com.facebook.presto.common.array.ObjectBigArray)

Aggregations

ObjectBigArray (com.facebook.presto.common.array.ObjectBigArray)2 RowPagesBuilder (com.facebook.presto.RowPagesBuilder)1 Page (com.facebook.presto.common.Page)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 Type (com.facebook.presto.common.type.Type)1 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)1