Search in sources :

Example 6 with DictionaryBlock

use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.

the class TestPage method testCompactDictionaryBlocks.

@Test
public void testCompactDictionaryBlocks() {
    int positionCount = 100;
    // Create 2 dictionary blocks with the same source id
    DictionaryId commonSourceId = randomDictionaryId();
    int commonDictionaryUsedPositions = 20;
    int[] commonDictionaryIds = getDictionaryIds(positionCount, commonDictionaryUsedPositions);
    // first dictionary contains "varbinary" values
    Slice[] dictionaryValues1 = createExpectedValues(50);
    Block dictionary1 = createSlicesBlock(dictionaryValues1);
    DictionaryBlock commonSourceIdBlock1 = new DictionaryBlock(positionCount, dictionary1, commonDictionaryIds, commonSourceId);
    // second dictionary block is "length(firstColumn)"
    BlockBuilder dictionary2 = BIGINT.createBlockBuilder(null, dictionary1.getPositionCount());
    for (Slice expectedValue : dictionaryValues1) {
        BIGINT.writeLong(dictionary2, expectedValue.length());
    }
    DictionaryBlock commonSourceIdBlock2 = new DictionaryBlock(positionCount, dictionary2.build(), commonDictionaryIds, commonSourceId);
    // Create block with a different source id, dictionary size, used
    int otherDictionaryUsedPositions = 30;
    int[] otherDictionaryIds = getDictionaryIds(positionCount, otherDictionaryUsedPositions);
    Block dictionary3 = createSlicesBlock(createExpectedValues(70));
    DictionaryBlock randomSourceIdBlock = new DictionaryBlock(dictionary3, otherDictionaryIds);
    Page page = new Page(commonSourceIdBlock1, randomSourceIdBlock, commonSourceIdBlock2);
    page.compact();
    // dictionary blocks should all be compact
    assertTrue(((DictionaryBlock) page.getBlock(0)).isCompact());
    assertTrue(((DictionaryBlock) page.getBlock(1)).isCompact());
    assertTrue(((DictionaryBlock) page.getBlock(2)).isCompact());
    assertEquals(((DictionaryBlock) page.getBlock(0)).getDictionary().getPositionCount(), commonDictionaryUsedPositions);
    assertEquals(((DictionaryBlock) page.getBlock(1)).getDictionary().getPositionCount(), otherDictionaryUsedPositions);
    assertEquals(((DictionaryBlock) page.getBlock(2)).getDictionary().getPositionCount(), commonDictionaryUsedPositions);
    // Blocks that had the same source id before compacting page should have the same source id after compacting page
    assertNotEquals(((DictionaryBlock) page.getBlock(0)).getDictionarySourceId(), ((DictionaryBlock) page.getBlock(1)).getDictionarySourceId());
    assertEquals(((DictionaryBlock) page.getBlock(0)).getDictionarySourceId(), ((DictionaryBlock) page.getBlock(2)).getDictionarySourceId());
}
Also used : DictionaryId(io.trino.spi.block.DictionaryId) DictionaryId.randomDictionaryId(io.trino.spi.block.DictionaryId.randomDictionaryId) Slice(io.airlift.slice.Slice) DictionaryBlock(io.trino.spi.block.DictionaryBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) LazyBlock(io.trino.spi.block.LazyBlock) Block(io.trino.spi.block.Block) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 7 with DictionaryBlock

use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.

the class TestPage method testCompactNestedDictionary.

@Test
public void testCompactNestedDictionary() {
    Slice[] expectedValues = createExpectedValues(10);
    Block valuesBlock = createSlicesBlock(expectedValues);
    DictionaryBlock nestedDictionary = new DictionaryBlock(valuesBlock, new int[] { 0, 1, 2, 2, 4, 5 });
    DictionaryBlock dictionary = new DictionaryBlock(nestedDictionary, new int[] { 2, 3, 2, 0 });
    Page page = new Page(dictionary);
    page.compact();
    // Page#compact does not unnest nested dictionaries
    assertFalse(((DictionaryBlock) page.getBlock(0)).isCompact());
}
Also used : Slice(io.airlift.slice.Slice) DictionaryBlock(io.trino.spi.block.DictionaryBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) LazyBlock(io.trino.spi.block.LazyBlock) Block(io.trino.spi.block.Block) Test(org.testng.annotations.Test)

Example 8 with DictionaryBlock

use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.

the class TestColumnarArray method assertDictionaryBlock.

private static <T> void assertDictionaryBlock(Block block, T[] expectedValues) {
    DictionaryBlock dictionaryBlock = createTestDictionaryBlock(block);
    T[] expectedDictionaryValues = createTestDictionaryExpectedValues(expectedValues);
    assertBlock(dictionaryBlock, expectedDictionaryValues);
    assertColumnarArray(dictionaryBlock, expectedDictionaryValues);
    assertRunLengthEncodedBlock(dictionaryBlock, expectedDictionaryValues);
}
Also used : ColumnarTestUtils.createTestDictionaryBlock(io.trino.block.ColumnarTestUtils.createTestDictionaryBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock)

