Search in sources :

Example 1 with BlockLease

use of com.facebook.presto.common.block.BlockLease in project presto by prestodb.

the class OrcSelectiveRecordReader method applyFilterFunctions.

private int applyFilterFunctions(List<FilterFunctionWithStats> filterFunctions, Set<Integer> filterFunctionInputs, int[] positions, int positionCount) {
    BlockLease[] blockLeases = new BlockLease[hiveColumnIndices.length];
    Block[] blocks = new Block[hiveColumnIndices.length];
    for (int columnIndex : filterFunctionInputs) {
        if (constantValues[columnIndex] != null) {
            blocks[columnIndex] = RunLengthEncodedBlock.create(columnTypes.get(columnIndex), constantValues[columnIndex] == NULL_MARKER ? null : constantValues[columnIndex], positionCount);
        } else {
            blockLeases[columnIndex] = getStreamReader(columnIndex).getBlockView(positions, positionCount);
            Block block = blockLeases[columnIndex].get();
            if (coercers[columnIndex] != null) {
                block = coercers[columnIndex].apply(block);
            }
            blocks[columnIndex] = block;
        }
    }
    initializeTmpErrors(positionCount);
    for (int i = 0; i < positionCount; i++) {
        tmpErrors[i] = errors[positions[i]];
    }
    Arrays.fill(errors, null);
    try {
        initializeOutputPositions(positionCount);
        for (int i = 0; i < filterFunctions.size(); i++) {
            FilterFunctionWithStats functionWithStats = filterFunctions.get(i);
            FilterFunction function = functionWithStats.getFunction();
            int[] inputs = function.getInputChannels();
            Block[] inputBlocks = new Block[inputs.length];
            for (int j = 0; j < inputs.length; j++) {
                inputBlocks[j] = blocks[filterFunctionInputMapping.get(inputs[j])];
            }
            Page page = new Page(positionCount, inputBlocks);
            long startTime = System.nanoTime();
            int inputPositionCount = positionCount;
            positionCount = function.filter(page, outputPositions, positionCount, tmpErrors);
            functionWithStats.getStats().update(inputPositionCount, positionCount, System.nanoTime() - startTime);
            if (positionCount == 0) {
                break;
            }
        }
        // e.g. make outputPositions a subset of positions array
        for (int i = 0; i < positionCount; i++) {
            outputPositions[i] = positions[outputPositions[i]];
            errors[outputPositions[i]] = tmpErrors[i];
        }
        return positionCount;
    } finally {
        for (BlockLease blockLease : blockLeases) {
            if (blockLease != null) {
                blockLease.close();
            }
        }
    }
}
Also used : FilterFunction(com.facebook.presto.common.predicate.FilterFunction) BlockLease(com.facebook.presto.common.block.BlockLease) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) LazyBlock(com.facebook.presto.common.block.LazyBlock) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page)

Example 2 with BlockLease

use of com.facebook.presto.common.block.BlockLease in project presto by prestodb.

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 3 with BlockLease

use of com.facebook.presto.common.block.BlockLease in project presto by prestodb.

the class MapFlatSelectiveStreamReader method assembleMapBlock.

private Block assembleMapBlock(boolean includeNulls) {
    offsets = ensureCapacity(offsets, outputPositionCount + 1);
    offsets[0] = 0;
    int offset = 0;
    int inMapIndex = 0;
    for (int i = 0; i < outputPositionCount; i++) {
        if (!includeNulls || !nulls[i]) {
            offset += nestedLengths[inMapIndex];
            inMapIndex++;
        }
        offsets[i + 1] = offset;
    }
    BlockLease[] valueBlockLeases = new BlockLease[keyCount];
    Block[] valueBlocks = new Block[keyCount];
    for (int i = 0; i < keyCount; i++) {
        if (nestedPositionCounts[i] > 0) {
            valueBlockLeases[i] = valueStreamReaders.get(i).getBlockView(nestedOutputPositions[i], nestedPositionCounts[i]);
            valueBlocks[i] = valueBlockLeases[i].get();
        } else {
            valueBlocks[i] = outputType.getKeyType().createBlockBuilder(null, 0).build();
        }
    }
    int[] keyIds = new int[offset];
    int count = 0;
    Type valueType = outputType.getValueType();
    BlockBuilder valueBlockBuilder;
    if (valueType instanceof FixedWidthType) {
        valueBlockBuilder = ((FixedWidthType) valueType).createFixedSizeBlockBuilder(offset);
    } else {
        valueBlockBuilder = valueType.createBlockBuilder(null, offset);
    }
    int[] valueBlockPositions = new int[keyCount];
    inMapIndex = 0;
    for (int i = 0; i < outputPositionCount; i++) {
        if (includeNulls && nulls[i]) {
            continue;
        }
        for (int keyIndex = 0; keyIndex < keyCount; keyIndex++) {
            if (inMap[keyIndex][inMapIndex]) {
                valueType.appendTo(valueBlocks[keyIndex], valueBlockPositions[keyIndex], valueBlockBuilder);
                valueBlockPositions[keyIndex]++;
                keyIds[count++] = keyIndex;
            }
        }
        inMapIndex++;
    }
    for (int i = 0; i < keyCount; i++) {
        if (valueBlockLeases[i] != null) {
            valueBlockLeases[i].close();
        }
    }
    return outputType.createBlockFromKeyValue(outputPositionCount, Optional.ofNullable(includeNulls ? nulls : null), offsets, new DictionaryBlock(keyBlock, keyIds), valueBlockBuilder.build());
}
Also used : TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) SmallintType(com.facebook.presto.common.type.SmallintType) MapType(com.facebook.presto.common.type.MapType) IntegerType(com.facebook.presto.common.type.IntegerType) Type(com.facebook.presto.common.type.Type) FixedWidthType(com.facebook.presto.common.type.FixedWidthType) BlockLease(com.facebook.presto.common.block.BlockLease) ClosingBlockLease(com.facebook.presto.common.block.ClosingBlockLease) 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) VariableWidthBlockBuilder(com.facebook.presto.common.block.VariableWidthBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) FixedWidthType(com.facebook.presto.common.type.FixedWidthType)

