use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestDirectEntryBlockBuilder method testNestedRow.
@Test
public void testNestedRow() {
MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
ArrayType arrayType = new ArrayType(DOUBLE);
RowType.Field nestedRowField = new RowType.Field(Optional.of("my_struct"), INTEGER);
RowType nestedRowType = RowType.from(ImmutableList.of(nestedRowField));
List<Type> rowType = ImmutableList.of(REAL, mapType, arrayType, nestedRowType);
RowBlockBuilder beginEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
for (int i = 0; i < POSITION_COUNT; i++) {
SingleRowBlockWriter singleRowBlockWriter = beginEntryBlockBuilder.beginBlockEntry();
REAL.writeLong(singleRowBlockWriter, i);
// Write Map<Bigint, Varchar> with 5 entries.
BlockBuilder mapWriter = singleRowBlockWriter.beginBlockEntry();
for (int j = 0; j < 5; j++) {
BIGINT.writeLong(mapWriter, i + j);
VARCHAR.writeSlice(mapWriter, utf8Slice("Value1" + j));
}
singleRowBlockWriter.closeEntry();
// Write array .
BlockBuilder arrayWriter = singleRowBlockWriter.beginBlockEntry();
for (int j = 0; j < 8; j++) {
DOUBLE.writeDouble(arrayWriter, i * 3 + j);
}
singleRowBlockWriter.closeEntry();
// Write row type.
BlockBuilder rowWriter = singleRowBlockWriter.beginBlockEntry();
if (i % 2 == 0) {
rowWriter.appendNull();
} else {
INTEGER.writeLong(rowWriter, i);
}
singleRowBlockWriter.closeEntry();
beginEntryBlockBuilder.closeEntry();
beginEntryBlockBuilder.appendNull();
}
RowBlockBuilder directEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
for (int i = 0; i < POSITION_COUNT; i++) {
directEntryBlockBuilder.beginDirectEntry();
REAL.writeLong(directEntryBlockBuilder.getBlockBuilder(0), i);
// Write Map<Bigint, Varchar> with 5 entries.
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) directEntryBlockBuilder.getBlockBuilder(1);
mapBlockBuilder.beginDirectEntry();
BlockBuilder keyBuilder = mapBlockBuilder.getKeyBlockBuilder();
BlockBuilder valueBuilder = mapBlockBuilder.getValueBlockBuilder();
for (int j = 0; j < 5; j++) {
BIGINT.writeLong(keyBuilder, i + j);
VARCHAR.writeSlice(valueBuilder, utf8Slice("Value1" + j));
}
mapBlockBuilder.closeEntry();
// Write array .
ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getBlockBuilder(2);
arrayBuilder.beginDirectEntry();
for (int j = 0; j < 8; j++) {
DOUBLE.writeDouble(arrayBuilder.getElementBlockBuilder(), i * 3 + j);
}
arrayBuilder.closeEntry();
// Write row type.
RowBlockBuilder nestedRowBuilder = (RowBlockBuilder) directEntryBlockBuilder.getBlockBuilder(3);
nestedRowBuilder.beginDirectEntry();
BlockBuilder nestedRowValueBuilder = nestedRowBuilder.getBlockBuilder(0);
if (i % 2 == 0) {
nestedRowValueBuilder.appendNull();
} else {
INTEGER.writeLong(nestedRowValueBuilder, i);
}
nestedRowBuilder.closeEntry();
directEntryBlockBuilder.closeEntry();
directEntryBlockBuilder.appendNull();
}
Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestDirectEntryBlockBuilder method testArrayWithNestedMap.
@Test
public void testArrayWithNestedMap() {
MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
ArrayBlockBuilder beginEntryBlockBuilder = new ArrayBlockBuilder(mapType, null, POSITION_COUNT);
for (int i = 0; i < POSITION_COUNT; i++) {
SingleArrayBlockWriter singleArrayBlockWriter = beginEntryBlockBuilder.beginBlockEntry();
for (int j = 0; j < 10; j++) {
BlockBuilder innerMapWriter = singleArrayBlockWriter.beginBlockEntry();
BIGINT.writeLong(innerMapWriter, i);
VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value1" + j));
BIGINT.writeLong(innerMapWriter, i + 1);
VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value2" + j));
singleArrayBlockWriter.closeEntry();
singleArrayBlockWriter.appendNull();
}
beginEntryBlockBuilder.closeEntry();
}
ArrayBlockBuilder directEntryBlockBuilder = new ArrayBlockBuilder(mapType, null, POSITION_COUNT);
for (int i = 0; i < POSITION_COUNT; i++) {
directEntryBlockBuilder.beginDirectEntry();
MapBlockBuilder innerBuilder = (MapBlockBuilder) directEntryBlockBuilder.getElementBlockBuilder();
BlockBuilder keyBuilder = innerBuilder.getKeyBlockBuilder();
BlockBuilder valueBuilder = innerBuilder.getValueBlockBuilder();
for (int j = 0; j < 10; j++) {
innerBuilder.beginDirectEntry();
BIGINT.writeLong(keyBuilder, i);
BIGINT.writeLong(keyBuilder, i + 1);
VARCHAR.writeSlice(valueBuilder, utf8Slice("Value1" + j));
VARCHAR.writeSlice(valueBuilder, utf8Slice("Value2" + j));
innerBuilder.closeEntry();
innerBuilder.appendNull();
}
directEntryBlockBuilder.closeEntry();
}
Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlock method createBlockBuilderWithValues.
private static BlockBuilder createBlockBuilderWithValues(Map<String, Long>[] maps) {
MapType mapType = mapType(VARCHAR, BIGINT);
BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
for (Map<String, Long> map : maps) {
createBlockBuilderWithValues(map, mapBlockBuilder);
}
return mapBlockBuilder;
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlock method createBlockBuilderWithValues.
private static void createBlockBuilderWithValues(Map<String, Long> map, BlockBuilder mapBlockBuilder) {
if (map == null) {
mapBlockBuilder.appendNull();
} else {
BlockBuilder elementBlockBuilder = mapBlockBuilder.beginBlockEntry();
for (Map.Entry<String, Long> entry : map.entrySet()) {
VARCHAR.writeSlice(elementBlockBuilder, utf8Slice(entry.getKey()));
if (entry.getValue() == null) {
elementBlockBuilder.appendNull();
} else {
BIGINT.writeLong(elementBlockBuilder, entry.getValue());
}
}
mapBlockBuilder.closeEntry();
}
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlock method testSingleValueBlock.
@Test
public void testSingleValueBlock() {
// 1 entry map.
Map<String, Long>[] values = createTestMap(50);
BlockBuilder mapBlockBuilder = createBlockBuilderWithValues(values);
Block mapBlock = mapBlockBuilder.build();
assertSame(mapBlock, mapBlock.getSingleValueBlock(0));
assertNotSame(mapBlockBuilder, mapBlockBuilder.getSingleValueBlock(0));
// 2 entries map.
values = createTestMap(50, 50);
mapBlockBuilder = createBlockBuilderWithValues(values);
mapBlock = mapBlockBuilder.build();
Block firstElement = mapBlock.getRegion(0, 1);
assertNotSame(firstElement, firstElement.getSingleValueBlock(0));
Block secondElementCopy = mapBlock.copyRegion(1, 1);
assertSame(secondElementCopy, secondElementCopy.getSingleValueBlock(0));
// Test with null elements.
values = new Map[] { null };
mapBlockBuilder = createBlockBuilderWithValues(values);
mapBlock = mapBlockBuilder.build();
assertSame(mapBlock, mapBlock.getSingleValueBlock(0));
assertNotSame(mapBlock, mapBlockBuilder.getSingleValueBlock(0));
// Test with 2 null elements.
values = new Map[] { null, null };
mapBlockBuilder = createBlockBuilderWithValues(values);
mapBlock = mapBlockBuilder.build();
assertNotSame(mapBlock, mapBlock.getSingleValueBlock(0));
}
Aggregations