Search in sources :

Example 11 with MapBlock

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

Example 12 with MapBlock

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

the class TestMapBlock method testCompactBlock.

@Test
public void testCompactBlock() {
    Block emptyBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]);
    Block compactKeyBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes());
    Block compactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes());
    Block inCompactKeyBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes());
    Block inCompactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes());
    int[] offsets = { 0, 1, 1, 2, 4, 8, 16 };
    boolean[] mapIsNull = { false, true, false, false, false, false };
    testCompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(0, Optional.empty(), new int[1], emptyBlock, emptyBlock));
    testCompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(mapIsNull.length, Optional.of(mapIsNull), offsets, compactKeyBlock, compactValueBlock));
    // TODO: Add test case for a sliced MapBlock
    // underlying key/value block is not compact
    testIncompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(mapIsNull.length, Optional.of(mapIsNull), offsets, inCompactKeyBlock, inCompactValueBlock));
}
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) ByteArrayBlock(com.facebook.presto.common.block.ByteArrayBlock) 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