Search in sources :

Example 1 with DictionaryBlock

use of com.facebook.presto.spi.block.DictionaryBlock in project presto by prestodb.

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
    List<Integer> dictionaryPositionsToCopy = new ArrayList<>(min(dictionarySize, positionCount));
    int[] remapIndex = new int[dictionarySize];
    Arrays.fill(remapIndex, -1);
    int newIndex = 0;
    for (int i = 0; i < positionCount; i++) {
        int position = firstDictionaryBlock.getId(i);
        if (remapIndex[position] == -1) {
            dictionaryPositionsToCopy.add(position);
            remapIndex[position] = newIndex;
            newIndex++;
        }
    }
    // entire dictionary is referenced
    if (dictionaryPositionsToCopy.size() == 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);
            outputDictionaryBlocks.add(new DictionaryBlock(positionCount, compactDictionary, newIds, true, newDictionaryId));
        } catch (UnsupportedOperationException e) {
            // ignore if copy positions is not supported for the dictionary
            outputDictionaryBlocks.add(dictionaryBlock);
        }
    }
    return outputDictionaryBlocks;
}
Also used : DictionaryId.randomDictionaryId(com.facebook.presto.spi.block.DictionaryId.randomDictionaryId) DictionaryId(com.facebook.presto.spi.block.DictionaryId) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) ArrayList(java.util.ArrayList) Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock)

Example 2 with DictionaryBlock

use of com.facebook.presto.spi.block.DictionaryBlock in project presto by prestodb.

the class Page method getRelatedDictionaryBlocks.

private Map<DictionaryId, DictionaryBlockIndexes> getRelatedDictionaryBlocks() {
    Map<DictionaryId, DictionaryBlockIndexes> relatedDictionaryBlocks = new HashMap<>();
    for (int i = 0; i < blocks.length; i++) {
        Block block = blocks[i];
        if (block instanceof DictionaryBlock) {
            DictionaryBlock dictionaryBlock = (DictionaryBlock) block;
            relatedDictionaryBlocks.computeIfAbsent(dictionaryBlock.getDictionarySourceId(), id -> new DictionaryBlockIndexes()).addBlock(dictionaryBlock, i);
        }
    }
    return relatedDictionaryBlocks;
}
Also used : DictionaryId.randomDictionaryId(com.facebook.presto.spi.block.DictionaryId.randomDictionaryId) AtomicLong(java.util.concurrent.atomic.AtomicLong) Arrays(java.util.Arrays) List(java.util.List) Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) DictionaryId(com.facebook.presto.spi.block.DictionaryId) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) HashMap(java.util.HashMap) Math.min(java.lang.Math.min) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) DictionaryId.randomDictionaryId(com.facebook.presto.spi.block.DictionaryId.randomDictionaryId) DictionaryId(com.facebook.presto.spi.block.DictionaryId) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock)

Example 3 with DictionaryBlock

use of com.facebook.presto.spi.block.DictionaryBlock in project presto by prestodb.

the class GenericPageProcessor method filterPage.

private int[] filterPage(Page page) {
    int[] selected = new int[page.getPositionCount()];
    int index = 0;
    if (filterFunction.getInputChannels().size() == 1) {
        int channel = getOnlyElement(filterFunction.getInputChannels());
        if (page.getBlock(channel) instanceof DictionaryBlock) {
            DictionaryBlock dictionaryBlock = (DictionaryBlock) page.getBlock(channel);
            Block dictionary = dictionaryBlock.getDictionary();
            Block[] blocks = new Block[page.getPositionCount()];
            blocks[channel] = dictionary;
            boolean[] selectedDictionaryPositions;
            if (inputFilterDictionary == dictionary) {
                selectedDictionaryPositions = filterResult;
            } else {
                selectedDictionaryPositions = new boolean[dictionary.getPositionCount()];
                for (int i = 0; i < dictionary.getPositionCount(); i++) {
                    selectedDictionaryPositions[i] = filterFunction.filter(i, blocks);
                }
                inputFilterDictionary = dictionary;
                filterResult = selectedDictionaryPositions;
            }
            for (int i = 0; i < page.getPositionCount(); i++) {
                if (selectedDictionaryPositions[dictionaryBlock.getId(i)]) {
                    selected[index] = i;
                    index++;
                }
            }
            return copyOf(selected, index);
        }
        if (page.getBlock(channel) instanceof RunLengthEncodedBlock) {
            RunLengthEncodedBlock rleBlock = (RunLengthEncodedBlock) page.getBlock(channel);
            Block[] blocks = new Block[page.getPositionCount()];
            blocks[channel] = rleBlock.getValue();
            if (filterFunction.filter(0, blocks)) {
                return IntStream.range(0, page.getPositionCount()).toArray();
            }
            return new int[0];
        }
    }
    Block[] blocks = page.getBlocks();
    for (int position = 0; position < page.getPositionCount(); position++) {
        if (filterFunction.filter(position, blocks)) {
            selected[index] = position;
            index++;
        }
    }
    return copyOf(selected, index);
}
Also used : DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) LazyBlock(com.facebook.presto.spi.block.LazyBlock) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock)

