use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method testCloseEntryStrict.
@Test
public void testCloseEntryStrict() throws Exception {
MapBlockBuilder mapBlockBuilder = createMapBlockBuilder();
// Add MAP_POSITIONS maps with only one entry but the same key
for (int i = 0; i < MAP_POSITIONS; i++) {
appendSingleEntryMap(mapBlockBuilder, 1);
}
assertFalse(mapBlockBuilder.isHashTablesPresent());
BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
// The purpose of this test is to make sure offset is calculated correctly in MapBlockBuilder.closeEntryStrict()
for (int i = 0; i < 50; i++) {
BIGINT.writeLong(entryBuilder, i);
BIGINT.writeLong(entryBuilder, -1);
}
mapBlockBuilder.closeEntryStrict(KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
assertTrue(mapBlockBuilder.isHashTablesPresent());
// Verify Keys
for (int i = 0; i < MAP_POSITIONS; i++) {
SingleMapBlock block = (SingleMapBlock) mapBlockBuilder.getBlock(i);
assertEquals(block.seekKeyExact(1, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE), 1);
}
SingleMapBlock singleMapBlock = (SingleMapBlock) mapBlockBuilder.getBlock(MAP_POSITIONS);
for (int i = 0; i < 50; i++) {
assertEquals(singleMapBlock.seekKeyExact(i, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE), i * 2 + 1);
}
// Verify that Block also has the hash tables loaded.
MapBlock mapBlock = (MapBlock) mapBlockBuilder.build();
assertTrue(mapBlock.isHashTablesPresent());
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method testAppendStructureWithMissingHash.
@Test
public void testAppendStructureWithMissingHash() {
MapBlockBuilder mapBlockBuilder = createMapBlockBuilder();
for (int i = 0; i < MAP_POSITIONS; i++) {
appendSingleEntryMap(mapBlockBuilder, i);
}
assertFalse(mapBlockBuilder.isHashTablesPresent());
MapBlockBuilder anotherBuilder = createMapBlockBuilder();
for (int i = 0; i < MAP_POSITIONS; i++) {
SingleMapBlock block = (SingleMapBlock) mapBlockBuilder.getBlock(i);
anotherBuilder.appendStructure(block);
}
assertFalse(anotherBuilder.isHashTablesPresent());
for (int i = 0; i < MAP_POSITIONS; i++) {
verifyOnlyKeyInMap(anotherBuilder, i, i);
assertTrue(anotherBuilder.isHashTablesPresent());
}
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method testMapBuilderSeekLoadsHashMap.
@Test
public void testMapBuilderSeekLoadsHashMap() {
MapBlockBuilder mapBlockBuilder = createMapBlockBuilder();
for (int i = 0; i < MAP_POSITIONS; i++) {
appendSingleEntryMap(mapBlockBuilder, i);
}
assertFalse(mapBlockBuilder.isHashTablesPresent());
// Verify Keys
for (int i = 0; i < MAP_POSITIONS; i++) {
SingleMapBlock block = (SingleMapBlock) mapBlockBuilder.getBlock(i);
assertEquals(block.seekKeyExact(i, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE), 1);
assertTrue(mapBlockBuilder.isHashTablesPresent());
}
for (int i = 0; i < MAP_POSITIONS; i++) {
// Add more entries and verify the keys.
appendSingleEntryMap(mapBlockBuilder, i);
verifyOnlyKeyInMap(mapBlockBuilder, MAP_POSITIONS + i, i);
}
// Verify that Block and Block Builder also has hash tables present when created.
MapBlock mapBlock = (MapBlock) mapBlockBuilder.build();
assertTrue(mapBlock.isHashTablesPresent());
MapBlockBuilder anotherBuilder = (MapBlockBuilder) mapBlockBuilder.newBlockBuilderLike(null);
assertTrue(anotherBuilder.isHashTablesPresent());
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method testMismatchedKeyValuePositionCountThrows.
@Test(expectedExceptions = IllegalStateException.class)
public void testMismatchedKeyValuePositionCountThrows() {
MapBlockBuilder mapBlockBuilder = createMapBlockBuilder();
mapBlockBuilder.beginDirectEntry();
// Write 2 keys.
mapBlockBuilder.getKeyBlockBuilder().writeLong(1);
mapBlockBuilder.getKeyBlockBuilder().writeLong(2);
// Write 3 values.
mapBlockBuilder.getValueBlockBuilder().writeLong(3);
mapBlockBuilder.getValueBlockBuilder().writeLong(4);
mapBlockBuilder.getValueBlockBuilder().writeLong(5);
mapBlockBuilder.closeEntry();
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method testMapBlockBuilderWithNullKeys.
@Test
public void testMapBlockBuilderWithNullKeys() {
MapBlockBuilder blockBuilder = createMapBlockBuilder();
for (int i = 0; i < MAP_POSITIONS; i++) {
if (i % 10 == 0) {
// Map is null
blockBuilder.appendNull();
} else {
// Map is valid.
BlockBuilder entryBuilder = blockBuilder.beginBlockEntry();
for (int j = 0; j < i; j++) {
if (j == 5) {
// Null Keys
entryBuilder.appendNull();
} else {
BIGINT.writeLong(entryBuilder, j);
}
BIGINT.writeLong(entryBuilder, i);
}
blockBuilder.closeEntry();
}
assertFalse(blockBuilder.isHashTablesPresent());
}
// Verify the contents of the map.
MapBlock mapBlock = (MapBlock) blockBuilder.build();
assertFalse(mapBlock.isHashTablesPresent());
ColumnarMap columnarMap = ColumnarMap.toColumnarMap(mapBlock);
for (int i = 0; i < MAP_POSITIONS; i++) {
assertEquals(columnarMap.isNull(i), i % 10 == 0);
Block keysBlock = columnarMap.getKeysBlock();
Block valuesBlock = columnarMap.getValuesBlock();
if (!columnarMap.isNull(i)) {
int offset = columnarMap.getOffset(i);
for (int j = 0; j < i; j++) {
assertEquals(keysBlock.isNull(offset + j), j == 5);
if (!keysBlock.isNull(offset + j)) {
assertEquals(BIGINT.getLong(keysBlock, offset + j), j);
}
assertEquals(BIGINT.getLong(valuesBlock, offset + j), i);
}
}
}
// Verify block and newBlockBuilder does not have the hashTables present.
assertFalse(mapBlock.isHashTablesPresent());
MapBlockBuilder anotherBuilder = (MapBlockBuilder) blockBuilder.newBlockBuilderLike(null);
assertFalse(anotherBuilder.isHashTablesPresent());
}
Aggregations