use of com.facebook.presto.common.block.SingleMapBlock 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.SingleMapBlock 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.SingleMapBlock 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.SingleMapBlock in project presto by prestodb.
the class TestMapBlockBuilder method testDirectBlockEntry.
@Test
public void testDirectBlockEntry() {
MapType innerMapType = new MapType(BIGINT, BIGINT, KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
MapType mapType = new MapType(BIGINT, innerMapType, KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
MapBlockBuilder blockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, MAP_POSITIONS);
int numberOfOuterElements = 10;
int numberOfInnerElements = 500;
int outerKeyBase = 100;
int keyBase = 1_000;
int valueBase = 1_000_000;
// Each outer map's value has 500 entries of map {long, long} (This is called inner map).
for (int element = 0; element < MAP_POSITIONS; element++) {
blockBuilder.beginDirectEntry();
BlockBuilder outerKeyBuilder = blockBuilder.getKeyBlockBuilder();
for (int outer = 0; outer < numberOfOuterElements; outer++) {
BIGINT.writeLong(outerKeyBuilder, element * outerKeyBase + outer);
}
MapBlockBuilder outerValueBuilder = (MapBlockBuilder) blockBuilder.getValueBlockBuilder();
for (int outer = 0; outer < numberOfOuterElements; outer++) {
outerValueBuilder.beginDirectEntry();
BlockBuilder innerKeyBuilder = outerValueBuilder.getKeyBlockBuilder();
for (int inner = 0; inner < numberOfInnerElements; inner++) {
BIGINT.writeLong(innerKeyBuilder, inner + outer * keyBase);
}
BlockBuilder innerValueBuilder = outerValueBuilder.getValueBlockBuilder();
for (int inner = 0; inner < numberOfInnerElements; inner++) {
BIGINT.writeLong(innerValueBuilder, inner + outer * valueBase);
}
outerValueBuilder.closeEntry();
}
blockBuilder.closeEntry();
}
assertEquals(blockBuilder.getPositionCount(), MAP_POSITIONS);
for (int element = 0; element < blockBuilder.getPositionCount(); element++) {
SingleMapBlock outerBlock = (SingleMapBlock) blockBuilder.getBlock(element);
assertEquals(outerBlock.getPositionCount(), numberOfOuterElements * 2);
for (int outer = 0; outer < numberOfOuterElements; outer++) {
assertEquals(outerBlock.getLong(outer * 2), (long) element * outerKeyBase + outer);
SingleMapBlock innerValueBlock = (SingleMapBlock) outerBlock.getBlock(outer * 2 + 1);
assertEquals(innerValueBlock.getPositionCount(), numberOfInnerElements * 2);
for (int inner = 0; inner < numberOfInnerElements; inner++) {
assertEquals(innerValueBlock.getLong(inner * 2), (long) outer * keyBase + inner);
assertEquals(innerValueBlock.getLong(inner * 2 + 1), (long) outer * valueBase + inner);
}
}
}
}
use of com.facebook.presto.common.block.SingleMapBlock in project presto by prestodb.
the class TestMapBlockBuilder method verifyOnlyKeyInMap.
private void verifyOnlyKeyInMap(AbstractMapBlock block, int position, int key) {
SingleMapBlock singleMapBlock = (SingleMapBlock) block.getBlock(position);
assertEquals(singleMapBlock.seekKeyExact(key, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE), 1);
// Any int other than key will do.
int nonExistentKey = key + 1;
assertEquals(singleMapBlock.seekKeyExact(nonExistentKey, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE), -1);
}
Aggregations