Example 4 with DictionaryBlock

use of com.facebook.presto.spi.block.DictionaryBlock in project presto by prestodb.

the class MultiChannelGroupByHash method createPageWithExtractedDictionary.

// For a page that contains DictionaryBlocks, create a new page in which
// the dictionaries from the DictionaryBlocks are extracted into the corresponding channels
// From Page(DictionaryBlock1, DictionaryBlock2) create new page with Page(dictionary1, dictionary2)
private Page createPageWithExtractedDictionary(Page page) {
    Block[] blocks = new Block[page.getChannelCount()];
    Block dictionary = ((DictionaryBlock) page.getBlock(channels[0])).getDictionary();
    // extract data dictionary
    blocks[channels[0]] = dictionary;
    // extract hash dictionary
    if (inputHashChannel.isPresent()) {
        blocks[inputHashChannel.get()] = ((DictionaryBlock) page.getBlock(inputHashChannel.get())).getDictionary();
    }
    return new Page(dictionary.getPositionCount(), blocks);
}
Also used : DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) Page(com.facebook.presto.spi.Page)

Example 5 with DictionaryBlock

use of com.facebook.presto.spi.block.DictionaryBlock in project presto by prestodb.

the class MultiChannelGroupByHash method processDictionary.

private Block processDictionary(Page page) {
    verify(canProcessDictionary(page), "invalid call to processDictionary");
    DictionaryBlock dictionaryBlock = (DictionaryBlock) page.getBlock(channels[0]);
    updateDictionaryLookBack(dictionaryBlock.getDictionary());
    Page dictionaryPage = createPageWithExtractedDictionary(page);
    BlockBuilder blockBuilder = BIGINT.createFixedSizeBlockBuilder(page.getPositionCount());
    for (int i = 0; i < page.getPositionCount(); i++) {
        int positionInDictionary = dictionaryBlock.getId(i);
        int groupId = getGroupId(hashGenerator, dictionaryPage, positionInDictionary);
        BIGINT.writeLong(blockBuilder, groupId);
    }
    verify(blockBuilder.getPositionCount() == page.getPositionCount(), "invalid position count");
    return blockBuilder.build();
}
Also used : DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) Page(com.facebook.presto.spi.Page) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Aggregations

DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)32 Block (com.facebook.presto.spi.block.Block)12 Test (org.testng.annotations.Test)12 Slice (io.airlift.slice.Slice)9 Page (com.facebook.presto.spi.Page)7 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)6 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)5 DictionaryId (com.facebook.presto.spi.block.DictionaryId)5 DictionaryId.randomDictionaryId (com.facebook.presto.spi.block.DictionaryId.randomDictionaryId)5 LazyBlock (com.facebook.presto.spi.block.LazyBlock)5 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)5 SliceArrayBlock (com.facebook.presto.spi.block.SliceArrayBlock)5 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)4 PageProcessor (com.facebook.presto.operator.PageProcessor)3 ExpressionCompiler (com.facebook.presto.sql.gen.ExpressionCompiler)3 ConstantExpression (com.facebook.presto.sql.relational.ConstantExpression)3 InputReferenceExpression (com.facebook.presto.sql.relational.InputReferenceExpression)3 Signature (com.facebook.presto.metadata.Signature)2 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)2 CallExpression (com.facebook.presto.sql.relational.CallExpression)2