Search in sources :

Example 6 with DictionaryId

use of io.trino.spi.block.DictionaryId 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

Block (io.trino.spi.block.Block)6 DictionaryBlock (io.trino.spi.block.DictionaryBlock)6 DictionaryId (io.trino.spi.block.DictionaryId)6 DictionaryId.randomDictionaryId (io.trino.spi.block.DictionaryId.randomDictionaryId)5 Test (org.testng.annotations.Test)3 Slice (io.airlift.slice.Slice)2 VariableWidthBlock (io.trino.spi.block.VariableWidthBlock)2 ArrayList (java.util.ArrayList)2 SizeOf.sizeOf (io.airlift.slice.SizeOf.sizeOf)1 BlockAssertions.createLongSequenceBlock (io.trino.block.BlockAssertions.createLongSequenceBlock)1 BlockAssertions.createLongsBlock (io.trino.block.BlockAssertions.createLongsBlock)1 BlockAssertions.createSlicesBlock (io.trino.block.BlockAssertions.createSlicesBlock)1 BlockAssertions.createStringSequenceBlock (io.trino.block.BlockAssertions.createStringSequenceBlock)1 GroupByHash.createGroupByHash (io.trino.operator.GroupByHash.createGroupByHash)1 Page (io.trino.spi.Page)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 IntArrayBlock (io.trino.spi.block.IntArrayBlock)1 LazyBlock (io.trino.spi.block.LazyBlock)1 RunLengthEncodedBlock (io.trino.spi.block.RunLengthEncodedBlock)1 TypeTestUtils.getHashBlock (io.trino.type.TypeTestUtils.getHashBlock)1