Search in sources :

Example 11 with FixedPageSource

use of io.trino.spi.connector.FixedPageSource in project trino by trinodb.

the class ColumnRangesSystemTable method pageSource.

@Override
public ConnectorPageSource pageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) {
    String metadataSqlQuery = getColumnRangesMetadataSqlQuery(sourceTable, indexedRaptorColumns);
    List<Type> columnTypes = tableMetadata.getColumns().stream().map(ColumnMetadata::getType).collect(toImmutableList());
    PageListBuilder pageListBuilder = new PageListBuilder(columnTypes);
    try (Connection connection = dbi.open().getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(metadataSqlQuery)) {
        if (resultSet.next()) {
            pageListBuilder.beginRow();
            for (int i = 0; i < columnTypes.size(); ++i) {
                BlockBuilder blockBuilder = pageListBuilder.nextBlockBuilder();
                Type columnType = columnTypes.get(i);
                if (columnType.equals(BIGINT) || columnType.equals(DATE)) {
                    long value = resultSet.getLong(i + 1);
                    if (!resultSet.wasNull()) {
                        columnType.writeLong(blockBuilder, value);
                    } else {
                        blockBuilder.appendNull();
                    }
                } else if (columnType.equals(TIMESTAMP_MILLIS)) {
                    long value = resultSet.getLong(i + 1);
                    if (!resultSet.wasNull()) {
                        columnType.writeLong(blockBuilder, value * MICROSECONDS_PER_MILLISECOND);
                    } else {
                        blockBuilder.appendNull();
                    }
                } else if (columnType.equals(BOOLEAN)) {
                    boolean value = resultSet.getBoolean(i + 1);
                    if (!resultSet.wasNull()) {
                        BOOLEAN.writeBoolean(blockBuilder, value);
                    } else {
                        blockBuilder.appendNull();
                    }
                } else {
                    throw new VerifyException("Unknown or unsupported column type: " + columnType);
                }
            }
        }
    } catch (SQLException | JdbiException e) {
        throw metadataError(e);
    }
    return new FixedPageSource(pageListBuilder.build());
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) FixedPageSource(io.trino.spi.connector.FixedPageSource) Type(io.trino.spi.type.Type) VerifyException(com.google.common.base.VerifyException) ResultSet(java.sql.ResultSet) JdbiException(org.jdbi.v3.core.JdbiException) BlockBuilder(io.trino.spi.block.BlockBuilder)

Aggregations

FixedPageSource (io.trino.spi.connector.FixedPageSource)11 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)7 Test (org.testng.annotations.Test)7 Page (io.trino.spi.Page)6 Type (io.trino.spi.type.Type)5 Duration (io.airlift.units.Duration)4 CatalogName (io.trino.connector.CatalogName)4 Split (io.trino.metadata.Split)4 ScheduledSplit (io.trino.execution.ScheduledSplit)3 SplitAssignment (io.trino.execution.SplitAssignment)3 CursorProcessor (io.trino.operator.project.CursorProcessor)3 PageProcessor (io.trino.operator.project.PageProcessor)3 BlockBuilder (io.trino.spi.block.BlockBuilder)3 ImmutableList (com.google.common.collect.ImmutableList)2 OperatorAssertion.toMaterializedResult (io.trino.operator.OperatorAssertion.toMaterializedResult)2 Block (io.trino.spi.block.Block)2 ConnectorPageSource (io.trino.spi.connector.ConnectorPageSource)2 RowExpression (io.trino.sql.relational.RowExpression)2 TestingSplit (io.trino.testing.TestingSplit)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1