Search in sources :

Example 6 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class TableFinishOperator method getOutput.

@Override
public Page getOutput() {
    if (snapshotState != null) {
        Page marker = snapshotState.nextMarker();
        if (marker != null) {
            return marker;
        }
    }
    if (!isBlocked().isDone()) {
        return null;
    }
    if (!statisticsAggregationOperator.isFinished()) {
        verify(statisticsAggregationOperator.isBlocked().isDone(), "aggregation operator should not be blocked");
        OperationTimer timer = new OperationTimer(statisticsCpuTimerEnabled);
        Page page = statisticsAggregationOperator.getOutput();
        timer.end(statisticsTiming);
        if (page == null) {
            return null;
        }
        for (int position = 0; position < page.getPositionCount(); position++) {
            computedStatistics.add(getComputedStatistics(page, position));
        }
        return null;
    }
    if (state != State.FINISHING) {
        return null;
    }
    state = State.FINISHED;
    outputMetadata = tableFinisher.finishTable(ImmutableList.copyOf(fragment), ImmutableList.copyOf(computedStatistics));
    // output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder page = new PageBuilder(1, TYPES);
    page.declarePosition();
    BIGINT.writeLong(page.getBlockBuilder(0), rowCount);
    return page.build();
}
Also used : Page(io.prestosql.spi.Page) PageBuilder(io.prestosql.spi.PageBuilder)

Example 7 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class RowNumberOperator method getSelectedRows.

private Page getSelectedRows() {
    verify(selectedRowPageBuilder.isPresent());
    int rowNumberChannel = types.size() - 1;
    PageBuilder pageBuilder = selectedRowPageBuilder.get();
    verify(pageBuilder.isEmpty());
    for (int currentPosition = 0; currentPosition < inputPage.getPositionCount(); currentPosition++) {
        long partitionId = getPartitionId(currentPosition);
        long rowCount = partitionRowCount.get(partitionId);
        if (rowCount == maxRowsPerPartition.get()) {
            continue;
        }
        pageBuilder.declarePosition();
        for (int i = 0; i < outputChannels.length; i++) {
            int channel = outputChannels[i];
            Type type = types.get(i);
            type.appendTo(inputPage.getBlock(channel), currentPosition, pageBuilder.getBlockBuilder(i));
        }
        BIGINT.writeLong(pageBuilder.getBlockBuilder(rowNumberChannel), rowCount + 1);
        partitionRowCount.set(partitionId, rowCount + 1);
    }
    if (pageBuilder.isEmpty()) {
        return null;
    }
    Page page = pageBuilder.build();
    pageBuilder.reset();
    return page;
}
Also used : Type(io.prestosql.spi.type.Type) Page(io.prestosql.spi.Page) PageBuilder(io.prestosql.spi.PageBuilder)

Example 8 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class MapConcatFunction method mapConcat.

@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Object state, Block[] maps) {
    int entries = 0;
    int lastMapIndex = maps.length - 1;
    int firstMapIndex = lastMapIndex;
    for (int i = 0; i < maps.length; i++) {
        entries += maps[i].getPositionCount();
        if (maps[i].getPositionCount() > 0) {
            lastMapIndex = i;
            firstMapIndex = min(firstMapIndex, i);
        }
    }
    if (lastMapIndex == firstMapIndex) {
        return maps[lastMapIndex];
    }
    PageBuilder pageBuilder = (PageBuilder) state;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    // TODO: we should move TypedSet into user state as well
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    TypedSet typedSet = new TypedSet(keyType, entries / 2, FUNCTION_NAME);
    BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    // the last map
    Block map = maps[lastMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        typedSet.add(map, i);
        keyType.appendTo(map, i, blockBuilder);
        valueType.appendTo(map, i + 1, blockBuilder);
    }
    // the map between the last and the first
    for (int idx = lastMapIndex - 1; idx > firstMapIndex; idx--) {
        map = maps[idx];
        for (int i = 0; i < map.getPositionCount(); i += 2) {
            if (!typedSet.contains(map, i)) {
                typedSet.add(map, i);
                keyType.appendTo(map, i, blockBuilder);
                valueType.appendTo(map, i + 1, blockBuilder);
            }
        }
    }
    // the first map
    map = maps[firstMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        if (!typedSet.contains(map, i)) {
            keyType.appendTo(map, i, blockBuilder);
            valueType.appendTo(map, i + 1, blockBuilder);
        }
    }
    mapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) TypedSet(io.prestosql.operator.aggregation.TypedSet) Block(io.prestosql.spi.block.Block) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 9 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class TestHivePageSink method writeTestFile.

