Search in sources :

Example 1 with ArrayBlock

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

the class ParquetReader method readMap.

private Block readMap(Type type, List<String> path, IntList elementOffsets) throws IOException {
    List<Type> parameters = type.getTypeParameters();
    checkArgument(parameters.size() == 2, "Maps must have two type parameters, found %d", parameters.size());
    Block[] blocks = new Block[parameters.size()];
    IntList keyOffsets = new IntArrayList();
    IntList valueOffsets = new IntArrayList();
    path.add(MAP_TYPE_NAME);
    blocks[0] = readBlock(MAP_KEY_NAME, parameters.get(0), path, keyOffsets);
    blocks[1] = readBlock(MAP_VALUE_NAME, parameters.get(1), path, valueOffsets);
    path.remove(MAP_TYPE_NAME);
    if (blocks[0].getPositionCount() == 0) {
        for (int i = 0; i < batchSize; i++) {
            elementOffsets.add(0);
        }
        return RunLengthEncodedBlock.create(parameters.get(0), null, batchSize);
    }
    InterleavedBlock interleavedBlock = new InterleavedBlock(new Block[] { blocks[0], blocks[1] });
    int[] offsets = new int[batchSize + 1];
    for (int i = 1; i < offsets.length; i++) {
        int elementPositionCount = keyOffsets.getInt(i - 1) * 2;
        elementOffsets.add(elementPositionCount);
        offsets[i] = offsets[i - 1] + elementPositionCount;
    }
    return new ArrayBlock(batchSize, new boolean[batchSize], offsets, interleavedBlock);
}
Also used : Type(com.facebook.presto.spi.type.Type) MessageType(parquet.schema.MessageType) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) Block(com.facebook.presto.spi.block.Block) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) IntList(it.unimi.dsi.fastutil.ints.IntList)

Example 2 with ArrayBlock

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

the class ListStreamReader method readBlock.

@Override
public Block readBlock(Type type) 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("Value is not null but data stream is not present");
            }
            long elementSkipSize = lengthStream.sum(readOffset);
            elementStreamReader.prepareNextRead(toIntExact(elementSkipSize));
        }
    }
    // The length vector could be reused, but this simplifies the code below by
    // taking advantage of null entries being initialized to zero.  The vector
    // could be reinitialized for each loop, but that is likely just as expensive
    // as allocating a new array
    int[] lengthVector = new int[nextBatchSize];
    boolean[] nullVector = new boolean[nextBatchSize];
    if (presentStream == null) {
        if (lengthStream == null) {
            throw new OrcCorruptionException("Value is not null but data stream is not present");
        }
        lengthStream.nextIntVector(nextBatchSize, lengthVector);
    } else {
        int nullValues = presentStream.getUnsetBits(nextBatchSize, nullVector);
        if (nullValues != nextBatchSize) {
            if (lengthStream == null) {
                throw new OrcCorruptionException("Value is not null but data stream is not present");
            }
            lengthStream.nextIntVector(nextBatchSize, lengthVector, nullVector);
        }
    }
    int[] offsets = new int[nextBatchSize + 1];
    for (int i = 1; i < offsets.length; i++) {
        int length = lengthVector[i - 1];
        offsets[i] = offsets[i - 1] + length;
    }
    Type elementType = type.getTypeParameters().get(0);
    int elementCount = offsets[offsets.length - 1];
    Block elements;
    if (elementCount > 0) {
        elementStreamReader.prepareNextRead(elementCount);
        elements = elementStreamReader.readBlock(elementType);
    } else {
        elements = elementType.createBlockBuilder(new BlockBuilderStatus(), 0).build();
    }
    ArrayBlock arrayBlock = new ArrayBlock(nextBatchSize, nullVector, offsets, elements);
    readOffset = 0;
    nextBatchSize = 0;
    return arrayBlock;
}
Also used : Type(com.facebook.presto.spi.type.Type) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) Block(com.facebook.presto.spi.block.Block) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) OrcCorruptionException(com.facebook.presto.orc.OrcCorruptionException) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 3 with ArrayBlock

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

the class StructStreamReader method readBlock.

