Search in sources :

Example 1 with InterleavedBlockBuilder

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

the class AccumuloRowSerializer method getBlockFromMap.

/**
     * Encodes the given map into a Block.
     *
     * @param mapType Presto type of the map
     * @param map Map of key/value pairs to encode
     * @return Presto Block
     */
static Block getBlockFromMap(Type mapType, Map<?, ?> map) {
    Type keyType = mapType.getTypeParameters().get(0);
    Type valueType = mapType.getTypeParameters().get(1);
    BlockBuilder builder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), map.size() * 2);
    for (Entry<?, ?> entry : map.entrySet()) {
        writeObject(builder, keyType, entry.getKey());
        writeObject(builder, valueType, entry.getValue());
    }
    return builder.build();
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) 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 2 with InterleavedBlockBuilder

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

the class TestSerDeUtils method testMapBlock.

@Test
public void testMapBlock() {
    MapHolder holder = new MapHolder();
    holder.map = new TreeMap<>();
    holder.map.put("twelve", new InnerStruct(13, 14L));
    holder.map.put("fifteen", new InnerStruct(16, 17L));
    com.facebook.presto.spi.type.Type rowType = new RowType(ImmutableList.of(INTEGER, BIGINT), Optional.empty());
    com.facebook.presto.spi.type.Type mapOfVarcharRowType = new RowType(ImmutableList.of(new MapType(createUnboundedVarcharType(), rowType)), Optional.empty());
    Block actual = toBinaryBlock(mapOfVarcharRowType, holder, getInspector(MapHolder.class));
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(createUnboundedVarcharType(), rowType), new BlockBuilderStatus(), 1024);
    createUnboundedVarcharType().writeString(blockBuilder, "fifteen");
    rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 16, 17L));
    createUnboundedVarcharType().writeString(blockBuilder, "twelve");
    rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 13, 14L));
    Block expected = rowBlockOf(ImmutableList.of(new MapType(createUnboundedVarcharType(), rowType)), blockBuilder);
    assertBlockEquals(actual, expected);
}
Also used : RowType(com.facebook.presto.type.RowType) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) MapType(com.facebook.presto.type.MapType) Block(com.facebook.presto.spi.block.Block) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 3 with InterleavedBlockBuilder

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

the class TestSerDeUtils method testStructBlock.