private static long writeTestFile(HiveConfig config, HiveMetastore metastore, String outputPath) {
    HiveTransactionHandle transaction = new HiveTransactionHandle();
    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(HiveTestUtils.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(getSession(config), columnTypes, ImmutableList.of(page));
    MaterializedResult results = toMaterializedResult(getSession(config), columnTypes, pages);
    assertEquals(results, expectedResults);
    assertEquals(round(stats.getInputPageSizeInBytes().getAllTime().getMax()), page.getRetainedSizeInBytes());
    return length;
}
Also used : Path(org.apache.hadoop.fs.Path) NONE(io.prestosql.plugin.hive.HiveCompressionCodec.NONE) HiveTestUtils.getDefaultHiveSelectiveFactories(io.prestosql.plugin.hive.HiveTestUtils.getDefaultHiveSelectiveFactories) MoreFiles.deleteRecursively(com.google.common.io.MoreFiles.deleteRecursively) Assertions.assertGreaterThan(io.airlift.testing.Assertions.assertGreaterThan) ConnectorPageSink(io.prestosql.spi.connector.ConnectorPageSink) Test(org.testng.annotations.Test) TpchColumnTypes(io.airlift.tpch.TpchColumnTypes) MaterializedResult(io.prestosql.testing.MaterializedResult) Assert.assertEquals(io.prestosql.testing.assertions.Assert.assertEquals) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) Math.round(java.lang.Math.round) Files.createTempDirectory(java.nio.file.Files.createTempDirectory) Slices(io.airlift.slice.Slices) HIVE_STRING(io.prestosql.plugin.hive.HiveType.HIVE_STRING) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Path(org.apache.hadoop.fs.Path) Matchers.anyInt(org.mockito.Matchers.anyInt) Type(io.prestosql.spi.type.Type) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) HIVE_INT(io.prestosql.plugin.hive.HiveType.HIVE_INT) SERIALIZATION_LIB(org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_LIB) TpchColumnType(io.airlift.tpch.TpchColumnType) PageIndexerFactory(io.prestosql.spi.PageIndexerFactory) HIVE_LONG(io.prestosql.plugin.hive.HiveType.HIVE_LONG) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder) MetadataManager.createTestMetadataManager(io.prestosql.metadata.MetadataManager.createTestMetadataManager) PageBuilder(io.prestosql.spi.PageBuilder) String.format(java.lang.String.format) LineItemGenerator(io.airlift.tpch.LineItemGenerator) PageIndexer(io.prestosql.spi.PageIndexer) List(java.util.List) ConnectorPageSource(io.prestosql.spi.connector.ConnectorPageSource) Stream(java.util.stream.Stream) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) Optional(java.util.Optional) TestingNodeManager(io.prestosql.testing.TestingNodeManager) Joiner(com.google.common.base.Joiner) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) JsonCodec(io.airlift.json.JsonCodec) Mockito.mock(org.mockito.Mockito.mock) LineItem(io.airlift.tpch.LineItem) HivePageSinkMetadata(io.prestosql.plugin.hive.metastore.HivePageSinkMetadata) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) OptionalInt(java.util.OptionalInt) DIRECT_TO_TARGET_NEW_DIRECTORY(io.prestosql.plugin.hive.LocationHandle.WriteMode.DIRECT_TO_TARGET_NEW_DIRECTORY) ArrayList(java.util.ArrayList) GroupByHashPageIndexerFactory(io.prestosql.GroupByHashPageIndexerFactory) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) REGULAR(io.prestosql.plugin.hive.HiveColumnHandle.ColumnType.REGULAR) GenericExceptionAction(io.prestosql.plugin.hive.authentication.GenericExceptionAction) ALLOW_INSECURE(com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE) ImmutableList(com.google.common.collect.ImmutableList) Matchers.anyObject(org.mockito.Matchers.anyObject) LineItemColumn(io.airlift.tpch.LineItemColumn) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) DATE(io.prestosql.spi.type.DateType.DATE) FileHiveMetastore.createTestingFileHiveMetastore(io.prestosql.plugin.hive.metastore.file.FileHiveMetastore.createTestingFileHiveMetastore) HiveMetastore(io.prestosql.plugin.hive.metastore.HiveMetastore) HiveIdentity(io.prestosql.plugin.hive.authentication.HiveIdentity) Properties(java.util.Properties) ConnectorTableHandle(io.prestosql.spi.connector.ConnectorTableHandle) TypeManager(io.prestosql.spi.type.TypeManager) Page(io.prestosql.spi.Page) IOException(java.io.IOException) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) Mockito.when(org.mockito.Mockito.when) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) IntArrayBlock(io.prestosql.spi.block.IntArrayBlock) HIVE_DATE(io.prestosql.plugin.hive.HiveType.HIVE_DATE) HIVE_DOUBLE(io.prestosql.plugin.hive.HiveType.HIVE_DOUBLE) JoinCompiler(io.prestosql.sql.gen.JoinCompiler) FILE_INPUT_FORMAT(org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.FILE_INPUT_FORMAT) Assert.assertTrue(org.testng.Assert.assertTrue) TestingConnectorSession(io.prestosql.testing.TestingConnectorSession) Collections(java.util.Collections) LineItemColumn(io.airlift.tpch.LineItemColumn) ArrayList(java.util.ArrayList) LineItem(io.airlift.tpch.LineItem) Page(io.prestosql.spi.Page) PageBuilder(io.prestosql.spi.PageBuilder) ConnectorPageSource(io.prestosql.spi.connector.ConnectorPageSource) Type(io.prestosql.spi.type.Type) TpchColumnType(io.airlift.tpch.TpchColumnType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) ConnectorPageSink(io.prestosql.spi.connector.ConnectorPageSink) MaterializedResult(io.prestosql.testing.MaterializedResult) File(java.io.File) LineItemGenerator(io.airlift.tpch.LineItemGenerator) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 10 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class Query method getNextResult.

