use of com.facebook.presto.common.block.ArrayBlockBuilder in project presto by prestodb.
the class TestColumnarArray method createBlockBuilderWithValues.
public static BlockBuilder createBlockBuilderWithValues(Slice[][] expectedValues) {
BlockBuilder blockBuilder = new ArrayBlockBuilder(VARCHAR, null, 100, 100);
for (Slice[] expectedValue : expectedValues) {
if (expectedValue == null) {
blockBuilder.appendNull();
} else {
BlockBuilder elementBlockBuilder = VARCHAR.createBlockBuilder(null, expectedValue.length);
for (Slice v : expectedValue) {
if (v == null) {
elementBlockBuilder.appendNull();
} else {
VARCHAR.writeSlice(elementBlockBuilder, v);
}
}
blockBuilder.appendStructure(elementBlockBuilder.build());
}
}
return blockBuilder;
}
use of com.facebook.presto.common.block.ArrayBlockBuilder in project presto by prestodb.
the class TestArrayBlock method testSingleValueBlock.
@Test
public void testSingleValueBlock() {
// 1 entry array.
long[][] values = createTestArray(50);
BlockBuilder arrayBlockBuilder = createBlockBuilderWithValues(values);
Block arrayBlock = arrayBlockBuilder.build();
assertSame(arrayBlock, arrayBlock.getSingleValueBlock(0));
assertNotSame(arrayBlockBuilder, arrayBlockBuilder.getSingleValueBlock(0));
// 2 entries array.
values = createTestArray(50, 50);
arrayBlockBuilder = createBlockBuilderWithValues(values);
arrayBlock = arrayBlockBuilder.build();
Block firstElement = arrayBlock.getRegion(0, 1);
assertNotSame(firstElement, firstElement.getSingleValueBlock(0));
Block secondElementCopy = arrayBlock.copyRegion(1, 1);
assertSame(secondElementCopy, secondElementCopy.getSingleValueBlock(0));
// Test with null elements.
values = new long[][] { null };
arrayBlockBuilder = new ArrayBlockBuilder(BIGINT, null, 1, 100);
writeValues(values, arrayBlockBuilder);
arrayBlock = arrayBlockBuilder.build();
assertSame(arrayBlock, arrayBlock.getSingleValueBlock(0));
assertNotSame(arrayBlock, arrayBlockBuilder.getSingleValueBlock(0));
// Test with 2 null elements.
values = new long[][] { null, null };
arrayBlockBuilder = createBlockBuilderWithValues(values);
arrayBlock = arrayBlockBuilder.build();
assertNotSame(arrayBlock, arrayBlock.getSingleValueBlock(0));
}
use of com.facebook.presto.common.block.ArrayBlockBuilder in project presto by prestodb.
the class TestArrayBlock method testLazyBlockBuilderInitialization.
@Test
public void testLazyBlockBuilderInitialization() {
long[][] expectedValues = new long[ARRAY_SIZES.length][];
Random rand = new Random(47);
for (int i = 0; i < ARRAY_SIZES.length; i++) {
expectedValues[i] = rand.longs(ARRAY_SIZES[i]).toArray();
}
BlockBuilder emptyBlockBuilder = new ArrayBlockBuilder(BIGINT, null, 0, 0);
BlockBuilder blockBuilder = new ArrayBlockBuilder(BIGINT, null, 100, 100);
assertEquals(blockBuilder.getSizeInBytes(), emptyBlockBuilder.getSizeInBytes());
assertEquals(blockBuilder.getRetainedSizeInBytes(), emptyBlockBuilder.getRetainedSizeInBytes());
writeValues(expectedValues, blockBuilder);
assertTrue(blockBuilder.getSizeInBytes() > emptyBlockBuilder.getSizeInBytes());
assertTrue(blockBuilder.getRetainedSizeInBytes() > emptyBlockBuilder.getRetainedSizeInBytes());
blockBuilder = blockBuilder.newBlockBuilderLike(null);
assertEquals(blockBuilder.getSizeInBytes(), emptyBlockBuilder.getSizeInBytes());
assertEquals(blockBuilder.getRetainedSizeInBytes(), emptyBlockBuilder.getRetainedSizeInBytes());
}
use of com.facebook.presto.common.block.ArrayBlockBuilder in project presto by prestodb.
the class TestDirectEntryBlockBuilder method testNestedMap.
@Test
public void testNestedMap() {
MapType innerMap = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
ArrayType arrayType = new ArrayType(innerMap);
MapType mapType = new MapType(BIGINT, arrayType, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
MapBlockBuilder beginEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
for (int i = 0; i < POSITION_COUNT; i++) {
BlockBuilder mapWriter = beginEntryBlockBuilder.beginBlockEntry();
for (int j = 0; j < 7; j++) {
// Key
BIGINT.writeLong(mapWriter, i * 7 + j);
BlockBuilder arrayWriter = mapWriter.beginBlockEntry();
// Map contains 4 elements.
for (int k = 0; k < 10; k++) {
BlockBuilder innerMapWriter = arrayWriter.beginBlockEntry();
for (int l = 0; l < 3; l++) {
BIGINT.writeLong(innerMapWriter, k * 10 + l);
VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value" + l));
}
arrayWriter.closeEntry();
arrayWriter.appendNull();
}
mapWriter.closeEntry();
}
beginEntryBlockBuilder.closeEntry();
}
MapBlockBuilder directEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
for (int i = 0; i < POSITION_COUNT; i++) {
directEntryBlockBuilder.beginDirectEntry();
BlockBuilder keyBuilder = directEntryBlockBuilder.getKeyBlockBuilder();
ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getValueBlockBuilder();
MapBlockBuilder innerMapBuilder = (MapBlockBuilder) arrayBuilder.getElementBlockBuilder();
BlockBuilder innerMapKeyBuilder = innerMapBuilder.getKeyBlockBuilder();
BlockBuilder innerMapValueBuilder = innerMapBuilder.getValueBlockBuilder();
for (int j = 0; j < 7; j++) {
// Key
BIGINT.writeLong(keyBuilder, i * 7 + j);
arrayBuilder.beginDirectEntry();
// Map contains 4 elements.
for (int k = 0; k < 10; k++) {
innerMapBuilder.beginDirectEntry();
for (int l = 0; l < 3; l++) {
BIGINT.writeLong(innerMapKeyBuilder, k * 10 + l);
VARCHAR.writeSlice(innerMapValueBuilder, utf8Slice("Value" + l));
}
innerMapBuilder.closeEntry();
innerMapBuilder.appendNull();
}
arrayBuilder.closeEntry();
}
directEntryBlockBuilder.closeEntry();
}
Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Aggregations