Example 9 with DictionaryBlock

use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.

the class TestDictionaryBlock method testCompactGetPositions.

@Test
public void testCompactGetPositions() {
    DictionaryBlock block = new DictionaryBlock(createSlicesBlock(createExpectedValues(10)), new int[] { 0, 1, 2, 3, 4, 5 }).compact();
    // 3, 3, 4, 5, 2, 0, 1, 1
    block = (DictionaryBlock) block.getPositions(new int[] { 3, 3, 4, 5, 2, 0, 1, 1 }, 0, 7);
    assertTrue(block.isCompact());
    // 3, 3, 4, 5, 2, 0, 1, 1, 0, 2, 5, 4, 3
    block = (DictionaryBlock) block.getPositions(new int[] { 0, 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1 }, 0, 12);
    assertTrue(block.isCompact());
    // 3, 4, 3, 4, 3
    block = (DictionaryBlock) block.getPositions(new int[] { 0, 2, 0, 2, 0 }, 0, 5);
    assertFalse(block.isCompact());
    block = block.compact();
    // 3, 4, 4, 4
    block = (DictionaryBlock) block.getPositions(new int[] { 0, 1, 1, 1 }, 0, 4);
    assertTrue(block.isCompact());
    // 4, 4, 4, 4
    block = (DictionaryBlock) block.getPositions(new int[] { 1, 1, 1, 1 }, 0, 4);
    assertFalse(block.isCompact());
    block = block.compact();
    // 4
    block = (DictionaryBlock) block.getPositions(new int[] { 0 }, 0, 1);
    assertTrue(block.isCompact());
    // empty
    block = (DictionaryBlock) block.getPositions(new int[] {}, 0, 0);
    assertFalse(block.isCompact());
    block = block.compact();
    // empty
    block = (DictionaryBlock) block.getPositions(new int[] {}, 0, 0);
    assertTrue(block.isCompact());
}
Also used : DictionaryBlock(io.trino.spi.block.DictionaryBlock) Test(org.testng.annotations.Test)

Example 10 with DictionaryBlock

use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.

the class TestDictionaryBlock method testCompactAllKeysReferenced.

@Test
public void testCompactAllKeysReferenced() {
    Slice[] expectedValues = createExpectedValues(5);
    DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 10);
    DictionaryBlock compactBlock = dictionaryBlock.compact();
    // When there is nothing to compact, we return the same block
    assertEquals(compactBlock.getDictionary(), dictionaryBlock.getDictionary());
    assertEquals(compactBlock.getPositionCount(), dictionaryBlock.getPositionCount());
    for (int position = 0; position < compactBlock.getPositionCount(); position++) {
        assertEquals(compactBlock.getId(position), dictionaryBlock.getId(position));
    }
    assertEquals(compactBlock.isCompact(), true);
}
Also used : Slice(io.airlift.slice.Slice) DictionaryBlock(io.trino.spi.block.DictionaryBlock) Test(org.testng.annotations.Test)

Aggregations

DictionaryBlock (io.trino.spi.block.DictionaryBlock)71 Block (io.trino.spi.block.Block)39 Test (org.testng.annotations.Test)38 Slice (io.airlift.slice.Slice)21 RunLengthEncodedBlock (io.trino.spi.block.RunLengthEncodedBlock)16 Page (io.trino.spi.Page)15 LazyBlock (io.trino.spi.block.LazyBlock)10 BlockAssertions.createLongSequenceBlock (io.trino.block.BlockAssertions.createLongSequenceBlock)9 BlockAssertions.createLongsBlock (io.trino.block.BlockAssertions.createLongsBlock)9 LongArrayBlock (io.trino.spi.block.LongArrayBlock)9 ColumnarTestUtils.assertBlock (io.trino.block.ColumnarTestUtils.assertBlock)8 PageBuilderStatus (io.trino.spi.block.PageBuilderStatus)8 VariableWidthBlock (io.trino.spi.block.VariableWidthBlock)7 DictionaryId (io.trino.spi.block.DictionaryId)6 BlockAssertions.createLongDictionaryBlock (io.trino.block.BlockAssertions.createLongDictionaryBlock)5 BlockAssertions.createSlicesBlock (io.trino.block.BlockAssertions.createSlicesBlock)5 TestingUnnesterUtil.createSimpleBlock (io.trino.operator.unnest.TestingUnnesterUtil.createSimpleBlock)5 BlockBuilder (io.trino.spi.block.BlockBuilder)5 DictionaryId.randomDictionaryId (io.trino.spi.block.DictionaryId.randomDictionaryId)5 IntArrayBlock (io.trino.spi.block.IntArrayBlock)5