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();
}
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);
}
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()));
}
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);
}
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));
}
Aggregations