Search in sources :

Example 6 with MapBlockBuilder

use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.

the class TestColumnarMap method testMapWithNullKeys.

@Test
public void testMapWithNullKeys() {
    // Presto Query Engine does not support Map with Null keys.
    // Presto ORC reader and Writer are used as library in some other
    // projects and it requires null keys to be supported in the Map.
    MapBlockBuilder mapBlockBuilder = createMapBuilder(100);
    mapBlockBuilder.appendNull();
    for (int i = 1; i < 100; i++) {
        Slice valueSlice = Slices.utf8Slice(Integer.toString(i));
        SingleMapBlockWriter blockWriter = mapBlockBuilder.beginBlockEntry();
        // Null Key
        blockWriter.appendNull();
        VARCHAR.writeSlice(blockWriter, valueSlice);
        for (int j = 1; j < i; j++) {
            Slice keySlice = Slices.utf8Slice(Integer.toString(j));
            VARCHAR.writeSlice(blockWriter, keySlice);
            VARCHAR.writeSlice(blockWriter, valueSlice);
        }
        mapBlockBuilder.closeEntry();
    }
    ColumnarMap columnarMap = toColumnarMap(mapBlockBuilder.build());
    assertEquals(columnarMap.getPositionCount(), 100);
    assertTrue(columnarMap.isNull(0));
    for (int i = 1; i < 100; i++) {
        assertFalse(columnarMap.isNull(i));
        Slice valueSlice = Slices.utf8Slice(Integer.toString(i));
        int offset = columnarMap.getOffset(i);
        assertTrue(columnarMap.getKeysBlock().isNull(offset));
        verifySlice(columnarMap.getValuesBlock(), offset, valueSlice);
        for (int j = 1; j < i; j++) {
            Slice keySlice = Slices.utf8Slice(Integer.toString(j));
            verifySlice(columnarMap.getKeysBlock(), offset + j, keySlice);
            verifySlice(columnarMap.getValuesBlock(), offset + j, valueSlice);
        }
    }
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Slice(io.airlift.slice.Slice) SingleMapBlockWriter(com.facebook.presto.common.block.SingleMapBlockWriter) ColumnarMap(com.facebook.presto.common.block.ColumnarMap) ColumnarMap.toColumnarMap(com.facebook.presto.common.block.ColumnarMap.toColumnarMap) Test(org.testng.annotations.Test)

Example 7 with MapBlockBuilder

use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.

the class TestColumnarMap method createMapBuilder.

private static MapBlockBuilder createMapBuilder(int expectedEntries) {
    MethodHandle varcharNativeEquals = MethodHandleUtil.methodHandle(Slice.class, "equals", Object.class).asType(MethodType.methodType(boolean.class, Slice.class, Slice.class));
    MethodHandle varcharBlockEquals = compose(varcharNativeEquals, nativeValueGetter(VARCHAR), nativeValueGetter(VARCHAR));
    return new MapBlockBuilder(VARCHAR, VARCHAR, varcharBlockEquals, MethodHandleUtil.methodHandle(TestColumnarMap.class, "blockVarcharHashCode", Block.class, int.class), null, expectedEntries);
}
Also used : Slice(io.airlift.slice.Slice) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) ColumnarTestUtils.createTestDictionaryBlock(com.facebook.presto.block.ColumnarTestUtils.createTestDictionaryBlock) ColumnarTestUtils.createTestRleBlock(com.facebook.presto.block.ColumnarTestUtils.createTestRleBlock) ColumnarTestUtils.assertBlock(com.facebook.presto.block.ColumnarTestUtils.assertBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block) MethodHandle(java.lang.invoke.MethodHandle)

Example 8 with MapBlockBuilder

use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.

the class TestMapBlockBuilder method appendSingleEntryMap.

private void appendSingleEntryMap(MapBlockBuilder mapBlockBuilder, int i) {
    BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
    BIGINT.writeLong(entryBuilder, i);
    BIGINT.writeLong(entryBuilder, -1);
    mapBlockBuilder.closeEntry();
}
Also used : BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder)

Example 9 with MapBlockBuilder

use of com.facebook.presto.common.block.MapBlockBuilder 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 10 with MapBlockBuilder

use of com.facebook.presto.common.block.MapBlockBuilder 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)

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