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();
}
use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class TestMapOperators method testStackRepresentation.
@Test
public void testStackRepresentation() throws Exception {
Block array = arrayBlockOf(BIGINT, 1L, 2L);
Block actualBlock = mapBlockOf(DOUBLE, new ArrayType(BIGINT), ImmutableMap.of(1.0, array));
DynamicSliceOutput actualSliceOutput = new DynamicSliceOutput(100);
writeBlock(actualSliceOutput, actualBlock);
Block expectedBlock = new InterleavedBlockBuilder(ImmutableList.of(DOUBLE, new ArrayType(BIGINT)), new BlockBuilderStatus(), 3).writeLong(doubleToLongBits(1.0)).closeEntry().writeObject(BIGINT.createBlockBuilder(new BlockBuilderStatus(), 1).writeLong(1L).closeEntry().writeLong(2L).closeEntry().build()).closeEntry().build();
DynamicSliceOutput expectedSliceOutput = new DynamicSliceOutput(100);
writeBlock(expectedSliceOutput, expectedBlock);
assertEquals(actualSliceOutput.slice(), expectedSliceOutput.slice());
}
use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class TestArrayOperators method testArrayHashOperator.
@Test
public void testArrayHashOperator() {
assertArrayHashOperator("ARRAY[1, 2]", INTEGER, ImmutableList.of(1, 2));
assertArrayHashOperator("ARRAY[true, false]", BOOLEAN, ImmutableList.of(true, false));
// test with ARRAY[ MAP( ARRAY[1], ARRAY[2] ) ]
MapType mapType = new MapType(INTEGER, INTEGER);
BlockBuilder mapBuilder = new InterleavedBlockBuilder(ImmutableList.of(INTEGER, INTEGER), new BlockBuilderStatus(), 2);
INTEGER.writeLong(mapBuilder, 1);
INTEGER.writeLong(mapBuilder, 2);
assertArrayHashOperator("ARRAY[MAP(ARRAY[1], ARRAY[2])]", mapType, ImmutableList.of(mapBuilder.build()));
}
use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class TestInterleavedBlock method testGetSizeInBytes.
@Test
private void testGetSizeInBytes() {
int numEntries = 2000;
VarcharType unboundedVarcharType = createUnboundedVarcharType();
InterleavedBlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(unboundedVarcharType, BIGINT), new BlockBuilderStatus(), numEntries);
for (int i = 0; i < numEntries; i += 2) {
unboundedVarcharType.writeString(blockBuilder, String.valueOf(ThreadLocalRandom.current().nextLong()));
BIGINT.writeLong(blockBuilder, ThreadLocalRandom.current().nextLong());
}
InterleavedBlock block = blockBuilder.build();
List<Block> splitQuarter = splitBlock(block, 4);
int sizeInBytes = block.getSizeInBytes();
int quarter1size = splitQuarter.get(0).getSizeInBytes();
int quarter2size = splitQuarter.get(1).getSizeInBytes();
int quarter3size = splitQuarter.get(2).getSizeInBytes();
int quarter4size = splitQuarter.get(3).getSizeInBytes();
double expectedQuarterSizeMin = sizeInBytes * 0.2;
double expectedQuarterSizeMax = sizeInBytes * 0.3;
assertTrue(quarter1size > expectedQuarterSizeMin && quarter1size < expectedQuarterSizeMax, format("quarter1size is %s, should be between %s and %s", quarter1size, expectedQuarterSizeMin, expectedQuarterSizeMax));
assertTrue(quarter2size > expectedQuarterSizeMin && quarter2size < expectedQuarterSizeMax, format("quarter2size is %s, should be between %s and %s", quarter2size, expectedQuarterSizeMin, expectedQuarterSizeMax));
assertTrue(quarter3size > expectedQuarterSizeMin && quarter3size < expectedQuarterSizeMax, format("quarter3size is %s, should be between %s and %s", quarter3size, expectedQuarterSizeMin, expectedQuarterSizeMax));
assertTrue(quarter4size > expectedQuarterSizeMin && quarter4size < expectedQuarterSizeMax, format("quarter4size is %s, should be between %s and %s", quarter4size, expectedQuarterSizeMin, expectedQuarterSizeMax));
assertEquals(quarter1size + quarter2size + quarter3size + quarter4size, sizeInBytes);
}
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;
}
Aggregations