use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestColumnarMap method testMapWithNullKeys.
@Test
public void testMapWithNullKeys() {
// Presto Query Engine does not support Map with Null keys.
// Presto ORC reader and Writer are used as library in some other
// projects and it requires null keys to be supported in the Map.
MapBlockBuilder mapBlockBuilder = createMapBuilder(100);
mapBlockBuilder.appendNull();
for (int i = 1; i < 100; i++) {
Slice valueSlice = Slices.utf8Slice(Integer.toString(i));
SingleMapBlockWriter blockWriter = mapBlockBuilder.beginBlockEntry();
// Null Key
blockWriter.appendNull();
VARCHAR.writeSlice(blockWriter, valueSlice);
for (int j = 1; j < i; j++) {
Slice keySlice = Slices.utf8Slice(Integer.toString(j));
VARCHAR.writeSlice(blockWriter, keySlice);
VARCHAR.writeSlice(blockWriter, valueSlice);
}
mapBlockBuilder.closeEntry();
}
ColumnarMap columnarMap = toColumnarMap(mapBlockBuilder.build());
assertEquals(columnarMap.getPositionCount(), 100);
assertTrue(columnarMap.isNull(0));
for (int i = 1; i < 100; i++) {
assertFalse(columnarMap.isNull(i));
Slice valueSlice = Slices.utf8Slice(Integer.toString(i));
int offset = columnarMap.getOffset(i);
assertTrue(columnarMap.getKeysBlock().isNull(offset));
verifySlice(columnarMap.getValuesBlock(), offset, valueSlice);
for (int j = 1; j < i; j++) {
Slice keySlice = Slices.utf8Slice(Integer.toString(j));
verifySlice(columnarMap.getKeysBlock(), offset + j, keySlice);
verifySlice(columnarMap.getValuesBlock(), offset + j, valueSlice);
}
}
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestColumnarMap method createMapBuilder.
private static MapBlockBuilder createMapBuilder(int expectedEntries) {
MethodHandle varcharNativeEquals = MethodHandleUtil.methodHandle(Slice.class, "equals", Object.class).asType(MethodType.methodType(boolean.class, Slice.class, Slice.class));
MethodHandle varcharBlockEquals = compose(varcharNativeEquals, nativeValueGetter(VARCHAR), nativeValueGetter(VARCHAR));
return new MapBlockBuilder(VARCHAR, VARCHAR, varcharBlockEquals, MethodHandleUtil.methodHandle(TestColumnarMap.class, "blockVarcharHashCode", Block.class, int.class), null, expectedEntries);
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method appendSingleEntryMap.
private void appendSingleEntryMap(MapBlockBuilder mapBlockBuilder, int i) {
BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
BIGINT.writeLong(entryBuilder, i);
BIGINT.writeLong(entryBuilder, -1);
mapBlockBuilder.closeEntry();
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method testAppendStructureWithHashPresent.
@Test
public void testAppendStructureWithHashPresent() {
MapBlockBuilder mapBlockBuilder = createMapBlockBuilder();
for (int i = 0; i < MAP_POSITIONS; i++) {
appendSingleEntryMap(mapBlockBuilder, i);
}
assertFalse(mapBlockBuilder.isHashTablesPresent());
MapBlockBuilder anotherBuilder = (MapBlockBuilder) mapBlockBuilder.newBlockBuilderLike(null);
MapBlock mapBlock = (MapBlock) mapBlockBuilder.build();
for (int i = 0; i < MAP_POSITIONS; i++) {
anotherBuilder.appendStructureInternal(mapBlock, i);
}
SingleMapBlock singleMapBlock = (SingleMapBlock) mapBlock.getBlock(1);
singleMapBlock.seekKeyExact(1, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE);
assertFalse(anotherBuilder.isHashTablesPresent());
for (int i = 0; i < MAP_POSITIONS; i++) {
// Adding mapBlock with hash table present, forces the builder to build its hash table.
anotherBuilder.appendStructureInternal(mapBlock, i);
assertTrue(anotherBuilder.isHashTablesPresent());
// Verify the keys for them automatically.
verifyOnlyKeyInMap(anotherBuilder, MAP_POSITIONS + i, i);
}
for (int i = 0; i < MAP_POSITIONS; i++) {
verifyOnlyKeyInMap(anotherBuilder, i, i);
}
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method testMapBlockSeek.
@Test
public void testMapBlockSeek() {
MapBlockBuilder blockBuilder = createMapBlockBuilder();
for (int i = 0; i < MAP_POSITIONS; i++) {
BlockBuilder entryBuilder = blockBuilder.beginBlockEntry();
for (int j = 0; j < i; j++) {
// key
BIGINT.writeLong(entryBuilder, j);
// value
BIGINT.writeLong(entryBuilder, i);
}
blockBuilder.closeEntry();
}
assertFalse(blockBuilder.isHashTablesPresent());
MapBlock mapBlock = (MapBlock) blockBuilder.build();
assertFalse(mapBlock.isHashTablesPresent());
for (int i = 0; i < MAP_POSITIONS; i++) {
SingleMapBlock singleMapBlock = (SingleMapBlock) mapBlock.getBlock(i);
for (int j = 0; j < i; j++) {
assertEquals(singleMapBlock.seekKeyExact(j, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE), j * 2 + 1);
}
assertEquals(mapBlock.isHashTablesPresent(), i > 0);
}
// Block copies the HashMap, so block builder's hash table is still not present.
assertFalse(blockBuilder.isHashTablesPresent());
}
Aggregations