Example 4 with BlockLease

use of com.facebook.presto.common.block.BlockLease in project presto by prestodb.

the class ListSelectiveStreamReader method getBlockView.

@Override
public BlockLease getBlockView(int[] positions, int positionCount) {
    checkArgument(outputPositionCount > 0, "outputPositionCount must be greater than zero");
    checkState(outputRequired, "This stream reader doesn't produce output");
    checkState(positionCount <= outputPositionCount, "Not enough values");
    checkState(!valuesInUse, "BlockLease hasn't been closed yet");
    if (allNulls) {
        return newLease(new RunLengthEncodedBlock(outputType.createBlockBuilder(null, 1).appendNull().build(), positionCount));
    }
    boolean includeNulls = nullsAllowed && presentStream != null;
    if (positionCount != outputPositionCount) {
        compactValues(positions, positionCount, includeNulls);
    }
    BlockLease elementBlockLease;
    if (elementOutputPositionCount == 0) {
        elementBlockLease = newLease(outputType.getElementType().createBlockBuilder(null, 0).build());
    } else {
        elementBlockLease = elementStreamReader.getBlockView(elementOutputPositions, elementOutputPositionCount);
    }
    valuesInUse = true;
    Block block = ArrayBlock.fromElementBlock(positionCount, Optional.ofNullable(includeNulls ? nulls : null), offsets, elementBlockLease.get());
    return newLease(block, () -> closeBlockLease(elementBlockLease));
}
Also used : BlockLease(com.facebook.presto.common.block.BlockLease) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) Block(com.facebook.presto.common.block.Block) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock)

Example 5 with BlockLease

use of com.facebook.presto.common.block.BlockLease in project presto by prestodb.

the class MapDirectSelectiveStreamReader method getBlockView.

@Override
public BlockLease getBlockView(int[] positions, int positionCount) {
    checkArgument(outputPositionCount > 0, "outputPositionCount must be greater than zero");
    checkState(outputRequired, "This stream reader doesn't produce output");
    checkState(positionCount <= outputPositionCount, "Not enough values");
    checkState(!valuesInUse, "BlockLease hasn't been closed yet");
    if (allNulls) {
        return newLease(createNullBlock(outputType, positionCount));
    }
    boolean includeNulls = nullsAllowed && presentStream != null;
    if (positionCount != outputPositionCount) {
        compactValues(positions, positionCount, includeNulls);
    }
    if (nestedOutputPositionCount == 0) {
        return newLease(outputType.createBlockFromKeyValue(positionCount, Optional.ofNullable(includeNulls ? nulls : null), offsets, createEmptyBlock(outputType.getKeyType()), createEmptyBlock(outputType.getValueType())));
    }
    BlockLease keyBlockLease = keyReader.getBlockView(nestedOutputPositions, nestedOutputPositionCount);
    BlockLease valueBlockLease = valueReader.getBlockView(nestedOutputPositions, nestedOutputPositionCount);
    return newLease(outputType.createBlockFromKeyValue(positionCount, Optional.ofNullable(includeNulls ? nulls : null), offsets, keyBlockLease.get(), valueBlockLease.get()), keyBlockLease, valueBlockLease);
}
Also used : BlockLease(com.facebook.presto.common.block.BlockLease) ClosingBlockLease(com.facebook.presto.common.block.ClosingBlockLease)

Aggregations

BlockLease (com.facebook.presto.common.block.BlockLease)5 Block (com.facebook.presto.common.block.Block)4 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)4 ArrayBlock (com.facebook.presto.common.block.ArrayBlock)2 ClosingBlockLease (com.facebook.presto.common.block.ClosingBlockLease)2 DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)2 Page (com.facebook.presto.common.Page)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 ColumnarArray (com.facebook.presto.common.block.ColumnarArray)1 ColumnarMap (com.facebook.presto.common.block.ColumnarMap)1 ColumnarRow (com.facebook.presto.common.block.ColumnarRow)1 LazyBlock (com.facebook.presto.common.block.LazyBlock)1 LongArrayBlock (com.facebook.presto.common.block.LongArrayBlock)1 MapBlock (com.facebook.presto.common.block.MapBlock)1 RowBlock (com.facebook.presto.common.block.RowBlock)1 VariableWidthBlockBuilder (com.facebook.presto.common.block.VariableWidthBlockBuilder)1 FilterFunction (com.facebook.presto.common.predicate.FilterFunction)1 BigintType (com.facebook.presto.common.type.BigintType)1 FixedWidthType (com.facebook.presto.common.type.FixedWidthType)1 IntegerType (com.facebook.presto.common.type.IntegerType)1