Search in sources :

Example 6 with MapBlock

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);
}
Also used : BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) AbstractMapBlock(com.facebook.presto.common.block.AbstractMapBlock) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) MapBlock(com.facebook.presto.common.block.MapBlock) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) ByteArrayBlock(com.facebook.presto.common.block.ByteArrayBlock) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) BlockAssertions.createLongDictionaryBlock(com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock) Block(com.facebook.presto.common.block.Block) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) MethodHandle(java.lang.invoke.MethodHandle) TestingEnvironment.getOperatorMethodHandle(com.facebook.presto.testing.TestingEnvironment.getOperatorMethodHandle) Test(org.testng.annotations.Test)

Example 7 with MapBlock

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);
    }
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) AbstractMapBlock(com.facebook.presto.common.block.AbstractMapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) MapBlock(com.facebook.presto.common.block.MapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) Test(org.testng.annotations.Test)

Example 8 with MapBlock

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());
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) AbstractMapBlock(com.facebook.presto.common.block.AbstractMapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) MapBlock(com.facebook.presto.common.block.MapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Test(org.testng.annotations.Test)

Example 9 with MapBlock

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());
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) AbstractMapBlock(com.facebook.presto.common.block.AbstractMapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) MapBlock(com.facebook.presto.common.block.MapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Test(org.testng.annotations.Test)

Example 10 with MapBlock

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());
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) AbstractMapBlock(com.facebook.presto.common.block.AbstractMapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) MapBlock(com.facebook.presto.common.block.MapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) Test(org.testng.annotations.Test)

Aggregations

MapBlock (com.facebook.presto.common.block.MapBlock)12 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)10 AbstractMapBlock (com.facebook.presto.common.block.AbstractMapBlock)9 Test (org.testng.annotations.Test)9 Block (com.facebook.presto.common.block.Block)7 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)6 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)4 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)4 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)3 BlockAssertions.createRLEBlock (com.facebook.presto.block.BlockAssertions.createRLEBlock)3 BlockAssertions.createRandomLongsBlock (com.facebook.presto.block.BlockAssertions.createRandomLongsBlock)3 BlockAssertions.createStringsBlock (com.facebook.presto.block.BlockAssertions.createStringsBlock)3 ByteArrayBlock (com.facebook.presto.common.block.ByteArrayBlock)3 ColumnarMap (com.facebook.presto.common.block.ColumnarMap)2 HashMap (java.util.HashMap)2 BlockAssertions.createMapBlock (com.facebook.presto.block.BlockAssertions.createMapBlock)1 BlockAssertions.createMapType (com.facebook.presto.block.BlockAssertions.createMapType)1 ArrayBlock (com.facebook.presto.common.block.ArrayBlock)1 BlockLease (com.facebook.presto.common.block.BlockLease)1 ColumnarArray (com.facebook.presto.common.block.ColumnarArray)1