private synchronized DataCenterQueryResults getNextResult(long token, DataSize targetResultSize) {
    // check if the result for the token have already been created
    Optional<DataCenterQueryResults> cachedResult = getCachedDCQueryResult(token);
    if (cachedResult.isPresent()) {
        return cachedResult.get();
    }
    verify(nextToken.isPresent(), "Can not generate next result when next token is not present");
    verify(token == nextToken.getAsLong(), "Expected token to equal next token");
    URI queryHtmlUri = null;
    try {
        queryHtmlUri = new URI("http://localhost");
    } catch (URISyntaxException e) {
        log.error("get uri error: %s", e.getMessage());
    }
    // Remove as many pages as possible from the exchange until just greater than DESIRED_RESULT_BYTES
    // NOTE: it is critical that query results are created for the pages removed from the exchange
    // client while holding the lock because the query may transition to the finished state when the
    // last page is removed.  If another thread observes this state before the response is cached
    // the pages will be lost.
    List<SerializedPage> data = null;
    try {
        ImmutableList.Builder<SerializedPage> builder = new ImmutableList.Builder<>();
        long bytes = 0;
        long rows = 0;
        long targetResultBytes = targetResultSize.toBytes();
        while (bytes < targetResultBytes) {
            // at this point, origin is irrelevant, so we can safely ignore it
            SerializedPage serializedPage = exchangeClient.pollPage(null).getLeft();
            if (serializedPage == null) {
                break;
            }
            builder.add(new SerializedPage(serializedPage.getSlice().getBytes(), serializedPage.getPageCodecMarkers(), serializedPage.getPositionCount(), serializedPage.getUncompressedSizeInBytes()));
            bytes += serializedPage.getUncompressedSizeInBytes();
            rows += serializedPage.getPositionCount();
        }
        if (rows > 0) {
            // client implementations do not properly handle empty list of data
            data = builder.build();
        }
    } catch (Throwable cause) {
        queryManager.failQuery(queryId, cause);
    }
    // get the query info before returning
    // force update if query manager is closed
    QueryInfo queryInfo = queryManager.getFullQueryInfo(queryId);
    queryManager.recordHeartbeat(queryId);
    closeExchangeClientIfNecessary(queryInfo);
    // for queries with no output, return a fake result for clients that require it
    if ((queryInfo.getState() == QueryState.FINISHED) && !queryInfo.getOutputStage().isPresent()) {
        columns = ImmutableList.of(createColumn("result", BooleanType.BOOLEAN));
        PageBuilder pageBuilder = new PageBuilder(Collections.singletonList(BooleanType.BOOLEAN));
        pageBuilder.declarePosition();
        BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
        BooleanType.BOOLEAN.writeBoolean(blockBuilder, true);
        Page page = pageBuilder.build();
        SerializedPage serializedPage = serde.serialize(page);
        data = Collections.singletonList(new SerializedPage(serializedPage.getSlice().getBytes(), serializedPage.getPageCodecMarkers(), serializedPage.getPositionCount(), serializedPage.getUncompressedSizeInBytes()));
    } else if (queryInfo.isRunningAsync()) {
        columns = ImmutableList.of(createColumn("result", BooleanType.BOOLEAN), createColumn("runningAsync", BooleanType.BOOLEAN));
        PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(BooleanType.BOOLEAN, BooleanType.BOOLEAN));
        pageBuilder.declarePosition();
        BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
        BooleanType.BOOLEAN.writeBoolean(blockBuilder, true);
        BooleanType.BOOLEAN.writeBoolean(blockBuilder, true);
        Page page = pageBuilder.build();
        SerializedPage serializedPage = serde.serialize(page);
        data = Collections.singletonList(new SerializedPage(serializedPage.getSlice().getBytes(), serializedPage.getPageCodecMarkers(), serializedPage.getPositionCount(), serializedPage.getUncompressedSizeInBytes()));
    }
    // (3) Query supports and started running Async
    if ((!queryInfo.isFinalQueryInfo() && queryInfo.getState() != FAILED && !queryInfo.isRunningAsync()) || !exchangeClient.isClosed()) {
        nextToken = OptionalLong.of(token + 1);
    } else {
        nextToken = OptionalLong.empty();
    }
    URI nextResultsUri = null;
    if (nextToken.isPresent()) {
        try {
            nextResultsUri = new URI(Long.toString(nextToken.getAsLong()));
        } catch (URISyntaxException e) {
            log.error("get uri error: %s", e.getMessage());
        }
    }
    // update catalog, schema, and path
    setCatalog = queryInfo.getSetCatalog();
    setSchema = queryInfo.getSetSchema();
    setPath = queryInfo.getSetPath();
    // update setSessionProperties
    setSessionProperties = queryInfo.getSetSessionProperties();
    resetSessionProperties = queryInfo.getResetSessionProperties();
    // update setRoles
    setRoles = queryInfo.getSetRoles();
    // update preparedStatements
    addedPreparedStatements = queryInfo.getAddedPreparedStatements();
    deallocatedPreparedStatements = queryInfo.getDeallocatedPreparedStatements();
    // update startedTransactionId
    startedTransactionId = queryInfo.getStartedTransactionId();
    clearTransactionId = queryInfo.isClearTransactionId();
    // first time through, self is null
    DataCenterQueryResults queryResults = new DataCenterQueryResults(queryId.toString(), queryHtmlUri, findCancelableLeafStage(queryInfo), nextResultsUri, columns, data, toStatementStats(queryInfo), toQueryError(queryInfo), mappedCopy(queryInfo.getWarnings(), Query::toClientWarning), queryInfo.getUpdateType(), true);
    // cache the new result
    lastToken = token;
    lastDCQueryResult = queryResults;
    return queryResults;
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) BlockBuilder(io.prestosql.spi.block.BlockBuilder) PageBuilder(io.prestosql.spi.PageBuilder) DataCenterQueryResults(io.prestosql.client.DataCenterQueryResults) SerializedPage(io.hetu.core.transport.execution.buffer.SerializedPage) Page(io.prestosql.spi.Page) URISyntaxException(java.net.URISyntaxException) PageBuilder(io.prestosql.spi.PageBuilder) QueryInfo(io.prestosql.execution.QueryInfo) URI(java.net.URI) SerializedPage(io.hetu.core.transport.execution.buffer.SerializedPage) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Aggregations

PageBuilder (io.prestosql.spi.PageBuilder)58 Page (io.prestosql.spi.Page)28 BlockBuilder (io.prestosql.spi.block.BlockBuilder)27 Type (io.prestosql.spi.type.Type)24 ImmutableList (com.google.common.collect.ImmutableList)18 Test (org.testng.annotations.Test)14 List (java.util.List)11 Block (io.prestosql.spi.block.Block)10 ArrayList (java.util.ArrayList)9 INTEGER (io.prestosql.spi.type.IntegerType.INTEGER)8 Collectors.toList (java.util.stream.Collectors.toList)8 Slice (io.airlift.slice.Slice)7 Benchmark (org.openjdk.jmh.annotations.Benchmark)7 Slices (io.airlift.slice.Slices)6 ConnectorPageSource (io.prestosql.spi.connector.ConnectorPageSource)6 ConnectorSession (io.prestosql.spi.connector.ConnectorSession)6 ArrayType (io.prestosql.spi.type.ArrayType)6 BIGINT (io.prestosql.spi.type.BigintType.BIGINT)6 DOUBLE (io.prestosql.spi.type.DoubleType.DOUBLE)6 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)6