@Override
public Block readBlock(Type type) 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 field readers
            readOffset = presentStream.countBitsSet(readOffset);
        }
        for (StreamReader structField : structFields) {
            structField.prepareNextRead(readOffset);
        }
    }
    List<Type> typeParameters = type.getTypeParameters();
    boolean[] nullVector = new boolean[nextBatchSize];
    Block[] blocks = new Block[typeParameters.size()];
    if (presentStream == null) {
        for (int i = 0; i < typeParameters.size(); i++) {
            StreamReader structField = structFields[i];
            structField.prepareNextRead(nextBatchSize);
            blocks[i] = structField.readBlock(typeParameters.get(i));
        }
    } else {
        int nullValues = presentStream.getUnsetBits(nextBatchSize, nullVector);
        if (nullValues != nextBatchSize) {
            for (int i = 0; i < typeParameters.size(); i++) {
                StreamReader structField = structFields[i];
                structField.prepareNextRead(nextBatchSize - nullValues);
                blocks[i] = structField.readBlock(typeParameters.get(i));
            }
        } else {
            for (int i = 0; i < typeParameters.size(); i++) {
                blocks[i] = typeParameters.get(i).createBlockBuilder(new BlockBuilderStatus(), 0).build();
            }
        }
    }
    // Build offsets for array block (null valued have no positions)
    int[] offsets = new int[nextBatchSize + 1];
    for (int i = 1; i < offsets.length; i++) {
        int length = nullVector[i - 1] ? 0 : typeParameters.size();
        offsets[i] = offsets[i - 1] + length;
    }
    // Struct is represented as an array block holding an interleaved block
    InterleavedBlock interleavedBlock = new InterleavedBlock(blocks);
    ArrayBlock arrayBlock = new ArrayBlock(nextBatchSize, nullVector, offsets, interleavedBlock);
    readOffset = 0;
    nextBatchSize = 0;
    return arrayBlock;
}
Also used : StreamReaders.createStreamReader(com.facebook.presto.orc.reader.StreamReaders.createStreamReader) Type(com.facebook.presto.spi.type.Type) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) Block(com.facebook.presto.spi.block.Block) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 4 with ArrayBlock

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

the class ParquetReader method readStruct.

private Block readStruct(Type type, List<String> path, IntList elementOffsets) throws IOException {
    List<TypeSignatureParameter> parameters = type.getTypeSignature().getParameters();
    Block[] blocks = new Block[parameters.size()];
    for (int i = 0; i < parameters.size(); i++) {
        NamedTypeSignature namedTypeSignature = parameters.get(i).getNamedTypeSignature();
        Type fieldType = typeManager.getType(namedTypeSignature.getTypeSignature());
        String name = namedTypeSignature.getName();
        blocks[i] = readBlock(name, fieldType, path, new IntArrayList());
    }
    InterleavedBlock interleavedBlock = new InterleavedBlock(blocks);
    int blockSize = blocks[0].getPositionCount();
    int[] offsets = new int[blockSize + 1];
    for (int i = 1; i < offsets.length; i++) {
        elementOffsets.add(parameters.size());
        offsets[i] = i * parameters.size();
    }
    return new ArrayBlock(blockSize, new boolean[blockSize], offsets, interleavedBlock);
}
Also used : Type(com.facebook.presto.spi.type.Type) MessageType(parquet.schema.MessageType) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) Block(com.facebook.presto.spi.block.Block) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock)

Example 5 with ArrayBlock

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

the class ParquetReader method readArray.

private Block readArray(Type type, List<String> path, IntList elementOffsets) throws IOException {
    List<Type> parameters = type.getTypeParameters();
    checkArgument(parameters.size() == 1, "Arrays must have a single type parameter, found %d", parameters.size());
    path.add(ARRAY_TYPE_NAME);
    Type elementType = parameters.get(0);
    Block block = readBlock(ARRAY_ELEMENT_NAME, elementType, path, elementOffsets);
    path.remove(ARRAY_TYPE_NAME);
    if (elementOffsets.isEmpty()) {
        for (int i = 0; i < batchSize; i++) {
            elementOffsets.add(0);
        }
        return RunLengthEncodedBlock.create(elementType, null, batchSize);
    }
    int[] offsets = new int[batchSize + 1];
    for (int i = 1; i < offsets.length; i++) {
        offsets[i] = offsets[i - 1] + elementOffsets.getInt(i - 1);
    }
    return new ArrayBlock(batchSize, new boolean[batchSize], offsets, block);
}
Also used : Type(com.facebook.presto.spi.type.Type) MessageType(parquet.schema.MessageType) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) Block(com.facebook.presto.spi.block.Block) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock)

Aggregations

ArrayBlock (com.facebook.presto.spi.block.ArrayBlock)6 Block (com.facebook.presto.spi.block.Block)6 Type (com.facebook.presto.spi.type.Type)6 InterleavedBlock (com.facebook.presto.spi.block.InterleavedBlock)5 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)3 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)3 MessageType (parquet.schema.MessageType)3 OrcCorruptionException (com.facebook.presto.orc.OrcCorruptionException)2 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)2 StreamReaders.createStreamReader (com.facebook.presto.orc.reader.StreamReaders.createStreamReader)1 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)1 TypeSignatureParameter (com.facebook.presto.spi.type.TypeSignatureParameter)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1