Search in sources :

Example 71 with DictionaryBlock

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

the class Page method compactRelatedBlocks.

private static List<DictionaryBlock> compactRelatedBlocks(List<DictionaryBlock> blocks) {
    DictionaryBlock firstDictionaryBlock = blocks.get(0);
    Block dictionary = firstDictionaryBlock.getDictionary();
    int positionCount = firstDictionaryBlock.getPositionCount();
    int dictionarySize = dictionary.getPositionCount();
    // determine which dictionary entries are referenced and build a reindex for them
    int[] dictionaryPositionsToCopy = new int[min(dictionarySize, positionCount)];
    int[] remapIndex = new int[dictionarySize];
    Arrays.fill(remapIndex, -1);
    int numberOfIndexes = 0;
    for (int i = 0; i < positionCount; i++) {
        int position = firstDictionaryBlock.getId(i);
        if (remapIndex[position] == -1) {
            dictionaryPositionsToCopy[numberOfIndexes] = position;
            remapIndex[position] = numberOfIndexes;
            numberOfIndexes++;
        }
    }
    // entire dictionary is referenced
    if (numberOfIndexes == dictionarySize) {
        return blocks;
    }
    // compact the dictionaries
    int[] newIds = getNewIds(positionCount, firstDictionaryBlock, remapIndex);
    List<DictionaryBlock> outputDictionaryBlocks = new ArrayList<>(blocks.size());
    DictionaryId newDictionaryId = randomDictionaryId();
    for (DictionaryBlock dictionaryBlock : blocks) {
        if (!firstDictionaryBlock.getDictionarySourceId().equals(dictionaryBlock.getDictionarySourceId())) {
            throw new IllegalArgumentException("dictionarySourceIds must be the same");
        }
        try {
            Block compactDictionary = dictionaryBlock.getDictionary().copyPositions(dictionaryPositionsToCopy, 0, numberOfIndexes);
            outputDictionaryBlocks.add(new DictionaryBlock(positionCount, compactDictionary, newIds, !(compactDictionary instanceof DictionaryBlock), newDictionaryId));
        } catch (UnsupportedOperationException e) {
            // ignore if copy positions is not supported for the dictionary
            outputDictionaryBlocks.add(dictionaryBlock);
        }
    }
    return outputDictionaryBlocks;
}
Also used : DictionaryId(io.trino.spi.block.DictionaryId) DictionaryId.randomDictionaryId(io.trino.spi.block.DictionaryId.randomDictionaryId) DictionaryBlock(io.trino.spi.block.DictionaryBlock) ArrayList(java.util.ArrayList) DictionaryBlock(io.trino.spi.block.DictionaryBlock) Block(io.trino.spi.block.Block)

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