Search in sources :

Example 1 with ArrayBlock

use of io.trino.spi.block.ArrayBlock in project trino by trinodb.

the class ListColumnReader 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(column.getOrcDataSourceId(), "Value is not null but data stream is not present");
            }
            long elementSkipSize = lengthStream.sum(readOffset);
            elementColumnReader.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(column.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(column.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) {
        elementColumnReader.prepareNextRead(elementCount);
        elements = blockFactory.createBlock(elementCount, elementColumnReader::readBlock, true);
    } 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 : Block(io.trino.spi.block.Block) ArrayBlock(io.trino.spi.block.ArrayBlock) OrcCorruptionException(io.trino.orc.OrcCorruptionException)

Example 2 with ArrayBlock

use of io.trino.spi.block.ArrayBlock in project trino by trinodb.

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 %s", 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(io.trino.parquet.GroupField) PrimitiveField(io.trino.parquet.PrimitiveField) Field(io.trino.parquet.Field) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) MapType(io.trino.spi.type.MapType) BooleanArrayList(it.unimi.dsi.fastutil.booleans.BooleanArrayList) Block(io.trino.spi.block.Block) RunLengthEncodedBlock(io.trino.spi.block.RunLengthEncodedBlock) ArrayBlock(io.trino.spi.block.ArrayBlock) RowBlock(io.trino.spi.block.RowBlock) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) IntList(it.unimi.dsi.fastutil.ints.IntList)

Example 3 with ArrayBlock

use of io.trino.spi.block.ArrayBlock in project trino by trinodb.

the class TrinoThriftBigintArray method fromBlock.

public static TrinoThriftBlock 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 TrinoThriftBigintArray(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);
        }
    }
    TrinoThriftBigint values = arrayBlock.apply((valuesBlock, startPosition, length) -> TrinoThriftBigint.fromBlock(valuesBlock), 0).getBigintData();
    checkState(values != null, "values must be present");
    checkState(totalSize(nulls, sizes) == values.numberOfRecords(), "unexpected number of values");
    return bigintArrayData(new TrinoThriftBigintArray(nulls, sizes, values));
}
Also used : TrinoThriftBlock(io.trino.plugin.thrift.api.TrinoThriftBlock) Arrays(java.util.Arrays) ThriftField(io.airlift.drift.annotations.ThriftField) TrinoThriftTypeUtils.sameSizeIfPresent(io.trino.plugin.thrift.api.datatypes.TrinoThriftTypeUtils.sameSizeIfPresent) Type(io.trino.spi.type.Type) TrinoThriftBlock.bigintArrayData(io.trino.plugin.thrift.api.TrinoThriftBlock.bigintArrayData) OPTIONAL(io.airlift.drift.annotations.ThriftField.Requiredness.OPTIONAL) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TrinoThriftTypeUtils.calculateOffsets(io.trino.plugin.thrift.api.datatypes.TrinoThriftTypeUtils.calculateOffsets) BIGINT(io.trino.spi.type.BigintType.BIGINT) Block(io.trino.spi.block.Block) TrinoThriftTypeUtils.totalSize(io.trino.plugin.thrift.api.datatypes.TrinoThriftTypeUtils.totalSize) ThriftConstructor(io.airlift.drift.annotations.ThriftConstructor) Optional(java.util.Optional) ThriftStruct(io.airlift.drift.annotations.ThriftStruct) LongArrayBlock(io.trino.spi.block.LongArrayBlock) AbstractArrayBlock(io.trino.spi.block.AbstractArrayBlock) ArrayBlock(io.trino.spi.block.ArrayBlock) Nullable(javax.annotation.Nullable) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) AbstractArrayBlock(io.trino.spi.block.AbstractArrayBlock)

Aggregations

ArrayBlock (io.trino.spi.block.ArrayBlock)3 Block (io.trino.spi.block.Block)3 Type (io.trino.spi.type.Type)2 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ThriftConstructor (io.airlift.drift.annotations.ThriftConstructor)1 ThriftField (io.airlift.drift.annotations.ThriftField)1 OPTIONAL (io.airlift.drift.annotations.ThriftField.Requiredness.OPTIONAL)1 ThriftStruct (io.airlift.drift.annotations.ThriftStruct)1 OrcCorruptionException (io.trino.orc.OrcCorruptionException)1 Field (io.trino.parquet.Field)1 GroupField (io.trino.parquet.GroupField)1 PrimitiveField (io.trino.parquet.PrimitiveField)1 TrinoThriftBlock (io.trino.plugin.thrift.api.TrinoThriftBlock)1 TrinoThriftBlock.bigintArrayData (io.trino.plugin.thrift.api.TrinoThriftBlock.bigintArrayData)1 TrinoThriftTypeUtils.calculateOffsets (io.trino.plugin.thrift.api.datatypes.TrinoThriftTypeUtils.calculateOffsets)1 TrinoThriftTypeUtils.sameSizeIfPresent (io.trino.plugin.thrift.api.datatypes.TrinoThriftTypeUtils.sameSizeIfPresent)1 TrinoThriftTypeUtils.totalSize (io.trino.plugin.thrift.api.datatypes.TrinoThriftTypeUtils.totalSize)1 AbstractArrayBlock (io.trino.spi.block.AbstractArrayBlock)1