Search in sources :

Example 41 with PageBuilder

use of io.trino.spi.PageBuilder in project trino by trinodb.

the class BenchmarkGroupByHash method createBigintPages.

private static List<Page> createBigintPages(int positionCount, int groupCount, int channelCount, boolean hashEnabled, boolean useMixedBlockTypes) {
    List<Type> types = Collections.nCopies(channelCount, BIGINT);
    ImmutableList.Builder<Page> pages = ImmutableList.builder();
    if (hashEnabled) {
        types = ImmutableList.copyOf(Iterables.concat(types, ImmutableList.of(BIGINT)));
    }
    PageBuilder pageBuilder = new PageBuilder(types);
    int pageCount = 0;
    for (int position = 0; position < positionCount; position++) {
        int rand = ThreadLocalRandom.current().nextInt(groupCount);
        pageBuilder.declarePosition();
        for (int numChannel = 0; numChannel < channelCount; numChannel++) {
            BIGINT.writeLong(pageBuilder.getBlockBuilder(numChannel), rand);
        }
        if (hashEnabled) {
            BIGINT.writeLong(pageBuilder.getBlockBuilder(channelCount), AbstractLongType.hash(rand));
        }
        if (pageBuilder.isFull()) {
            Page page = pageBuilder.build();
            pageBuilder.reset();
            if (useMixedBlockTypes) {
                if (pageCount % 3 == 0) {
                    pages.add(page);
                } else if (pageCount % 3 == 1) {
                    // rle page
                    Block[] blocks = new Block[page.getChannelCount()];
                    for (int channel = 0; channel < blocks.length; ++channel) {
                        blocks[channel] = new RunLengthEncodedBlock(page.getBlock(channel).getSingleValueBlock(0), page.getPositionCount());
                    }
                    pages.add(new Page(blocks));
                } else {
                    // dictionary page
                    int[] positions = IntStream.range(0, page.getPositionCount()).toArray();
                    Block[] blocks = new Block[page.getChannelCount()];
                    for (int channel = 0; channel < page.getChannelCount(); ++channel) {
                        blocks[channel] = new DictionaryBlock(page.getBlock(channel), positions);
                    }
                    pages.add(new Page(blocks));
                }
            } else {
                pages.add(page);
            }
            pageCount++;
        }
    }
    pages.add(pageBuilder.build());
    return pages.build();
}
Also used : Type(io.trino.spi.type.Type) AbstractLongType(io.trino.spi.type.AbstractLongType) ImmutableList(com.google.common.collect.ImmutableList) DictionaryBlock(io.trino.spi.block.DictionaryBlock) Page(io.trino.spi.Page) PageBuilder(io.trino.spi.PageBuilder) RunLengthEncodedBlock(io.trino.spi.block.RunLengthEncodedBlock)

Example 42 with PageBuilder

use of io.trino.spi.PageBuilder in project trino by trinodb.

the class AbstractRowChangeOperator method getOutput.

