Search in sources :

Example 1 with ArrayBlock

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

the class ListBatchStreamReader method readBlock.

@Override
public Block readBlock() throws IOException {
    if (!rowGroupOpen) {
        openRowGroup();
    }
    if (readOffset > 0) {
        if (presentStream != null) {
            // skip ahead the present bit reader, but count the set bits
            // and use this as the skip size for the data reader
            readOffset = presentStream.countBitsSet(readOffset);
        }
        if (readOffset > 0) {
            if (lengthStream == null) {
                throw new OrcCorruptionException(streamDescriptor.getOrcDataSourceId(), "Value is not null but data stream is not present");
            }
            long elementSkipSize = lengthStream.sum(readOffset);
            elementStreamReader.prepareNextRead(toIntExact(elementSkipSize));
        }
    }
    // We will use the offsetVector as the buffer to read the length values from lengthStream,
    // and the length values will be converted in-place to an offset vector.
    int[] offsetVector = new int[nextBatchSize + 1];
    boolean[] nullVector = null;
    if (presentStream == null) {
        if (lengthStream == null) {
            throw new OrcCorruptionException(streamDescriptor.getOrcDataSourceId(), "Value is not null but data stream is not present");
        }
        lengthStream.next(offsetVector, nextBatchSize);
    } else {
        nullVector = new boolean[nextBatchSize];
        int nullValues = presentStream.getUnsetBits(nextBatchSize, nullVector);
        if (nullValues != nextBatchSize) {
            if (lengthStream == null) {
                throw new OrcCorruptionException(streamDescriptor.getOrcDataSourceId(), "Value is not null but data stream is not present");
            }
            lengthStream.next(offsetVector, nextBatchSize - nullValues);
            unpackLengthNulls(offsetVector, nullVector, nextBatchSize - nullValues);
        }
    }
    convertLengthVectorToOffsetVector(offsetVector);
    int elementCount = offsetVector[offsetVector.length - 1];
    Block elements;
    if (elementCount > 0) {
        elementStreamReader.prepareNextRead(elementCount);
        elements = elementStreamReader.readBlock();
    } else {
        elements = elementType.createBlockBuilder(null, 0).build();
    }
    Block arrayBlock = ArrayBlock.fromElementBlock(nextBatchSize, Optional.ofNullable(nullVector), offsetVector, elements);
    readOffset = 0;
    nextBatchSize = 0;
    return arrayBlock;
}
Also used : ArrayBlock(com.facebook.presto.common.block.ArrayBlock) Block(com.facebook.presto.common.block.Block) OrcCorruptionException(com.facebook.presto.orc.OrcCorruptionException)

Example 2 with ArrayBlock

use of com.facebook.presto.common.block.ArrayBlock 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 ArrayBlock

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

the class ParquetReader method readArray.

private ColumnChunk readArray(GroupField field) throws IOException {
    List<Type> parameters = field.getType().getTypeParameters();
    checkArgument(parameters.size() == 1, "Arrays must have a single type parameter, found %d", parameters.size());
    Field elementField = field.getChildren().get(0).get();
    ColumnChunk columnChunk = readColumnChunk(elementField);
    IntList offsets = new IntArrayList();
    BooleanList valueIsNull = new BooleanArrayList();
    calculateCollectionOffsets(field, offsets, valueIsNull, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
    Block arrayBlock = ArrayBlock.fromElementBlock(valueIsNull.size(), Optional.of(valueIsNull.toBooleanArray()), offsets.toIntArray(), columnChunk.getBlock());
    return new ColumnChunk(arrayBlock, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
}
Also used : BooleanList(it.unimi.dsi.fastutil.booleans.BooleanList) GroupField(com.facebook.presto.parquet.GroupField) PrimitiveField(com.facebook.presto.parquet.PrimitiveField) Field(com.facebook.presto.parquet.Field) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) BooleanArrayList(it.unimi.dsi.fastutil.booleans.BooleanArrayList) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) RowBlock(com.facebook.presto.common.block.RowBlock) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) Block(com.facebook.presto.common.block.Block) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) IntList(it.unimi.dsi.fastutil.ints.IntList)