@Test
public void testStructBlock() {
    // test simple structs
    InnerStruct innerStruct = new InnerStruct(13, 14L);
    com.facebook.presto.spi.type.Type rowType = new RowType(ImmutableList.of(INTEGER, BIGINT), Optional.empty());
    Block actual = toBinaryBlock(rowType, innerStruct, getInspector(InnerStruct.class));
    Block expected = rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 13, 14L);
    assertBlockEquals(actual, expected);
    // test complex structs
    OuterStruct outerStruct = new OuterStruct();
    outerStruct.byteVal = (byte) 1;
    outerStruct.shortVal = (short) 2;
    outerStruct.intVal = 3;
    outerStruct.longVal = 4L;
    outerStruct.floatVal = 5.01f;
    outerStruct.doubleVal = 6.001d;
    outerStruct.stringVal = "seven";
    outerStruct.byteArray = new byte[] { '2' };
    InnerStruct is1 = new InnerStruct(2, -5L);
    InnerStruct is2 = new InnerStruct(-10, 0L);
    outerStruct.structArray = new ArrayList<>(2);
    outerStruct.structArray.add(is1);
    outerStruct.structArray.add(is2);
    outerStruct.map = new TreeMap<>();
    outerStruct.map.put("twelve", new InnerStruct(0, 5L));
    outerStruct.map.put("fifteen", new InnerStruct(-5, -10L));
    outerStruct.innerStruct = new InnerStruct(18, 19L);
    com.facebook.presto.spi.type.Type innerRowType = new RowType(ImmutableList.of(INTEGER, BIGINT), Optional.empty());
    com.facebook.presto.spi.type.Type arrayOfInnerRowType = new ArrayType(innerRowType);
    com.facebook.presto.spi.type.Type mapOfInnerRowType = new MapType(createUnboundedVarcharType(), innerRowType);
    List<com.facebook.presto.spi.type.Type> outerRowParameterTypes = ImmutableList.of(TINYINT, SMALLINT, INTEGER, BIGINT, REAL, DOUBLE, createUnboundedVarcharType(), createUnboundedVarcharType(), arrayOfInnerRowType, mapOfInnerRowType, innerRowType);
    com.facebook.presto.spi.type.Type outerRowType = new RowType(outerRowParameterTypes, Optional.empty());
    actual = toBinaryBlock(outerRowType, outerStruct, getInspector(OuterStruct.class));
    ImmutableList.Builder<Object> outerRowValues = ImmutableList.builder();
    outerRowValues.add((byte) 1);
    outerRowValues.add((short) 2);
    outerRowValues.add(3);
    outerRowValues.add(4L);
    outerRowValues.add(5.01f);
    outerRowValues.add(6.001d);
    outerRowValues.add("seven");
    outerRowValues.add(new byte[] { '2' });
    outerRowValues.add(arrayBlockOf(innerRowType, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 2, -5L), rowBlockOf(ImmutableList.of(INTEGER, BIGINT), -10, 0L)));
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(createUnboundedVarcharType(), innerRowType), new BlockBuilderStatus(), 1024);
    createUnboundedVarcharType().writeString(blockBuilder, "fifteen");
    innerRowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), -5, -10L));
    createUnboundedVarcharType().writeString(blockBuilder, "twelve");
    innerRowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 0, 5L));
    outerRowValues.add(blockBuilder.build());
    outerRowValues.add(rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 18, 19L));
    assertBlockEquals(actual, rowBlockOf(outerRowParameterTypes, outerRowValues.build().toArray()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) RowType(com.facebook.presto.type.RowType) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) MapType(com.facebook.presto.type.MapType) ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) RowType(com.facebook.presto.type.RowType) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) Type(java.lang.reflect.Type) Block(com.facebook.presto.spi.block.Block) SerDeUtils.getBlockObject(com.facebook.presto.hive.util.SerDeUtils.getBlockObject) SerDeUtils.serializeObject(com.facebook.presto.hive.util.SerDeUtils.serializeObject) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 4 with InterleavedBlockBuilder

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

the class TestMapOperators method assertMapHashOperator.

private void assertMapHashOperator(String inputString, Type keyType, Type valueType, List<Object> elements) {
    checkArgument(elements.size() % 2 == 0, "the size of elements should be even number");
    MapType mapType = new MapType(keyType, valueType);
    BlockBuilder mapArrayBuilder = mapType.createBlockBuilder(new BlockBuilderStatus(), 1);
    BlockBuilder mapBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), elements.size());
    for (int i = 0; i < elements.size(); i += 2) {
        appendToBlockBuilder(keyType, elements.get(i), mapBuilder);
        appendToBlockBuilder(valueType, elements.get(i + 1), mapBuilder);
    }
    mapType.writeObject(mapArrayBuilder, mapBuilder.build());
    long hashResult = mapType.hash(mapArrayBuilder.build(), 0);
    assertOperator(HASH_CODE, inputString, BIGINT, hashResult);
}
Also used : InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) TypeJsonUtils.appendToBlockBuilder(com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 5 with InterleavedBlockBuilder

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

the class TestRowOperators method assertRowHashOperator.

private void assertRowHashOperator(String inputString, List<Type> types, List<Object> elements) {
    checkArgument(types.size() == elements.size(), "types and elements must have the same size");
    RowType rowType = new RowType(types, Optional.empty());
    BlockBuilder rowArrayBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), 1);
    BlockBuilder rowBuilder = new InterleavedBlockBuilder(types, new BlockBuilderStatus(), types.size());
    for (int i = 0; i < types.size(); i++) {
        appendToBlockBuilder(types.get(i), elements.get(i), rowBuilder);
    }
    rowType.writeObject(rowArrayBuilder, rowBuilder.build());
    assertOperator(HASH_CODE, inputString, BIGINT, rowType.hash(rowArrayBuilder.build(), 0));
}
Also used : InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) TypeJsonUtils.appendToBlockBuilder(com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder) 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