Search in sources :

Example 11 with DictionaryBlock

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

the class TestDictionaryBlock method testCopyPositionsUnwrapping.

@Test
public void testCopyPositionsUnwrapping() {
    Slice[] expectedValues = createExpectedValues(10);
    DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100);
    assertTrue(dictionaryBlock.isCompact());
    // 0 and 1 position copies unwrap the dictionary
    assertInstanceOf(dictionaryBlock.copyPositions(new int[0], 0, 0), VariableWidthBlock.class);
    assertInstanceOf(dictionaryBlock.copyPositions(new int[1], 0, 1), VariableWidthBlock.class);
    // Dictionaries unwrap when each selected id appears only once
    int[] positionsToCopy = new int[] { 9, 7, 5, 3, 2, 1, 0 };
    assertInstanceOf(dictionaryBlock.copyPositions(positionsToCopy, 0, positionsToCopy.length), VariableWidthBlock.class);
    // Compact dictionaries unwrap
    DictionaryBlock compactSequentialDictionary = createDictionaryBlock(expectedValues, expectedValues.length);
    // force uniqueIds to be computed
    assertTrue(compactSequentialDictionary.isCompact());
    assertInstanceOf(compactSequentialDictionary.copyPositions(positionsToCopy, 0, positionsToCopy.length), VariableWidthBlock.class);
    // Nested dictionaries unwrap, even when not compact
    DictionaryBlock outerDictionary = new DictionaryBlock(compactSequentialDictionary, new int[] { 1, 3, 5, 7, 9 });
    // force uniqueIds to be computed
    assertFalse(outerDictionary.isCompact());
    positionsToCopy = new int[] { 0, 3, 2 };
    // inner dictionary is compact, and also unwraps
    assertInstanceOf(outerDictionary.copyPositions(positionsToCopy, 0, positionsToCopy.length), VariableWidthBlock.class);
    // Nested dictionaries unwrap down to a single dictionary layer when ids are repeated
    Block innermostRawBlock = createSlicesBlock(expectedValues);
    DictionaryBlock firstDictionary = new DictionaryBlock(innermostRawBlock, new int[] { 0, 1, 1, 7, 7, 5, 3 });
    DictionaryBlock secondDictionary = new DictionaryBlock(firstDictionary, new int[] { 3, 1, 1, 2 });
    DictionaryBlock thirdDictionary = new DictionaryBlock(secondDictionary, new int[] { 0, 0, 0, 2 });
    // Result is still a dictionary, but only a single layer
    positionsToCopy = new int[] { 2, 1, 0 };
    assertInstanceOf(thirdDictionary.copyPositions(positionsToCopy, 0, 2), DictionaryBlock.class);
    assertInstanceOf(((DictionaryBlock) thirdDictionary.copyPositions(positionsToCopy, 0, 2)).getDictionary(), VariableWidthBlock.class);
    // Re-check after all dictionaries have their uniqueIds computed
    assertFalse(firstDictionary.isCompact());
    assertFalse(secondDictionary.isCompact());
    assertFalse(thirdDictionary.isCompact());
    assertInstanceOf(thirdDictionary.copyPositions(positionsToCopy, 0, 2), DictionaryBlock.class);
    assertInstanceOf(((DictionaryBlock) thirdDictionary.copyPositions(positionsToCopy, 0, 2)).getDictionary(), VariableWidthBlock.class);
}
Also used : Slice(io.airlift.slice.Slice) DictionaryBlock(io.trino.spi.block.DictionaryBlock) BlockAssertions.createSlicesBlock(io.trino.block.BlockAssertions.createSlicesBlock) VariableWidthBlock(io.trino.spi.block.VariableWidthBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) IntArrayBlock(io.trino.spi.block.IntArrayBlock) Block(io.trino.spi.block.Block) Test(org.testng.annotations.Test)

Example 12 with DictionaryBlock

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

the class TestDictionaryBlock method testNestedDictionarySizes.

@Test
public void testNestedDictionarySizes() {
    // fixed width block
    Block fixedWidthBlock = new IntArrayBlock(100, Optional.empty(), IntStream.range(0, 100).toArray());
    assertDictionarySizeMethods(fixedWidthBlock);
    assertDictionarySizeMethods(new DictionaryBlock(fixedWidthBlock, IntStream.range(0, 50).toArray()));
    assertDictionarySizeMethods(new DictionaryBlock(new DictionaryBlock(fixedWidthBlock, IntStream.range(0, 50).toArray()), IntStream.range(0, 10).toArray()));
    // variable width block
    Block variableWidthBlock = createSlicesBlock(createExpectedValues(100));
    assertDictionarySizeMethods(variableWidthBlock);
    assertDictionarySizeMethods(new DictionaryBlock(variableWidthBlock, IntStream.range(0, 50).toArray()));
    assertDictionarySizeMethods(new DictionaryBlock(new DictionaryBlock(variableWidthBlock, IntStream.range(0, 50).toArray()), IntStream.range(0, 10).toArray()));
}
Also used : IntArrayBlock(io.trino.spi.block.IntArrayBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) BlockAssertions.createSlicesBlock(io.trino.block.BlockAssertions.createSlicesBlock) VariableWidthBlock(io.trino.spi.block.VariableWidthBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) IntArrayBlock(io.trino.spi.block.IntArrayBlock) Block(io.trino.spi.block.Block) Test(org.testng.annotations.Test)

Example 13 with DictionaryBlock

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

the class TestLookupJoinPageBuilder method testDifferentPositions.

