Search in sources :

Example 26 with InterleavedBlockBuilder

use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.

the class TestInterleavedBlock method createBlockBuilderWithValues.

private static InterleavedBlockBuilder createBlockBuilderWithValues(Slice[] expectedValues) {
    InterleavedBlockBuilder blockBuilder = new InterleavedBlockBuilder(TYPES, new BlockBuilderStatus(), expectedValues.length);
    int valueIndex = 0;
    while (valueIndex < expectedValues.length) {
        for (Type type : TYPES) {
            Class<?> javaType = type.getJavaType();
            Slice expectedValue = expectedValues[valueIndex];
            if (expectedValue == null) {
                blockBuilder.appendNull();
            } else if (javaType == boolean.class) {
                type.writeBoolean(blockBuilder, expectedValue.getByte(0) != 0);
            } else if (javaType == long.class) {
                type.writeLong(blockBuilder, expectedValue.getLong(0));
            } else if (javaType == double.class) {
                type.writeDouble(blockBuilder, expectedValue.getDouble(0));
            } else {
                blockBuilder.writeBytes(expectedValue, 0, expectedValue.length()).closeEntry();
            }
            valueIndex++;
        }
    }
    return blockBuilder;
}
Also used : VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) Slice(io.airlift.slice.Slice) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 27 with InterleavedBlockBuilder

use of com.facebook.presto.spi.block.InterleavedBlockBuilder 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)

Example 28 with InterleavedBlockBuilder

use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.

the class TypedHistogram method serialize.

public Block serialize() {
    Block valuesBlock = values.build();
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(type, BIGINT), new BlockBuilderStatus(), valuesBlock.getPositionCount() * 2);
    for (int i = 0; i < valuesBlock.getPositionCount(); i++) {
        type.appendTo(valuesBlock, i, blockBuilder);
        BIGINT.writeLong(blockBuilder, counts.get(i));
    }
    return blockBuilder.build();
}
Also used : Block(com.facebook.presto.spi.block.Block) 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

InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)28 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)26 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)23 Block (com.facebook.presto.spi.block.Block)11 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)9 Type (com.facebook.presto.spi.type.Type)8 Test (org.testng.annotations.Test)8 MapType (com.facebook.presto.type.MapType)5 RowType (com.facebook.presto.type.RowType)5 PrestoException (com.facebook.presto.spi.PrestoException)4 ArrayType (com.facebook.presto.type.ArrayType)4 VarcharType (com.facebook.presto.spi.type.VarcharType)3 VarcharType.createUnboundedVarcharType (com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType)3 Map (java.util.Map)3 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)2 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)2 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)2 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)2 BigintType (com.facebook.presto.spi.type.BigintType)2 BooleanType (com.facebook.presto.spi.type.BooleanType)2