Search in sources :

Example 1 with Block

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

the class HivePageSink method getWriterIndexes.

private int[] getWriterIndexes(Page page) {
    Page partitionColumns = extractColumns(page, partitionColumnsInputIndex);
    Block bucketBlock = buildBucketBlock(page);
    int[] writerIndexes = pagePartitioner.partitionPage(partitionColumns, bucketBlock);
    if (pagePartitioner.getMaxIndex() >= maxOpenWriters) {
        throw new PrestoException(HIVE_TOO_MANY_OPEN_PARTITIONS, "Too many open partitions");
    }
    // expand writers list to new size
    while (writers.size() <= pagePartitioner.getMaxIndex()) {
        writers.add(null);
        WriterPositions newWriterPositions = new WriterPositions();
        systemMemoryUsage += sizeOf(newWriterPositions.getPositionsArray());
        writerPositions.add(newWriterPositions);
    }
    // create missing writers
    for (int position = 0; position < page.getPositionCount(); position++) {
        int writerIndex = writerIndexes[position];
        if (writers.get(writerIndex) != null) {
            continue;
        }
        OptionalInt bucketNumber = OptionalInt.empty();
        if (bucketBlock != null) {
            bucketNumber = OptionalInt.of(bucketBlock.getInt(position, 0));
        }
        HiveWriter writer = writerFactory.createWriter(partitionColumns, position, bucketNumber);
        writers.set(writerIndex, writer);
    }
    verify(writers.size() == pagePartitioner.getMaxIndex() + 1);
    verify(!writers.contains(null));
    return writerIndexes;
}
Also used : Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) Page(com.facebook.presto.spi.Page) PrestoException(com.facebook.presto.spi.PrestoException) OptionalInt(java.util.OptionalInt)

Example 2 with Block

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

the class HivePageSource method getNextPage.

@Override
public Page getNextPage() {
    try {
        Block[] blocks = new Block[columnMappings.size()];
        Page dataPage = delegate.getNextPage();
        if (dataPage == null) {
            return null;
        }
        int batchSize = dataPage.getPositionCount();
        for (int fieldId = 0; fieldId < blocks.length; fieldId++) {
            ColumnMapping columnMapping = columnMappings.get(fieldId);
            if (columnMapping.isPrefilled()) {
                blocks[fieldId] = RunLengthEncodedBlock.create(types[fieldId], prefilledValues[fieldId], batchSize);
            } else {
                blocks[fieldId] = dataPage.getBlock(columnMapping.getIndex());
                if (coercers[fieldId] != null) {
                    blocks[fieldId] = new LazyBlock(batchSize, new CoercionLazyBlockLoader(blocks[fieldId], coercers[fieldId]));
                }
            }
        }
        return new Page(batchSize, blocks);
    } catch (PrestoException e) {
        closeWithSuppression(e);
        throw e;
    } catch (RuntimeException e) {
        closeWithSuppression(e);
        throw new PrestoException(HIVE_CURSOR_ERROR, e);
    }
}
Also used : LazyBlock(com.facebook.presto.spi.block.LazyBlock) Block(com.facebook.presto.spi.block.Block) LazyBlock(com.facebook.presto.spi.block.LazyBlock) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) Page(com.facebook.presto.spi.Page) PrestoException(com.facebook.presto.spi.PrestoException) ColumnMapping(com.facebook.presto.hive.HivePageSourceProvider.ColumnMapping)

Example 3 with Block

use of com.facebook.presto.spi.block.Block 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 4 with Block

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

the class ParquetReader method readBlock.

private Block readBlock(String name, Type type, List<String> path, IntList offsets) throws IOException {
    path.add(name);
    Optional<RichColumnDescriptor> descriptor = getDescriptor(fileSchema, requestedSchema, path);
    if (!descriptor.isPresent()) {
        path.remove(name);
        return RunLengthEncodedBlock.create(type, null, batchSize);
    }
    Block block;
    if (ROW.equals(type.getTypeSignature().getBase())) {
        block = readStruct(type, path, offsets);
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        block = readMap(type, path, offsets);
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        block = readArray(type, path, offsets);
    } else {
        block = readPrimitive(descriptor.get(), type, offsets);
    }
    path.remove(name);
    return block;
}
Also used : RichColumnDescriptor(com.facebook.presto.hive.parquet.RichColumnDescriptor) 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)

Example 5 with Block

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

the class TestSerDeUtils method testMapBlock.

@Test
public void testMapBlock() {
    MapHolder holder = new MapHolder();
    holder.map = new TreeMap<>();
    holder.map.put("twelve", new InnerStruct(13, 14L));
    holder.map.put("fifteen", new InnerStruct(16, 17L));
    com.facebook.presto.spi.type.Type rowType = new RowType(ImmutableList.of(INTEGER, BIGINT), Optional.empty());
    com.facebook.presto.spi.type.Type mapOfVarcharRowType = new RowType(ImmutableList.of(new MapType(createUnboundedVarcharType(), rowType)), Optional.empty());
    Block actual = toBinaryBlock(mapOfVarcharRowType, holder, getInspector(MapHolder.class));
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(createUnboundedVarcharType(), rowType), new BlockBuilderStatus(), 1024);
    createUnboundedVarcharType().writeString(blockBuilder, "fifteen");
    rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 16, 17L));
    createUnboundedVarcharType().writeString(blockBuilder, "twelve");
    rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 13, 14L));
    Block expected = rowBlockOf(ImmutableList.of(new MapType(createUnboundedVarcharType(), rowType)), blockBuilder);
    assertBlockEquals(actual, expected);
}
Also used : RowType(com.facebook.presto.type.RowType) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) MapType(com.facebook.presto.type.MapType) Block(com.facebook.presto.spi.block.Block) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Aggregations

Block (com.facebook.presto.spi.block.Block)262 Type (com.facebook.presto.spi.type.Type)71 Page (com.facebook.presto.spi.Page)67 Test (org.testng.annotations.Test)62 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)59 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)56 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)35 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)25 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)25 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)21 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)20 LazyBlock (com.facebook.presto.spi.block.LazyBlock)19 Variable (com.facebook.presto.bytecode.Variable)18 PrestoException (com.facebook.presto.spi.PrestoException)18 ArrayType (com.facebook.presto.type.ArrayType)17 ArrayList (java.util.ArrayList)17 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)16 Slice (io.airlift.slice.Slice)16 Parameter (com.facebook.presto.bytecode.Parameter)15 PageBuilder (com.facebook.presto.spi.PageBuilder)15