Search in sources :

Example 1 with ObjectBigArray

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();
    }
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) TypeSignature.arrayType(io.trino.spi.type.TypeSignature.arrayType) ArrayType(io.trino.spi.type.ArrayType) TypeSignature.rowType(io.trino.spi.type.TypeSignature.rowType) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) TypedSet(io.trino.operator.aggregation.TypedSet) TypedSet.createEqualityTypedSet(io.trino.operator.aggregation.TypedSet.createEqualityTypedSet) ObjectBigArray(io.trino.array.ObjectBigArray) BlockBuilder(io.trino.spi.block.BlockBuilder)

Aggregations

ObjectBigArray (io.trino.array.ObjectBigArray)1 TypedSet (io.trino.operator.aggregation.TypedSet)1 TypedSet.createEqualityTypedSet (io.trino.operator.aggregation.TypedSet.createEqualityTypedSet)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 ArrayType (io.trino.spi.type.ArrayType)1 Type (io.trino.spi.type.Type)1 TypeSignature.arrayType (io.trino.spi.type.TypeSignature.arrayType)1 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)1 TypeSignature.rowType (io.trino.spi.type.TypeSignature.rowType)1