Example 4 with ArrayBlock

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

the class PrestoThriftBigintArray method fromBlock.

public static PrestoThriftBlock fromBlock(Block block) {
    checkArgument(block instanceof AbstractArrayBlock, "block is not of an array type");
    AbstractArrayBlock arrayBlock = (AbstractArrayBlock) block;
    int positions = arrayBlock.getPositionCount();
    if (positions == 0) {
        return bigintArrayData(new PrestoThriftBigintArray(null, null, null));
    }
    boolean[] nulls = null;
    int[] sizes = null;
    for (int position = 0; position < positions; position++) {
        if (arrayBlock.isNull(position)) {
            if (nulls == null) {
                nulls = new boolean[positions];
            }
            nulls[position] = true;
        } else {
            if (sizes == null) {
                sizes = new int[positions];
            }
            sizes[position] = arrayBlock.apply((valuesBlock, startPosition, length) -> length, position);
        }
    }
    PrestoThriftBigint values = arrayBlock.apply((valuesBlock, startPosition, length) -> PrestoThriftBigint.fromBlock(valuesBlock), 0).getBigintData();
    checkState(values != null, "values must be present");
    checkState(totalSize(nulls, sizes) == values.numberOfRecords(), "unexpected number of values");
    return bigintArrayData(new PrestoThriftBigintArray(nulls, sizes, values));
}
Also used : BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) Arrays(java.util.Arrays) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) AbstractArrayBlock(com.facebook.presto.common.block.AbstractArrayBlock) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) ThriftStruct(com.facebook.drift.annotations.ThriftStruct) ThriftConstructor(com.facebook.drift.annotations.ThriftConstructor) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) OPTIONAL(com.facebook.drift.annotations.ThriftField.Requiredness.OPTIONAL) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PrestoThriftTypeUtils.sameSizeIfPresent(com.facebook.presto.thrift.api.datatypes.PrestoThriftTypeUtils.sameSizeIfPresent) PrestoThriftBlock.bigintArrayData(com.facebook.presto.thrift.api.datatypes.PrestoThriftBlock.bigintArrayData) Optional(java.util.Optional) PrestoThriftTypeUtils.totalSize(com.facebook.presto.thrift.api.datatypes.PrestoThriftTypeUtils.totalSize) ThriftField(com.facebook.drift.annotations.ThriftField) Block(com.facebook.presto.common.block.Block) PrestoThriftTypeUtils.calculateOffsets(com.facebook.presto.thrift.api.datatypes.PrestoThriftTypeUtils.calculateOffsets) Type(com.facebook.presto.common.type.Type) Nullable(javax.annotation.Nullable) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) AbstractArrayBlock(com.facebook.presto.common.block.AbstractArrayBlock)

Aggregations

ArrayBlock (com.facebook.presto.common.block.ArrayBlock)4 Block (com.facebook.presto.common.block.Block)4 LongArrayBlock (com.facebook.presto.common.block.LongArrayBlock)2 RowBlock (com.facebook.presto.common.block.RowBlock)2 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)2 Type (com.facebook.presto.common.type.Type)2 ThriftConstructor (com.facebook.drift.annotations.ThriftConstructor)1 ThriftField (com.facebook.drift.annotations.ThriftField)1 OPTIONAL (com.facebook.drift.annotations.ThriftField.Requiredness.OPTIONAL)1 ThriftStruct (com.facebook.drift.annotations.ThriftStruct)1 AbstractArrayBlock (com.facebook.presto.common.block.AbstractArrayBlock)1 BlockLease (com.facebook.presto.common.block.BlockLease)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 DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)1 IntArrayBlock (com.facebook.presto.common.block.IntArrayBlock)1 MapBlock (com.facebook.presto.common.block.MapBlock)1 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)1 MapType (com.facebook.presto.common.type.MapType)1