use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class TestTypedKeyValueHeap method test.
private static void test(IntStream keyInputStream, Stream<String> valueInputStream, BlockComparator comparator, Iterator<String> outputIterator) {
BlockBuilder keysBlockBuilder = BIGINT.createBlockBuilder(new BlockBuilderStatus(), INPUT_SIZE);
BlockBuilder valuesBlockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), INPUT_SIZE);
keyInputStream.forEach(x -> BIGINT.writeLong(keysBlockBuilder, x));
valueInputStream.forEach(x -> VARCHAR.writeString(valuesBlockBuilder, x));
TypedKeyValueHeap heap = new TypedKeyValueHeap(comparator, BIGINT, VARCHAR, OUTPUT_SIZE);
heap.addAll(keysBlockBuilder, valuesBlockBuilder);
BlockBuilder resultBlockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), OUTPUT_SIZE);
heap.popAll(resultBlockBuilder);
Block resultBlock = resultBlockBuilder.build();
assertEquals(resultBlock.getPositionCount(), OUTPUT_SIZE);
for (int i = 0; i < OUTPUT_SIZE; i++) {
assertEquals(VARCHAR.getSlice(resultBlock, i).toStringUtf8(), outputIterator.next());
}
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class TestTypedSet method testGetElementPosition.
@Test
public void testGetElementPosition() throws Exception {
int elementCount = 100;
TypedSet typedSet = new TypedSet(BIGINT, elementCount);
BlockBuilder blockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount);
for (int i = 0; i < elementCount; i++) {
BIGINT.writeLong(blockBuilder, i);
typedSet.add(blockBuilder, i);
}
for (int j = 0; j < blockBuilder.getPositionCount(); j++) {
assertEquals(typedSet.positionOf(blockBuilder, j), j);
}
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class TestStateCompiler method testComplexSerialization.
@Test
public void testComplexSerialization() {
Type arrayType = new ArrayType(BIGINT);
Type mapType = new MapType(BIGINT, VARCHAR);
Map<String, Type> fieldMap = ImmutableMap.of("Block", arrayType, "AnotherBlock", mapType);
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
AccumulatorStateSerializer<TestComplexState> serializer = StateCompiler.generateStateSerializer(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
TestComplexState singleState = factory.createSingleState();
TestComplexState deserializedState = factory.createSingleState();
singleState.setBoolean(true);
singleState.setLong(1);
singleState.setDouble(2.0);
singleState.setByte((byte) 3);
singleState.setSlice(utf8Slice("test"));
singleState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
singleState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
singleState.setBlock(array);
BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
BIGINT.writeLong(mapBlockBuilder, 123L);
VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
Block map = mapBlockBuilder.build();
singleState.setAnotherBlock(map);
BlockBuilder builder = new RowType(ImmutableList.of(BOOLEAN, TINYINT, DOUBLE, BIGINT, mapType, VARBINARY, arrayType, VARBINARY, VARBINARY), Optional.empty()).createBlockBuilder(new BlockBuilderStatus(), 1);
serializer.serialize(singleState, builder);
Block block = builder.build();
serializer.deserialize(block, 0, deserializedState);
assertEquals(deserializedState.getBoolean(), singleState.getBoolean());
assertEquals(deserializedState.getLong(), singleState.getLong());
assertEquals(deserializedState.getDouble(), singleState.getDouble());
assertEquals(deserializedState.getByte(), singleState.getByte());
assertEquals(deserializedState.getSlice(), singleState.getSlice());
assertEquals(deserializedState.getAnotherSlice(), singleState.getAnotherSlice());
assertEquals(deserializedState.getYetAnotherSlice(), singleState.getYetAnotherSlice());
assertEquals(deserializedState.getBlock().getLong(0, 0), singleState.getBlock().getLong(0, 0));
assertEquals(deserializedState.getAnotherBlock().getLong(0, 0), singleState.getAnotherBlock().getLong(0, 0));
assertEquals(deserializedState.getAnotherBlock().getSlice(1, 0, 9), singleState.getAnotherBlock().getSlice(1, 0, 9));
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class TestStateCompiler method testVarianceStateSerialization.
@Test
public void testVarianceStateSerialization() {
AccumulatorStateFactory<VarianceState> factory = StateCompiler.generateStateFactory(VarianceState.class);
AccumulatorStateSerializer<VarianceState> serializer = StateCompiler.generateStateSerializer(VarianceState.class);
VarianceState singleState = factory.createSingleState();
VarianceState deserializedState = factory.createSingleState();
singleState.setMean(1);
singleState.setCount(2);
singleState.setM2(3);
BlockBuilder builder = new RowType(ImmutableList.of(BIGINT, DOUBLE, DOUBLE), Optional.empty()).createBlockBuilder(new BlockBuilderStatus(), 1);
serializer.serialize(singleState, builder);
Block block = builder.build();
serializer.deserialize(block, 0, deserializedState);
assertEquals(deserializedState.getCount(), singleState.getCount());
assertEquals(deserializedState.getMean(), singleState.getMean());
assertEquals(deserializedState.getM2(), singleState.getM2());
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class TestStateCompiler method testComplexStateEstimatedSize.
@Test
public void testComplexStateEstimatedSize() {
Map<String, Type> fieldMap = ImmutableMap.of("Block", new ArrayType(BIGINT), "AnotherBlock", new MapType(BIGINT, VARCHAR));
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
TestComplexState groupedState = factory.createGroupedState();
assertEquals(groupedState.getEstimatedSize(), 76064);
for (int i = 0; i < 1000; i++) {
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setSlice(utf8Slice("test"));
groupedState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
BIGINT.writeLong(mapBlockBuilder, 123L);
VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
Block map = mapBlockBuilder.build();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), 76064 + 1274 * (i + 1));
}
for (int i = 0; i < 1000; i++) {
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setSlice(utf8Slice("test"));
groupedState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
BIGINT.writeLong(mapBlockBuilder, 123L);
VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
Block map = mapBlockBuilder.build();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), 76064 + 1274 * 1000);
}
}
Aggregations