Search in sources :

Example 1 with SystemTable

use of io.trino.spi.connector.SystemTable 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 SystemTable

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

the class SystemSplitManager method getSplits.

@Override
public ConnectorSplitSource getSplits(ConnectorTransactionHandle transaction, ConnectorSession session, ConnectorTableHandle tableHandle, SplitSchedulingStrategy splitSchedulingStrategy, DynamicFilter dynamicFilter) {
    SystemTableHandle table = (SystemTableHandle) tableHandle;
    TupleDomain<ColumnHandle> constraint = table.getConstraint();
    SystemTable systemTable = tables.getSystemTable(session, table.getSchemaTableName()).orElseThrow(() -> new TableNotFoundException(table.getSchemaTableName()));
    Distribution tableDistributionMode = systemTable.getDistribution();
    if (tableDistributionMode == SINGLE_COORDINATOR) {
        HostAddress address = nodeManager.getCurrentNode().getHostAndPort();
        ConnectorSplit split = new SystemSplit(address, constraint);
        return new FixedSplitSource(ImmutableList.of(split));
    }
    ImmutableList.Builder<ConnectorSplit> splits = ImmutableList.builder();
    ImmutableSet.Builder<InternalNode> nodes = ImmutableSet.builder();
    if (tableDistributionMode == ALL_COORDINATORS) {
        nodes.addAll(nodeManager.getCoordinators());
    } else if (tableDistributionMode == ALL_NODES) {
        nodes.addAll(nodeManager.getNodes(ACTIVE));
    }
    Set<InternalNode> nodeSet = nodes.build();
    for (InternalNode node : nodeSet) {
        splits.add(new SystemSplit(node.getHostAndPort(), constraint));
    }
    return new FixedSplitSource(splits.build());
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) ImmutableList(com.google.common.collect.ImmutableList) HostAddress(io.trino.spi.HostAddress) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) ImmutableSet(com.google.common.collect.ImmutableSet) FixedSplitSource(io.trino.spi.connector.FixedSplitSource) Distribution(io.trino.spi.connector.SystemTable.Distribution) SystemTable(io.trino.spi.connector.SystemTable) InternalNode(io.trino.metadata.InternalNode) ConnectorSplit(io.trino.spi.connector.ConnectorSplit)

Example 3 with SystemTable

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

the class SystemTablesMetadata method listTableColumns.

@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix) {
    requireNonNull(prefix, "prefix is null");
    if (prefix.getTable().isPresent()) {
        // if table is concrete we just use tables.getSystemTable to support tables which are not listable
        SchemaTableName tableName = prefix.toSchemaTableName();
        return tables.getSystemTable(session, tableName).map(systemTable -> singletonMap(tableName, systemTable.getTableMetadata().getColumns())).orElseGet(ImmutableMap::of);
    }
    ImmutableMap.Builder<SchemaTableName, List<ColumnMetadata>> builder = ImmutableMap.builder();
    for (SystemTable table : tables.listSystemTables(session)) {
        ConnectorTableMetadata tableMetadata = table.getTableMetadata();
        if (prefix.matches(tableMetadata.getTable())) {
            builder.put(tableMetadata.getTable(), tableMetadata.getColumns());
        }
    }
    return builder.buildOrThrow();
}
Also used : Constraint(io.trino.spi.connector.Constraint) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SystemColumnHandle.toSystemColumnHandles(io.trino.connector.system.SystemColumnHandle.toSystemColumnHandles) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) Collections.singletonMap(java.util.Collections.singletonMap) JdbcTable(io.trino.connector.system.jdbc.JdbcTable) MetadataUtil.findColumnMetadata(io.trino.metadata.MetadataUtil.findColumnMetadata) NOT_FOUND(io.trino.spi.StandardErrorCode.NOT_FOUND) ConstraintApplicationResult(io.trino.spi.connector.ConstraintApplicationResult) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TrinoException(io.trino.spi.TrinoException) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) Sets(com.google.common.collect.Sets) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) List(java.util.List) ConnectorTableProperties(io.trino.spi.connector.ConnectorTableProperties) SchemaTablePrefix(io.trino.spi.connector.SchemaTablePrefix) Optional(java.util.Optional) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) SystemTable(io.trino.spi.connector.SystemTable) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) SystemTable(io.trino.spi.connector.SystemTable) SchemaTableName(io.trino.spi.connector.SchemaTableName) ImmutableMap(com.google.common.collect.ImmutableMap) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Example 4 with SystemTable

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