@Override
public Page getOutput() {
    if ((state != State.FINISHING) || !finishFuture.isDone()) {
        return null;
    }
    state = State.FINISHED;
    Collection<Slice> fragments = getFutureValue(finishFuture);
    // output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder page = new PageBuilder(fragments.size() + 1, TYPES);
    BlockBuilder rowsBuilder = page.getBlockBuilder(0);
    BlockBuilder fragmentBuilder = page.getBlockBuilder(1);
    // write row count
    page.declarePosition();
    BIGINT.writeLong(rowsBuilder, rowCount);
    fragmentBuilder.appendNull();
    // write fragments
    for (Slice fragment : fragments) {
        page.declarePosition();
        rowsBuilder.appendNull();
        VARBINARY.writeSlice(fragmentBuilder, fragment);
    }
    return page.build();
}
Also used : Slice(io.airlift.slice.Slice) PageBuilder(io.trino.spi.PageBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 43 with PageBuilder

use of io.trino.spi.PageBuilder in project trino by trinodb.

the class AggregationOperator method getOutput.

@Override
public Page getOutput() {
    if (state != State.HAS_OUTPUT) {
        return null;
    }
    // project results into output blocks
    List<Type> types = aggregates.stream().map(Aggregator::getType).collect(toImmutableList());
    // output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder pageBuilder = new PageBuilder(1, types);
    pageBuilder.declarePosition();
    for (int i = 0; i < aggregates.size(); i++) {
        Aggregator aggregator = aggregates.get(i);
        BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
        aggregator.evaluate(blockBuilder);
    }
    state = State.FINISHED;
    return pageBuilder.build();
}
Also used : Type(io.trino.spi.type.Type) Aggregator(io.trino.operator.aggregation.Aggregator) PageBuilder(io.trino.spi.PageBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 44 with PageBuilder

use of io.trino.spi.PageBuilder in project trino by trinodb.

the class TestHivePageSink method writeTestFile.

private static long writeTestFile(HiveConfig config, HiveMetastore metastore, String outputPath) {
    HiveTransactionHandle transaction = new HiveTransactionHandle(false);
    HiveWriterStats stats = new HiveWriterStats();
    ConnectorPageSink pageSink = createPageSink(transaction, config, metastore, new Path("file:///" + outputPath), stats);
    List<LineItemColumn> columns = getTestColumns();
    List<Type> columnTypes = columns.stream().map(LineItemColumn::getType).map(TestHivePageSink::getHiveType).map(hiveType -> hiveType.getType(TESTING_TYPE_MANAGER)).collect(toList());
    PageBuilder pageBuilder = new PageBuilder(columnTypes);
    int rows = 0;
    for (LineItem lineItem : new LineItemGenerator(0.01, 1, 1)) {
        rows++;
        if (rows >= NUM_ROWS) {
            break;
        }
        pageBuilder.declarePosition();
        for (int i = 0; i < columns.size(); i++) {
            LineItemColumn column = columns.get(i);
            BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
            switch(column.getType().getBase()) {
                case IDENTIFIER:
                    BIGINT.writeLong(blockBuilder, column.getIdentifier(lineItem));
                    break;
                case INTEGER:
                    INTEGER.writeLong(blockBuilder, column.getInteger(lineItem));
                    break;
                case DATE:
                    DATE.writeLong(blockBuilder, column.getDate(lineItem));
                    break;
                case DOUBLE:
                    DOUBLE.writeDouble(blockBuilder, column.getDouble(lineItem));
                    break;
                case VARCHAR:
                    createUnboundedVarcharType().writeSlice(blockBuilder, Slices.utf8Slice(column.getString(lineItem)));
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported type " + column.getType());
            }
        }
    }
    Page page = pageBuilder.build();
    pageSink.appendPage(page);
    getFutureValue(pageSink.finish());
    File outputDir = new File(outputPath);
    List<File> files = ImmutableList.copyOf(outputDir.listFiles((dir, name) -> !name.endsWith(".crc")));
    File outputFile = getOnlyElement(files);
    long length = outputFile.length();
    ConnectorPageSource pageSource = createPageSource(transaction, config, outputFile);
    List<Page> pages = new ArrayList<>();
    while (!pageSource.isFinished()) {
        Page nextPage = pageSource.getNextPage();
        if (nextPage != null) {
            pages.add(nextPage.getLoadedPage());
        }
    }
    MaterializedResult expectedResults = toMaterializedResult(getHiveSession(config), columnTypes, ImmutableList.of(page));
    MaterializedResult results = toMaterializedResult(getHiveSession(config), columnTypes, pages);
    assertEquals(results, expectedResults);
    assertEquals(round(stats.getInputPageSizeInBytes().getAllTime().getMax()), page.getRetainedSizeInBytes());
    return length;
}
Also used : Path(org.apache.hadoop.fs.Path) MoreFiles.deleteRecursively(com.google.common.io.MoreFiles.deleteRecursively) MaterializedResult(io.trino.testing.MaterializedResult) Assertions.assertGreaterThan(io.airlift.testing.Assertions.assertGreaterThan) Test(org.testng.annotations.Test) NO_ACID_TRANSACTION(io.trino.plugin.hive.acid.AcidTransaction.NO_ACID_TRANSACTION) TypeOperators(io.trino.spi.type.TypeOperators) SplitWeight(io.trino.spi.SplitWeight) HiveMetastoreFactory(io.trino.plugin.hive.metastore.HiveMetastoreFactory) TpchColumnType(io.trino.tpch.TpchColumnType) Math.round(java.lang.Math.round) Slices(io.airlift.slice.Slices) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Path(org.apache.hadoop.fs.Path) LineItemColumn(io.trino.tpch.LineItemColumn) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) INTEGER(io.trino.spi.type.IntegerType.INTEGER) Assert.assertEquals(io.trino.testing.assertions.Assert.assertEquals) SERIALIZATION_LIB(org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_LIB) HiveTestUtils.getDefaultHiveRecordCursorProviders(io.trino.plugin.hive.HiveTestUtils.getDefaultHiveRecordCursorProviders) TestingNodeManager(io.trino.testing.TestingNodeManager) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) HIVE_DATE(io.trino.plugin.hive.HiveType.HIVE_DATE) HDFS_ENVIRONMENT(io.trino.plugin.hive.HiveTestUtils.HDFS_ENVIRONMENT) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) LineItemGenerator(io.trino.tpch.LineItemGenerator) LineItem(io.trino.tpch.LineItem) List(java.util.List) Stream(java.util.stream.Stream) BIGINT(io.trino.spi.type.BigintType.BIGINT) DynamicFilter(io.trino.spi.connector.DynamicFilter) Optional(java.util.Optional) HivePageSinkMetadata(io.trino.plugin.hive.metastore.HivePageSinkMetadata) DATE(io.trino.spi.type.DateType.DATE) Joiner(com.google.common.base.Joiner) JsonCodec(io.airlift.json.JsonCodec) DIRECT_TO_TARGET_NEW_DIRECTORY(io.trino.plugin.hive.LocationHandle.WriteMode.DIRECT_TO_TARGET_NEW_DIRECTORY) HiveTestUtils.getDefaultHivePageSourceFactories(io.trino.plugin.hive.HiveTestUtils.getDefaultHivePageSourceFactories) HIVE_DOUBLE(io.trino.plugin.hive.HiveType.HIVE_DOUBLE) PageBuilder(io.trino.spi.PageBuilder) Type(io.trino.spi.type.Type) Page(io.trino.spi.Page) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) JoinCompiler(io.trino.sql.gen.JoinCompiler) OptionalInt(java.util.OptionalInt) GroupByHashPageIndexerFactory(io.trino.operator.GroupByHashPageIndexerFactory) ArrayList(java.util.ArrayList) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) ALLOW_INSECURE(com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) NONE(io.trino.plugin.hive.HiveCompressionCodec.NONE) HiveColumnHandle.createBaseColumn(io.trino.plugin.hive.HiveColumnHandle.createBaseColumn) FileHiveMetastore.createTestingFileHiveMetastore(io.trino.plugin.hive.metastore.file.FileHiveMetastore.createTestingFileHiveMetastore) ConnectorPageSink(io.trino.spi.connector.ConnectorPageSink) BlockTypeOperators(io.trino.type.BlockTypeOperators) Properties(java.util.Properties) HIVE_LONG(io.trino.plugin.hive.HiveType.HIVE_LONG) HiveTestUtils.getDefaultHiveFileWriterFactories(io.trino.plugin.hive.HiveTestUtils.getDefaultHiveFileWriterFactories) TESTING_TYPE_MANAGER(io.trino.type.InternalTypeManager.TESTING_TYPE_MANAGER) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) HiveTestUtils.getHiveSessionProperties(io.trino.plugin.hive.HiveTestUtils.getHiveSessionProperties) ConnectorSession(io.trino.spi.connector.ConnectorSession) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) PAGE_SORTER(io.trino.plugin.hive.HiveTestUtils.PAGE_SORTER) File(java.io.File) HIVE_STRING(io.trino.plugin.hive.HiveType.HIVE_STRING) TpchColumnTypes(io.trino.tpch.TpchColumnTypes) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) HiveTestUtils.getHiveSession(io.trino.plugin.hive.HiveTestUtils.getHiveSession) HIVE_INT(io.trino.plugin.hive.HiveType.HIVE_INT) Collectors.toList(java.util.stream.Collectors.toList) FILE_INPUT_FORMAT(org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.FILE_INPUT_FORMAT) Assert.assertTrue(org.testng.Assert.assertTrue) BlockBuilder(io.trino.spi.block.BlockBuilder) REGULAR(io.trino.plugin.hive.HiveColumnHandle.ColumnType.REGULAR) LineItemColumn(io.trino.tpch.LineItemColumn) ArrayList(java.util.ArrayList) LineItem(io.trino.tpch.LineItem) Page(io.trino.spi.Page) PageBuilder(io.trino.spi.PageBuilder) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) TpchColumnType(io.trino.tpch.TpchColumnType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) ConnectorPageSink(io.trino.spi.connector.ConnectorPageSink) MaterializedResult(io.trino.testing.MaterializedResult) File(java.io.File) LineItemGenerator(io.trino.tpch.LineItemGenerator) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 45 with PageBuilder

