Search in sources :

Example 11 with MapBlockBuilder

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());
}
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 12 with MapBlockBuilder

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

Example 13 with MapBlockBuilder

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());
}
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 14 with MapBlockBuilder

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

Example 15 with MapBlockBuilder

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

Aggregations

MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)21 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)14 Test (org.testng.annotations.Test)13 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)8 AbstractMapBlock (com.facebook.presto.common.block.AbstractMapBlock)6 MapBlock (com.facebook.presto.common.block.MapBlock)6 MapType (com.facebook.presto.common.type.MapType)5 Slice (io.airlift.slice.Slice)5 ArrayBlockBuilder (com.facebook.presto.common.block.ArrayBlockBuilder)3 Block (com.facebook.presto.common.block.Block)3 RowBlockBuilder (com.facebook.presto.common.block.RowBlockBuilder)3 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)3 ColumnarMap (com.facebook.presto.common.block.ColumnarMap)2 ArrayType (com.facebook.presto.common.type.ArrayType)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)1 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)1