Search in sources :

Example 1 with MappedRecordSet

use of io.trino.split.MappedRecordSet in project trino by trinodb.

the class SystemPageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transaction, ConnectorSession session, ConnectorSplit split, ConnectorTableHandle table, List<ColumnHandle> columns, DynamicFilter dynamicFilter) {
    requireNonNull(columns, "columns is null");
    SystemTransactionHandle systemTransaction = (SystemTransactionHandle) transaction;
    SystemSplit systemSplit = (SystemSplit) split;
    SchemaTableName tableName = ((SystemTableHandle) table).getSchemaTableName();
    SystemTable systemTable = tables.getSystemTable(session, tableName).orElseThrow(() -> new TrinoException(NOT_FOUND, format("Table '%s' not found", tableName)));
    List<ColumnMetadata> tableColumns = systemTable.getTableMetadata().getColumns();
    Map<String, Integer> columnsByName = new HashMap<>();
    for (int i = 0; i < tableColumns.size(); i++) {
        ColumnMetadata column = tableColumns.get(i);
        if (columnsByName.put(column.getName(), i) != null) {
            throw new TrinoException(GENERIC_INTERNAL_ERROR, "Duplicate column name: " + column.getName());
        }
    }
    ImmutableList.Builder<Integer> userToSystemFieldIndex = ImmutableList.builder();
    for (ColumnHandle column : columns) {
        String columnName = ((SystemColumnHandle) column).getColumnName();
        Integer index = columnsByName.get(columnName);
        if (index == null) {
            throw new TrinoException(GENERIC_INTERNAL_ERROR, format("Column does not exist: %s.%s", tableName, columnName));
        }
        userToSystemFieldIndex.add(index);
    }
    TupleDomain<ColumnHandle> constraint = systemSplit.getConstraint();
    if (constraint.isNone()) {
        return new EmptyPageSource();
    }
    TupleDomain<Integer> newConstraint = systemSplit.getConstraint().transformKeys(columnHandle -> columnsByName.get(((SystemColumnHandle) columnHandle).getColumnName()));
    try {
        return new MappedPageSource(systemTable.pageSource(systemTransaction.getConnectorTransactionHandle(), session, newConstraint), userToSystemFieldIndex.build());
    } catch (UnsupportedOperationException e) {
        return new RecordPageSource(new MappedRecordSet(toRecordSet(systemTransaction.getConnectorTransactionHandle(), systemTable, session, newConstraint), userToSystemFieldIndex.build()));
    }
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SchemaTableName(io.trino.spi.connector.SchemaTableName) RecordPageSource(io.trino.spi.connector.RecordPageSource) MappedPageSource(io.trino.split.MappedPageSource) EmptyPageSource(io.trino.spi.connector.EmptyPageSource) MappedRecordSet(io.trino.split.MappedRecordSet) TrinoException(io.trino.spi.TrinoException) SystemTable(io.trino.spi.connector.SystemTable)

Example 2 with MappedRecordSet

use of io.trino.split.MappedRecordSet in project trino by trinodb.

the class ThriftIndexedTpchService method lookupIndexKeys.

/**
 * Get lookup result and re-map output columns based on requested order.
 */
private static RecordSet lookupIndexKeys(RecordSet keys, IndexedTable table, List<String> outputColumnNames, List<String> lookupColumnNames) {
    RecordSet allColumnsOutputRecordSet = table.lookupKeys(new MappedRecordSet(keys, computeRemap(lookupColumnNames, table.getKeyColumns())));
    List<Integer> outputRemap = computeRemap(table.getOutputColumns(), outputColumnNames);
    return new MappedRecordSet(allColumnsOutputRecordSet, outputRemap);
}
Also used : MappedRecordSet(io.trino.split.MappedRecordSet) RecordSet(io.trino.spi.connector.RecordSet) MappedRecordSet(io.trino.split.MappedRecordSet)

Example 3 with MappedRecordSet

use of io.trino.split.MappedRecordSet in project trino by trinodb.

the class TpchIndexProvider method getIndex.

@Override
public ConnectorIndex getIndex(ConnectorTransactionHandle transaction, ConnectorSession session, ConnectorIndexHandle indexHandle, List<ColumnHandle> lookupSchema, List<ColumnHandle> outputSchema) {
    TpchIndexHandle tpchIndexHandle = (TpchIndexHandle) indexHandle;
    Map<ColumnHandle, NullableValue> fixedValues = TupleDomain.extractFixedValues(tpchIndexHandle.getFixedValues()).get();
    checkArgument(lookupSchema.stream().noneMatch(handle -> fixedValues.keySet().contains(handle)), "Lookup columnHandles are not expected to overlap with the fixed value predicates");
    // Establish an order for the fixedValues
    List<ColumnHandle> fixedValueColumns = ImmutableList.copyOf(fixedValues.keySet());
    // Extract the fixedValues as their raw values and types
    List<Object> rawFixedValues = new ArrayList<>(fixedValueColumns.size());
    List<Type> rawFixedTypes = new ArrayList<>(fixedValueColumns.size());
    for (ColumnHandle fixedValueColumn : fixedValueColumns) {
        rawFixedValues.add(fixedValues.get(fixedValueColumn).getValue());
        rawFixedTypes.add(((TpchColumnHandle) fixedValueColumn).getType());
    }
    // Establish the schema after we append the fixed values to the lookup keys.
    List<ColumnHandle> finalLookupSchema = ImmutableList.<ColumnHandle>builder().addAll(lookupSchema).addAll(fixedValueColumns).build();
    Optional<TpchIndexedData.IndexedTable> indexedTable = indexedData.getIndexedTable(tpchIndexHandle.getTableName(), tpchIndexHandle.getScaleFactor(), tpchIndexHandle.getIndexColumnNames());
    checkState(indexedTable.isPresent());
    TpchIndexedData.IndexedTable table = indexedTable.get();
    // Compute how to map from the final lookup schema to the table index key order
    List<Integer> keyRemap = computeRemap(handleToNames(finalLookupSchema), table.getKeyColumns());
    Function<RecordSet, RecordSet> keyFormatter = key -> new MappedRecordSet(new AppendingRecordSet(key, rawFixedValues, rawFixedTypes), keyRemap);
    // Compute how to map from the output of the indexed data to the expected output schema
    List<Integer> outputRemap = computeRemap(table.getOutputColumns(), handleToNames(outputSchema));
    Function<RecordSet, RecordSet> outputFormatter = output -> new MappedRecordSet(output, outputRemap);
    return new TpchConnectorIndex(keyFormatter, outputFormatter, table);
}
Also used : TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) NullableValue(io.trino.spi.predicate.NullableValue) Type(io.trino.spi.type.Type) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) Function(java.util.function.Function) ConnectorIndexProvider(io.trino.spi.connector.ConnectorIndexProvider) ArrayList(java.util.ArrayList) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) RecordSet(io.trino.spi.connector.RecordSet) Optional(java.util.Optional) ConnectorIndexHandle(io.trino.spi.connector.ConnectorIndexHandle) ConnectorIndex(io.trino.spi.connector.ConnectorIndex) MappedRecordSet(io.trino.split.MappedRecordSet) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) ArrayList(java.util.ArrayList) NullableValue(io.trino.spi.predicate.NullableValue) Type(io.trino.spi.type.Type) MappedRecordSet(io.trino.split.MappedRecordSet) RecordSet(io.trino.spi.connector.RecordSet) MappedRecordSet(io.trino.split.MappedRecordSet)

Aggregations

MappedRecordSet (io.trino.split.MappedRecordSet)3 ImmutableList (com.google.common.collect.ImmutableList)2 ColumnHandle (io.trino.spi.connector.ColumnHandle)2 RecordSet (io.trino.spi.connector.RecordSet)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 TpchColumnHandle (io.trino.plugin.tpch.TpchColumnHandle)1 TrinoException (io.trino.spi.TrinoException)1 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)1 ConnectorIndex (io.trino.spi.connector.ConnectorIndex)1 ConnectorIndexHandle (io.trino.spi.connector.ConnectorIndexHandle)1 ConnectorIndexProvider (io.trino.spi.connector.ConnectorIndexProvider)1 ConnectorSession (io.trino.spi.connector.ConnectorSession)1 ConnectorTransactionHandle (io.trino.spi.connector.ConnectorTransactionHandle)1 EmptyPageSource (io.trino.spi.connector.EmptyPageSource)1 RecordPageSource (io.trino.spi.connector.RecordPageSource)1 SchemaTableName (io.trino.spi.connector.SchemaTableName)1 SystemTable (io.trino.spi.connector.SystemTable)1 NullableValue (io.trino.spi.predicate.NullableValue)1