use of com.facebook.presto.spi.block.BlockBuilderStatus 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.BlockBuilderStatus in project presto by prestodb.
the class ParquetColumnReader method readPrimitive.
public Block readPrimitive(Type type, IntList positions) throws IOException {
seek();
BlockBuilder blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), nextBatchSize);
int valueCount = 0;
while (valueCount < nextBatchSize) {
if (page == null) {
readNextPage();
}
int numValues = Math.min(remainingValueCountInPage, nextBatchSize - valueCount);
readValues(blockBuilder, numValues, type, positions);
valueCount += numValues;
updatePosition(numValues);
}
checkArgument(valueCount == nextBatchSize, "valueCount " + valueCount + " not equals to batchSize " + nextBatchSize);
readOffset = 0;
nextBatchSize = 0;
return blockBuilder.build();
}
use of com.facebook.presto.spi.block.BlockBuilderStatus 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.BlockBuilderStatus in project presto by prestodb.
the class TestSerDeUtils method getPrimitiveBlock.
private static Block getPrimitiveBlock(com.facebook.presto.spi.type.Type type, Object object, ObjectInspector inspector) {
BlockBuilder builder = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1);
serializeObject(type, builder, object, inspector);
return builder.build();
}
use of com.facebook.presto.spi.block.BlockBuilderStatus 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()));
}
Aggregations