Search in sources :

Example 56 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project urban-eureka by errir503.

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, true, newDictionaryId));
        } catch (UnsupportedOperationException e) {
            // ignore if copy positions is not supported for the dictionary
            outputDictionaryBlocks.add(dictionaryBlock);
        }
    }
    return outputDictionaryBlocks;
}
Also used : DictionaryId(com.facebook.presto.common.block.DictionaryId) DictionaryId.randomDictionaryId(com.facebook.presto.common.block.DictionaryId.randomDictionaryId) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) ArrayList(java.util.ArrayList) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block)

Example 57 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project urban-eureka by errir503.

the class AbstractBlockEncodingBuffer method mapPositionsToNestedBlock.

/**
 * Map the positions for DictionaryBlock to its nested dictionaryBlock, and positions for RunLengthEncodedBlock
 * to its nested value block. For example, positions [0, 2, 5] on DictionaryBlock with dictionary block ['a', 'b', 'c']
 * and ids [1, 1, 2, 0, 2, 1] will be mapped to [1, 2, 1], and the copied data will be ['b', 'c', 'b'].
 */
protected DecodedBlockNode mapPositionsToNestedBlock(DecodedBlockNode decodedBlockNode) {
    Object decodedObject = decodedBlockNode.getDecodedBlock();
    if (decodedObject instanceof DictionaryBlock) {
        DictionaryBlock dictionaryBlock = (DictionaryBlock) decodedObject;
        mappedPositions = ensureCapacity(mappedPositions, positionCount, SMALL, NONE, bufferAllocator);
        for (int i = 0; i < positionCount; i++) {
            mappedPositions[i] = dictionaryBlock.getId(positions[i]);
        }
        positionsMapped = true;
        return decodedBlockNode.getChildren().get(0);
    }
    if (decodedObject instanceof RunLengthEncodedBlock) {
        mappedPositions = ensureCapacity(mappedPositions, positionCount, SMALL, INITIALIZE, bufferAllocator);
        positionsMapped = true;
        return decodedBlockNode.getChildren().get(0);
    }
    positionsMapped = false;
    return decodedBlockNode;
}
Also used : DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock)

Example 58 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project urban-eureka by errir503.

the class OptimizedPartitionedOutputOperator method decodeBlock.

/**
 * Flatten the block and convert the nested-typed block into ColumnarArray/Map/Row.
 * For performance considerations we decode the block only once for each block instead of for each batch.
 *
 * @return A tree structure that contains the decoded block
 */