use of io.trino.spi.PageBuilder in project trino by trinodb.

the class ArrayJoin method arrayJoin.

@UsedByGeneratedCode
public static Slice arrayJoin(MethodHandle castFunction, Object state, ConnectorSession session, Block arrayBlock, Slice delimiter, Slice nullReplacement) {
    PageBuilder pageBuilder = (PageBuilder) state;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    int numElements = arrayBlock.getPositionCount();
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    boolean needsDelimiter = false;
    for (int i = 0; i < numElements; i++) {
        Slice value = null;
        if (!arrayBlock.isNull(i)) {
            try {
                value = (Slice) castFunction.invokeExact(session, arrayBlock, i);
            } catch (Throwable throwable) {
                // Restore pageBuilder into a consistent state
                blockBuilder.closeEntry();
                pageBuilder.declarePosition();
                throw new TrinoException(GENERIC_INTERNAL_ERROR, "Error casting array element to VARCHAR", throwable);
            }
        }
        if (value == null) {
            value = nullReplacement;
            if (value == null) {
                continue;
            }
        }
        if (needsDelimiter) {
            blockBuilder.writeBytes(delimiter, 0, delimiter.length());
        }
        blockBuilder.writeBytes(value, 0, value.length());
        needsDelimiter = true;
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return VARCHAR.getSlice(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : Slice(io.airlift.slice.Slice) TrinoException(io.trino.spi.TrinoException) PageBuilder(io.trino.spi.PageBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Aggregations

PageBuilder (io.trino.spi.PageBuilder)58 BlockBuilder (io.trino.spi.block.BlockBuilder)24 Page (io.trino.spi.Page)23 Type (io.trino.spi.type.Type)22 ImmutableList (com.google.common.collect.ImmutableList)14 Block (io.trino.spi.block.Block)11 Test (org.testng.annotations.Test)10 List (java.util.List)8 Slice (io.airlift.slice.Slice)6 ArrayType (io.trino.spi.type.ArrayType)6 INTEGER (io.trino.spi.type.IntegerType.INTEGER)5 Slices (io.airlift.slice.Slices)4 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)4 DictionaryBlock (io.trino.spi.block.DictionaryBlock)4 RunLengthEncodedBlock (io.trino.spi.block.RunLengthEncodedBlock)4 BIGINT (io.trino.spi.type.BigintType.BIGINT)4 DOUBLE (io.trino.spi.type.DoubleType.DOUBLE)4 MapType (io.trino.spi.type.MapType)4 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)4 RowType (io.trino.spi.type.RowType)3