the class SystemTablesMetadata method applyFilter.

@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle handle, Constraint constraint) {
    SystemTableHandle table = (SystemTableHandle) handle;
    TupleDomain<ColumnHandle> oldDomain = table.getConstraint();
    TupleDomain<ColumnHandle> newDomain = oldDomain.intersect(constraint.getSummary());
    if (oldDomain.equals(newDomain) && constraint.predicate().isEmpty()) {
        return Optional.empty();
    }
    SystemTable systemTable = checkAndGetTable(session, table);
    if (systemTable instanceof JdbcTable) {
        TupleDomain<ColumnHandle> filtered = ((JdbcTable) systemTable).applyFilter(session, effectiveConstraint(oldDomain, constraint, newDomain));
        newDomain = newDomain.intersect(filtered);
    }
    if (oldDomain.equals(newDomain)) {
        return Optional.empty();
    }
    if (newDomain.isNone()) {
    // TODO (https://github.com/trinodb/trino/issues/3647) indicate the table scan is empty
    }
    table = new SystemTableHandle(table.getSchemaName(), table.getTableName(), newDomain);
    return Optional.of(new ConstraintApplicationResult<>(table, constraint.getSummary(), false));
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) SystemTable(io.trino.spi.connector.SystemTable) JdbcTable(io.trino.connector.system.jdbc.JdbcTable)

Example 5 with SystemTable

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

the class PropertiesSystemTableProvider method getSystemTable.

@Override
public Optional<SystemTable> getSystemTable(HiveMetadata metadata, ConnectorSession session, SchemaTableName tableName) {
    if (!PROPERTIES.matches(tableName)) {
        return Optional.empty();
    }
    SchemaTableName sourceTableName = PROPERTIES.getSourceTableName(tableName);
    Table table = metadata.getMetastore().getTable(sourceTableName.getSchemaName(), sourceTableName.getTableName()).orElseThrow(() -> new TableNotFoundException(tableName));
    if (isDeltaLakeTable(table) || isIcebergTable(table)) {
        return Optional.empty();
    }
    Map<String, String> sortedTableParameters = ImmutableSortedMap.copyOf(table.getParameters());
    List<ColumnMetadata> columns = sortedTableParameters.keySet().stream().map(key -> new ColumnMetadata(key, VarcharType.VARCHAR)).collect(toImmutableList());
    List<Type> types = columns.stream().map(ColumnMetadata::getType).collect(toImmutableList());
    Iterable<List<Object>> propertyValues = ImmutableList.of(ImmutableList.copyOf(sortedTableParameters.values()));
    return Optional.of(createSystemTable(new ConnectorTableMetadata(sourceTableName, columns), constraint -> new InMemoryRecordSet(types, propertyValues).cursor()));
}
Also used : HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) Table(io.trino.plugin.hive.metastore.Table) PROPERTIES(io.trino.plugin.hive.SystemTableHandler.PROPERTIES) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) HiveUtil.isDeltaLakeTable(io.trino.plugin.hive.util.HiveUtil.isDeltaLakeTable) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Type(io.trino.spi.type.Type) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) ConnectorSession(io.trino.spi.connector.ConnectorSession) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) SchemaTableName(io.trino.spi.connector.SchemaTableName) VarcharType(io.trino.spi.type.VarcharType) List(java.util.List) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Optional(java.util.Optional) SystemTables.createSystemTable(io.trino.plugin.hive.util.SystemTables.createSystemTable) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SystemTable(io.trino.spi.connector.SystemTable) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isDeltaLakeTable(io.trino.plugin.hive.util.HiveUtil.isDeltaLakeTable) SystemTables.createSystemTable(io.trino.plugin.hive.util.SystemTables.createSystemTable) SystemTable(io.trino.spi.connector.SystemTable) SchemaTableName(io.trino.spi.connector.SchemaTableName) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Aggregations

SystemTable (io.trino.spi.connector.SystemTable)10 SchemaTableName (io.trino.spi.connector.SchemaTableName)6 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 TrinoException (io.trino.spi.TrinoException)5 ColumnHandle (io.trino.spi.connector.ColumnHandle)5 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)5 ConnectorSession (io.trino.spi.connector.ConnectorSession)5 ImmutableList (com.google.common.collect.ImmutableList)4 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)4 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)4 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Constraint (io.trino.spi.connector.Constraint)3 InMemoryRecordSet (io.trino.spi.connector.InMemoryRecordSet)3 Type (io.trino.spi.type.Type)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2