@VisibleForTesting
static DecodedBlockNode decodeBlock(BlockFlattener flattener, Closer blockLeaseCloser, Block block) {
    BlockLease lease = flattener.flatten(block);
    blockLeaseCloser.register(lease::close);
    Block decodedBlock = lease.get();
    long estimatedSizeInBytes = decodedBlock.getLogicalSizeInBytes();
    if (decodedBlock instanceof ArrayBlock) {
        ColumnarArray columnarArray = ColumnarArray.toColumnarArray(decodedBlock);
        Block childBlock = columnarArray.getElementsBlock();
        return new DecodedBlockNode(columnarArray, ImmutableList.of(decodeBlock(flattener, blockLeaseCloser, childBlock)), columnarArray.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    if (decodedBlock instanceof MapBlock) {
        ColumnarMap columnarMap = ColumnarMap.toColumnarMap(decodedBlock);
        Block keyBlock = columnarMap.getKeysBlock();
        Block valueBlock = columnarMap.getValuesBlock();
        return new DecodedBlockNode(columnarMap, ImmutableList.of(decodeBlock(flattener, blockLeaseCloser, keyBlock), decodeBlock(flattener, blockLeaseCloser, valueBlock)), columnarMap.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    if (decodedBlock instanceof RowBlock) {
        ColumnarRow columnarRow = ColumnarRow.toColumnarRow(decodedBlock);
        ImmutableList.Builder<DecodedBlockNode> children = ImmutableList.builder();
        for (int i = 0; i < columnarRow.getFieldCount(); i++) {
            Block childBlock = columnarRow.getField(i);
            children.add(decodeBlock(flattener, blockLeaseCloser, childBlock));
        }
        return new DecodedBlockNode(columnarRow, children.build(), columnarRow.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    if (decodedBlock instanceof DictionaryBlock) {
        Block dictionary = ((DictionaryBlock) decodedBlock).getDictionary();
        return new DecodedBlockNode(decodedBlock, ImmutableList.of(decodeBlock(flattener, blockLeaseCloser, dictionary)), decodedBlock.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    if (decodedBlock instanceof RunLengthEncodedBlock) {
        Block childBlock = ((RunLengthEncodedBlock) decodedBlock).getValue();
        return new DecodedBlockNode(decodedBlock, ImmutableList.of(decodeBlock(flattener, blockLeaseCloser, childBlock)), decodedBlock.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    return new DecodedBlockNode(decodedBlock, ImmutableList.of(), block.getRetainedSizeInBytes(), estimatedSizeInBytes);
}
Also used : ColumnarArray(com.facebook.presto.common.block.ColumnarArray) BlockLease(com.facebook.presto.common.block.BlockLease) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) ImmutableList(com.google.common.collect.ImmutableList) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) RowBlock(com.facebook.presto.common.block.RowBlock) ColumnarRow(com.facebook.presto.common.block.ColumnarRow) ColumnarMap(com.facebook.presto.common.block.ColumnarMap) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) MapBlock(com.facebook.presto.common.block.MapBlock) RowBlock(com.facebook.presto.common.block.RowBlock) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block) MapBlock(com.facebook.presto.common.block.MapBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 59 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project urban-eureka by errir503.

the class DictionaryAwarePageFilter method filter.

@Override
public SelectedPositions filter(SqlFunctionProperties properties, Page page) {
    Block block = page.getBlock(0).getLoadedBlock();
    if (block instanceof RunLengthEncodedBlock) {
        Block value = ((RunLengthEncodedBlock) block).getValue();
        Optional<boolean[]> selectedPosition = processDictionary(properties, value);
        // in that case we fallback and process again so the correct error message sent
        if (selectedPosition.isPresent()) {
            return SelectedPositions.positionsRange(0, selectedPosition.get()[0] ? page.getPositionCount() : 0);
        }
    }
    if (block instanceof DictionaryBlock) {
        DictionaryBlock dictionaryBlock = (DictionaryBlock) block;
        // Attempt to process the dictionary.  If dictionary is processing has not been considered effective, an empty response will be returned
        Optional<boolean[]> selectedDictionaryPositions = processDictionary(properties, dictionaryBlock.getDictionary());
        // record the usage count regardless of dictionary processing choice, so we have stats for next time
        lastDictionaryUsageCount += page.getPositionCount();
        // if dictionary was processed, produce a dictionary block; otherwise do normal processing
        if (selectedDictionaryPositions.isPresent()) {
            return selectDictionaryPositions(dictionaryBlock, selectedDictionaryPositions.get());
        }
    }
    return filter.filter(properties, new Page(block));
}
Also used : DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock)

Example 60 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project urban-eureka by errir503.

the class MultiChannelGroupByHash method canProcessDictionary.

private boolean canProcessDictionary(Page page) {
    if (!this.processDictionary || channels.length > 1 || !(page.getBlock(channels[0]) instanceof DictionaryBlock)) {
        return false;
    }
    if (inputHashChannel.isPresent()) {
        Block inputHashBlock = page.getBlock(inputHashChannel.get());
        DictionaryBlock inputDataBlock = (DictionaryBlock) page.getBlock(channels[0]);
        if (!(inputHashBlock instanceof DictionaryBlock)) {
            // data channel is dictionary encoded but hash channel is not
            return false;
        }
        if (!((DictionaryBlock) inputHashBlock).getDictionarySourceId().equals(inputDataBlock.getDictionarySourceId())) {
            // dictionarySourceIds of data block and hash block do not match
            return false;
        }
    }
    return true;
}
Also used : DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block)

Aggregations

DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)125 Test (org.testng.annotations.Test)66 Block (com.facebook.presto.common.block.Block)65 BlockAssertions.createRandomDictionaryBlock (com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock)32 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)28 Slice (io.airlift.slice.Slice)26 Page (com.facebook.presto.common.Page)25 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)22 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)22 BlockAssertions.createLongSequenceBlock (com.facebook.presto.block.BlockAssertions.createLongSequenceBlock)14 LongArrayBlock (com.facebook.presto.common.block.LongArrayBlock)14 DictionaryId (com.facebook.presto.common.block.DictionaryId)12 LazyBlock (com.facebook.presto.common.block.LazyBlock)12 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)10 BlockAssertions.createRLEBlock (com.facebook.presto.block.BlockAssertions.createRLEBlock)10 BlockAssertions.createRandomLongsBlock (com.facebook.presto.block.BlockAssertions.createRandomLongsBlock)10 BlockAssertions.createSlicesBlock (com.facebook.presto.block.BlockAssertions.createSlicesBlock)10 DictionaryId.randomDictionaryId (com.facebook.presto.common.block.DictionaryId.randomDictionaryId)10 IntArrayBlock (com.facebook.presto.common.block.IntArrayBlock)8 Type (com.facebook.presto.common.type.Type)8