use of com.facebook.presto.common.block.MapBlock in project presto by prestodb.
the class TestMapBlock method testSeekKey.
@Test
public void testSeekKey() {
Block keyBlock = createStringsBlock("k");
Block valueBlock = createStringsBlock("v");
Block mapBlock = mapType(VARCHAR, VARCHAR).createBlockFromKeyValue(1, Optional.empty(), IntStream.range(0, 2).toArray(), keyBlock, valueBlock);
SingleMapBlock singleMapBlock = (SingleMapBlock) mapBlock.getBlock(0);
MethodHandle keyNativeHashCode = getOperatorMethodHandle(OperatorType.HASH_CODE, VARCHAR);
MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(VARCHAR));
MethodHandle keyNativeEquals = getOperatorMethodHandle(OperatorType.EQUAL, VARCHAR, VARCHAR);
MethodHandle keyBlockNativeEquals = compose(keyNativeEquals, nativeValueGetter(VARCHAR));
// Testing not found case. The key to seek should be longer than AbstractVariableWidthType#EXPECTED_BYTES_PER_ENTRY(32) and has same hash code as the key in keyBlock.
// This is because the default capacity of the slice of the keyBlock for 1 single character key is EXPECTED_BYTES_PER_ENTRY. We want to make the seeked key out of boundary
// to make sure no exception is thrown.
assertEquals(singleMapBlock.seekKeyExact(Slices.utf8Slice(new String(new char[10]).replace("\0", "Doudou")), keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode), -1);
// Testing found case.
assertEquals(singleMapBlock.seekKeyExact(Slices.utf8Slice("k"), keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode), 1);
}
use of com.facebook.presto.common.block.MapBlock 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.MapBlock 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());
}
use of com.facebook.presto.common.block.MapBlock 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.MapBlock 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());
}
Aggregations