Search in sources :

Example 1 with DictionaryId

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

the class GenericPageProcessor method processColumnarDictionary.

@Override
public Page processColumnarDictionary(ConnectorSession session, Page page, List<? extends Type> types) {
    Page inputPage = getNonLazyPage(page);
    int[] selectedPositions = filterPage(inputPage);
    Map<DictionaryId, DictionaryId> dictionarySourceIds = new HashMap<>();
    if (selectedPositions.length == 0) {
        return null;
    }
    if (projections.isEmpty()) {
        return new Page(selectedPositions.length);
    }
    PageBuilder pageBuilder = new PageBuilder(types);
    Block[] inputBlocks = page.getBlocks();
    Block[] outputBlocks = new Block[projections.size()];
    for (int projectionIndex = 0; projectionIndex < projections.size(); projectionIndex++) {
        ProjectionFunction projection = projections.get(projectionIndex);
        if (canDictionaryProcess(projection, inputPage)) {
            outputBlocks[projectionIndex] = projectColumnarDictionary(inputPage, selectedPositions, projection, dictionarySourceIds);
        } else {
            outputBlocks[projectionIndex] = projectColumnar(selectedPositions, pageBuilder.getBlockBuilder(projectionIndex), inputBlocks, projection).build();
        }
    }
    for (Block block : outputBlocks) {
        verify(block.getPositionCount() == selectedPositions.length);
    }
    return new Page(selectedPositions.length, outputBlocks);
}
Also used : HashMap(java.util.HashMap) DictionaryId.randomDictionaryId(com.facebook.presto.spi.block.DictionaryId.randomDictionaryId) DictionaryId(com.facebook.presto.spi.block.DictionaryId) 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) Page(com.facebook.presto.spi.Page) PageBuilder(com.facebook.presto.spi.PageBuilder)

Example 2 with DictionaryId

use of com.facebook.presto.spi.block.DictionaryId 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 3 with DictionaryId

use of com.facebook.presto.spi.block.DictionaryId 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 4 with DictionaryId

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

the class GenericPageProcessor method projectColumnarDictionary.

private Block projectColumnarDictionary(Page inputPage, int[] selectedPositions, ProjectionFunction projection, Map<DictionaryId, DictionaryId> dictionarySourceIds) {
    int inputChannel = getOnlyElement(projection.getInputChannels());
    Block[] blocks = new Block[inputPage.getChannelCount()];
    if (inputPage.getBlock(inputChannel) instanceof RunLengthEncodedBlock) {
        RunLengthEncodedBlock rleBlock = (RunLengthEncodedBlock) inputPage.getBlock(inputChannel);
        BlockBuilder builder = projection.getType().createBlockBuilder(new BlockBuilderStatus(), 1);
        blocks[inputChannel] = rleBlock.getValue();
        projection.project(0, blocks, builder);
        return new RunLengthEncodedBlock(builder.build(), selectedPositions.length);
    }
    Block outputDictionary = projectDictionary(projection, inputPage);
    int[] outputIds = filterIds(projection, inputPage, selectedPositions);
    DictionaryBlock dictionaryBlock = (DictionaryBlock) inputPage.getBlock(inputChannel);
    DictionaryId sourceId = dictionarySourceIds.get(dictionaryBlock.getDictionarySourceId());
    if (sourceId == null) {
        sourceId = randomDictionaryId();
        dictionarySourceIds.put(dictionaryBlock.getDictionarySourceId(), sourceId);
    }
    return new DictionaryBlock(selectedPositions.length, outputDictionary, outputIds, false, sourceId);
}
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) 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) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 5 with DictionaryId

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

the class TestPage method testCompactDictionaryBlocks.

@Test
public void testCompactDictionaryBlocks() throws Exception {
    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);
    SliceArrayBlock dictionary1 = new SliceArrayBlock(dictionaryValues1.length, dictionaryValues1);
    DictionaryBlock commonSourceIdBlock1 = new DictionaryBlock(positionCount, dictionary1, commonDictionaryIds, commonSourceId);
    // second dictionary block is "length(firstColumn)"
    BlockBuilder dictionary2 = BIGINT.createBlockBuilder(new BlockBuilderStatus(), 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);
    SliceArrayBlock dictionary3 = new SliceArrayBlock(70, createExpectedValues(70));
    DictionaryBlock randomSourceIdBlock = new DictionaryBlock(positionCount, 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.randomDictionaryId(com.facebook.presto.spi.block.DictionaryId.randomDictionaryId) DictionaryId(com.facebook.presto.spi.block.DictionaryId) Slice(io.airlift.slice.Slice) SliceArrayBlock(com.facebook.presto.spi.block.SliceArrayBlock) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Aggregations

DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)6 DictionaryId (com.facebook.presto.spi.block.DictionaryId)6 DictionaryId.randomDictionaryId (com.facebook.presto.spi.block.DictionaryId.randomDictionaryId)6 Block (com.facebook.presto.spi.block.Block)5 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)2 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)2 LazyBlock (com.facebook.presto.spi.block.LazyBlock)2 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Page (com.facebook.presto.spi.Page)1 PageBuilder (com.facebook.presto.spi.PageBuilder)1 SliceArrayBlock (com.facebook.presto.spi.block.SliceArrayBlock)1 Slice (io.airlift.slice.Slice)1 Math.min (java.lang.Math.min)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1