Search in sources :

Example 1 with ObjectBigArray

use of com.facebook.presto.array.ObjectBigArray in project presto by prestodb.

the class MultiKeyValuePairs method toMultimapNativeEncoding.

/**
     * Serialize as a multimap: map(key, array(value)), each key can be associated with multiple values
     */
public Block toMultimapNativeEncoding() {
    Block keys = keyBlockBuilder.build();
    Block values = valueBlockBuilder.build();
    // Merge values of the same key into an array
    BlockBuilder distinctKeyBlockBuilder = keyType.createBlockBuilder(new BlockBuilderStatus(), keys.getPositionCount(), expectedValueSize(keyType, EXPECTED_ENTRY_SIZE));
    ObjectBigArray<BlockBuilder> valueArrayBlockBuilders = new ObjectBigArray<>();
    valueArrayBlockBuilders.ensureCapacity(keys.getPositionCount());
    TypedSet keySet = new TypedSet(keyType, keys.getPositionCount());
    for (int keyValueIndex = 0; keyValueIndex < keys.getPositionCount(); keyValueIndex++) {
        if (!keySet.contains(keys, keyValueIndex)) {
            keySet.add(keys, keyValueIndex);
            keyType.appendTo(keys, keyValueIndex, distinctKeyBlockBuilder);
            BlockBuilder valueArrayBuilder = valueType.createBlockBuilder(new BlockBuilderStatus(), 10, expectedValueSize(valueType, EXPECTED_ENTRY_SIZE));
            valueArrayBlockBuilders.set(keySet.positionOf(keys, keyValueIndex), valueArrayBuilder);
        }
        valueType.appendTo(values, keyValueIndex, valueArrayBlockBuilders.get(keySet.positionOf(keys, keyValueIndex)));
    }
    // Write keys and value arrays into one Block
    Block distinctKeys = distinctKeyBlockBuilder.build();
    Type valueArrayType = new ArrayType(valueType);
    BlockBuilder multimapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueArrayType), new BlockBuilderStatus(), distinctKeyBlockBuilder.getPositionCount());
    for (int i = 0; i < distinctKeys.getPositionCount(); i++) {
        keyType.appendTo(distinctKeys, i, multimapBlockBuilder);
        valueArrayType.writeObject(multimapBlockBuilder, valueArrayBlockBuilders.get(i).build());
    }
    return multimapBlockBuilder.build();
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) RowType(com.facebook.presto.type.RowType) Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) ObjectBigArray(com.facebook.presto.array.ObjectBigArray) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Aggregations

ObjectBigArray (com.facebook.presto.array.ObjectBigArray)1 Block (com.facebook.presto.spi.block.Block)1 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)1 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)1 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)1 Type (com.facebook.presto.spi.type.Type)1 ArrayType (com.facebook.presto.type.ArrayType)1 RowType (com.facebook.presto.type.RowType)1