Search in sources :

Example 1 with RowBlock

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

the class StructBatchStreamReader 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 field readers
            readOffset = presentStream.countBitsSet(readOffset);
        }
        for (BatchStreamReader structField : structFields.values()) {
            structField.prepareNextRead(readOffset);
        }
    }
    boolean[] nullVector = null;
    Block[] blocks;
    if (presentStream == null) {
        blocks = getBlocksForType(nextBatchSize);
    } else {
        nullVector = new boolean[nextBatchSize];
        int nullValues = presentStream.getUnsetBits(nextBatchSize, nullVector);
        if (nullValues != nextBatchSize) {
            blocks = getBlocksForType(nextBatchSize - nullValues);
        } else {
            List<Type> typeParameters = type.getTypeParameters();
            blocks = new Block[typeParameters.size()];
            for (int i = 0; i < typeParameters.size(); i++) {
                blocks[i] = typeParameters.get(i).createBlockBuilder(null, 0).build();
            }
        }
    }
    verify(Arrays.stream(blocks).mapToInt(Block::getPositionCount).distinct().count() == 1);
    // Struct is represented as a row block
    Block rowBlock = RowBlock.fromFieldBlocks(nextBatchSize, Optional.ofNullable(nullVector), blocks);
    readOffset = 0;
    nextBatchSize = 0;
    return rowBlock;
}
Also used : ReaderUtils.verifyStreamType(com.facebook.presto.orc.reader.ReaderUtils.verifyStreamType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) RowBlock(com.facebook.presto.common.block.RowBlock) Block(com.facebook.presto.common.block.Block)

Example 2 with RowBlock

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

the class TestStructBatchStreamReader method testReaderLowerCasesFieldNamesFromStream.

/**
 * The writer has fields with upper case characters, reader has same names downcased.
 */
@Test
public void testReaderLowerCasesFieldNamesFromStream() throws IOException {
    List<String> readerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));
    List<String> writerFields = new ArrayList<>(Arrays.asList("field_A", "field_B", "field_C"));
    List<String> writerData = new ArrayList<>(Arrays.asList("fieldAValue", "fieldBValue", "fieldCValue"));
    Type readerType = getType(readerFields);
    Type writerType = getType(writerFields);
    write(tempFile, writerType, writerData);
    RowBlock readBlock = read(tempFile, readerType);
    List actual = (List) readerType.getObjectValue(SESSION.getSqlFunctionProperties(), readBlock, 0);
    assertEquals(actual.size(), readerFields.size());
    assertEquals(actual.get(0), "fieldAValue");
    assertEquals(actual.get(1), "fieldBValue");
    assertEquals(actual.get(2), "fieldCValue");
}
Also used : Type(com.facebook.presto.common.type.Type) ArrayList(java.util.ArrayList) RowBlock(com.facebook.presto.common.block.RowBlock) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 3 with RowBlock

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

the class TestStructBatchStreamReader method testExtraFieldsInWriter.

/**
 * The ORC file has a field that is missing from the reader
 */
@Test
public void testExtraFieldsInWriter() throws IOException {
    // field_b is missing
    List<String> readerFields = new ArrayList<>(Arrays.asList("field_a", "field_c"));
    List<String> writerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));
    List<String> writerData = new ArrayList<>(Arrays.asList("field_a_value", "field_b_value", "field_c_value"));
    Type readerType = getType(readerFields);
    Type writerType = getType(writerFields);
    write(tempFile, writerType, writerData);
    RowBlock readBlock = read(tempFile, readerType);
    List actual = (List) readerType.getObjectValue(SESSION.getSqlFunctionProperties(), readBlock, 0);
    assertEquals(actual.size(), readerFields.size());
    assertEquals(actual.get(0), "field_a_value");
    assertEquals(actual.get(1), "field_c_value");
}
Also used : Type(com.facebook.presto.common.type.Type) ArrayList(java.util.ArrayList) RowBlock(com.facebook.presto.common.block.RowBlock) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 4 with RowBlock

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

the class TestStructBatchStreamReader method read.

private RowBlock read(TempFile tempFile, Type readerType) throws IOException {
    DataSize dataSize = new DataSize(1, MEGABYTE);
    OrcDataSource orcDataSource = new FileOrcDataSource(tempFile.getFile(), dataSize, dataSize, dataSize, true);
    OrcReader orcReader = new OrcReader(orcDataSource, ORC, new StorageOrcFileTailSource(), new StorageStripeMetadataSource(), NOOP_ORC_AGGREGATED_MEMORY_CONTEXT, new OrcReaderOptions(dataSize, dataSize, dataSize, false), false, NO_ENCRYPTION, DwrfKeyProvider.EMPTY, new RuntimeStats());
    Map<Integer, Type> includedColumns = new HashMap<>();
    includedColumns.put(0, readerType);
    OrcBatchRecordReader recordReader = orcReader.createBatchRecordReader(includedColumns, OrcPredicate.TRUE, UTC, new TestingHiveOrcAggregatedMemoryContext(), OrcReader.INITIAL_BATCH_SIZE);
    recordReader.nextBatch();
    RowBlock block = (RowBlock) recordReader.readBlock(0);
    recordReader.close();
    return block;
}
Also used : HashMap(java.util.HashMap) RuntimeStats(com.facebook.presto.common.RuntimeStats) StorageOrcFileTailSource(com.facebook.presto.orc.cache.StorageOrcFileTailSource) RowBlock(com.facebook.presto.common.block.RowBlock) Type(com.facebook.presto.common.type.Type) DataSize(io.airlift.units.DataSize)

Example 5 with RowBlock

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

the class TestStructBatchStreamReader method testReaderLowerCasesFieldNamesFromType.

/**
 * Reader has fields with upper case characters, writer has same names downcased.
 */
@Test
public void testReaderLowerCasesFieldNamesFromType() throws IOException {
    List<String> readerFields = new ArrayList<>(Arrays.asList("field_A", "field_B", "field_C"));
    List<String> writerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));
    List<String> writerData = new ArrayList<>(Arrays.asList("fieldAValue", "fieldBValue", "fieldCValue"));
    Type readerType = getType(readerFields);
    Type writerType = getType(writerFields);
    write(tempFile, writerType, writerData);
    RowBlock readBlock = read(tempFile, readerType);
    List actual = (List) readerType.getObjectValue(SESSION.getSqlFunctionProperties(), readBlock, 0);
    assertEquals(actual.size(), readerFields.size());
    assertEquals(actual.get(0), "fieldAValue");
    assertEquals(actual.get(1), "fieldBValue");
    assertEquals(actual.get(2), "fieldCValue");
}
Also used : Type(com.facebook.presto.common.type.Type) ArrayList(java.util.ArrayList) RowBlock(com.facebook.presto.common.block.RowBlock) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Aggregations

RowBlock (com.facebook.presto.common.block.RowBlock)11 Type (com.facebook.presto.common.type.Type)8 ImmutableList (com.google.common.collect.ImmutableList)6 Test (org.testng.annotations.Test)6 Block (com.facebook.presto.common.block.Block)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)3 ArrayBlock (com.facebook.presto.common.block.ArrayBlock)2 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)2 RowType (com.facebook.presto.common.type.RowType)2 DataSize (io.airlift.units.DataSize)2 Page (com.facebook.presto.common.Page)1 RuntimeStats (com.facebook.presto.common.RuntimeStats)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