@Test
public void testDifferentPositions() {
    int entries = 100;
    BlockBuilder blockBuilder = BIGINT.createFixedSizeBlockBuilder(entries);
    for (int i = 0; i < entries; i++) {
        BIGINT.writeLong(blockBuilder, i);
    }
    Block block = blockBuilder.build();
    Page page = new Page(block);
    JoinProbeFactory joinProbeFactory = new JoinProbeFactory(new int[] { 0 }, ImmutableList.of(0), OptionalInt.empty());
    LookupSource lookupSource = new TestLookupSource(ImmutableList.of(BIGINT), page);
    LookupJoinPageBuilder lookupJoinPageBuilder = new LookupJoinPageBuilder(ImmutableList.of(BIGINT));
    // empty
    JoinProbe probe = joinProbeFactory.createJoinProbe(page);
    Page output = lookupJoinPageBuilder.build(probe);
    assertEquals(output.getChannelCount(), 2);
    assertTrue(output.getBlock(0) instanceof DictionaryBlock);
    assertEquals(output.getPositionCount(), 0);
    lookupJoinPageBuilder.reset();
    // the probe covers non-sequential positions
    probe = joinProbeFactory.createJoinProbe(page);
    for (int joinPosition = 0; probe.advanceNextPosition(); joinPosition++) {
        if (joinPosition % 2 == 1) {
            continue;
        }
        lookupJoinPageBuilder.appendRow(probe, lookupSource, joinPosition);
    }
    output = lookupJoinPageBuilder.build(probe);
    assertEquals(output.getChannelCount(), 2);
    assertTrue(output.getBlock(0) instanceof DictionaryBlock);
    assertEquals(output.getPositionCount(), entries / 2);
    for (int i = 0; i < entries / 2; i++) {
        assertEquals(output.getBlock(0).getLong(i, 0), i * 2);
        assertEquals(output.getBlock(1).getLong(i, 0), i * 2);
    }
    lookupJoinPageBuilder.reset();
    // the probe covers everything
    probe = joinProbeFactory.createJoinProbe(page);
    for (int joinPosition = 0; probe.advanceNextPosition(); joinPosition++) {
        lookupJoinPageBuilder.appendRow(probe, lookupSource, joinPosition);
    }
    output = lookupJoinPageBuilder.build(probe);
    assertEquals(output.getChannelCount(), 2);
    assertFalse(output.getBlock(0) instanceof DictionaryBlock);
    assertEquals(output.getPositionCount(), entries);
    for (int i = 0; i < entries; i++) {
        assertEquals(output.getBlock(0).getLong(i, 0), i);
        assertEquals(output.getBlock(1).getLong(i, 0), i);
    }
    lookupJoinPageBuilder.reset();
    // the probe covers some sequential positions
    probe = joinProbeFactory.createJoinProbe(page);
    for (int joinPosition = 0; probe.advanceNextPosition(); joinPosition++) {
        if (joinPosition < 10 || joinPosition >= 50) {
            continue;
        }
        lookupJoinPageBuilder.appendRow(probe, lookupSource, joinPosition);
    }
    output = lookupJoinPageBuilder.build(probe);
    assertEquals(output.getChannelCount(), 2);
    assertFalse(output.getBlock(0) instanceof DictionaryBlock);
    assertEquals(output.getPositionCount(), 40);
    for (int i = 10; i < 50; i++) {
        assertEquals(output.getBlock(0).getLong(i - 10, 0), i);
        assertEquals(output.getBlock(1).getLong(i - 10, 0), i);
    }
}
Also used : JoinProbeFactory(io.trino.operator.join.JoinProbe.JoinProbeFactory) DictionaryBlock(io.trino.spi.block.DictionaryBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) Block(io.trino.spi.block.Block) Page(io.trino.spi.Page) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 14 with DictionaryBlock

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

the class TestPartitionedOutputOperator method testOutputForOneValueDictionaryBlock.

@Test
public void testOutputForOneValueDictionaryBlock() {
    PartitionedOutputOperator partitionedOutputOperator = partitionedOutputOperator(BIGINT).build();
    Page page = new Page(new DictionaryBlock(createLongsBlock(0), new int[] { 0, 0, 0, 0 }));
    processPages(partitionedOutputOperator, page);
    List<Object> partition0 = readLongs(outputBuffer.getEnqueuedDeserialized(0), 0);
    assertThat(partition0).containsExactlyElementsOf(nCopies(4, 0L));
    List<Object> partition1 = readLongs(outputBuffer.getEnqueuedDeserialized(1), 0);
    assertThat(partition1).isEmpty();
}
Also used : DictionaryBlock(io.trino.spi.block.DictionaryBlock) BlockAssertions.createLongDictionaryBlock(io.trino.block.BlockAssertions.createLongDictionaryBlock) Page(io.trino.spi.Page) Test(org.testng.annotations.Test)

Example 15 with DictionaryBlock

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

the class TestDictionaryAwarePageFilter method testDictionaryBlockProcessingWithUnusedFailure.

@Test
public void testDictionaryBlockProcessingWithUnusedFailure() {
    // match some
    testFilter(createDictionaryBlockWithUnusedEntries(20, 100), DictionaryBlock.class);
    // match none
    testFilter(createDictionaryBlockWithUnusedEntries(20, 0), DictionaryBlock.class);
    // match all
    testFilter(new DictionaryBlock(createLongsBlock(4, 5, -1), new int[100]), DictionaryBlock.class);
}
Also used : 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