Search in sources :

Example 1 with ViewInfo

use of io.trino.metadata.ViewInfo in project trino by trinodb.

the class TableCommentSystemTable method getComment.

private Optional<String> getComment(Session session, QualifiedTablePrefix prefix, SchemaTableName name, Map<SchemaTableName, ViewInfo> views, Map<SchemaTableName, ViewInfo> materializedViews) {
    ViewInfo materializedViewDefinition = materializedViews.get(name);
    if (materializedViewDefinition != null) {
        return materializedViewDefinition.getComment();
    }
    ViewInfo viewInfo = views.get(name);
    if (viewInfo != null) {
        return viewInfo.getComment();
    }
    QualifiedObjectName tableName = new QualifiedObjectName(prefix.getCatalogName(), name.getSchemaName(), name.getTableName());
    return metadata.getRedirectionAwareTableHandle(session, tableName).getTableHandle().map(handle -> metadata.getTableMetadata(session, handle)).map(metadata -> metadata.getMetadata().getComment()).orElseGet(() -> {
        // A previously listed table might have been dropped concurrently
        LOG.warn("Failed to get metadata for table: %s", name);
        return Optional.empty();
    });
}
Also used : FilterUtil.tryGetSingleVarcharValue(io.trino.connector.system.jdbc.FilterUtil.tryGetSingleVarcharValue) TableMetadataBuilder.tableMetadataBuilder(io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder) MetadataListing.getViews(io.trino.metadata.MetadataListing.getViews) Logger(io.airlift.log.Logger) FullConnectorSession(io.trino.FullConnectorSession) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) Inject(javax.inject.Inject) Map(java.util.Map) Sets.union(com.google.common.collect.Sets.union) Objects.requireNonNull(java.util.Objects.requireNonNull) ViewInfo(io.trino.metadata.ViewInfo) QualifiedTablePrefix(io.trino.metadata.QualifiedTablePrefix) MetadataListing.listTables(io.trino.metadata.MetadataListing.listTables) MetadataListing.listCatalogs(io.trino.metadata.MetadataListing.listCatalogs) SINGLE_COORDINATOR(io.trino.spi.connector.SystemTable.Distribution.SINGLE_COORDINATOR) ImmutableSet(com.google.common.collect.ImmutableSet) RecordCursor(io.trino.spi.connector.RecordCursor) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) Builder(io.trino.spi.connector.InMemoryRecordSet.Builder) SchemaTableName(io.trino.spi.connector.SchemaTableName) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) AccessControl(io.trino.security.AccessControl) MetadataListing.getMaterializedViews(io.trino.metadata.MetadataListing.getMaterializedViews) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) FilterUtil.tablePrefix(io.trino.connector.system.jdbc.FilterUtil.tablePrefix) SystemTable(io.trino.spi.connector.SystemTable) Session(io.trino.Session) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) ViewInfo(io.trino.metadata.ViewInfo)

Example 2 with ViewInfo

use of io.trino.metadata.ViewInfo in project trino by trinodb.

the class TableCommentSystemTable method cursor.

@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession connectorSession, TupleDomain<Integer> constraint) {
    Optional<String> catalogFilter = tryGetSingleVarcharValue(constraint, 0);
    Optional<String> schemaFilter = tryGetSingleVarcharValue(constraint, 1);
    Optional<String> tableFilter = tryGetSingleVarcharValue(constraint, 2);
    Session session = ((FullConnectorSession) connectorSession).getSession();
    Builder table = InMemoryRecordSet.builder(COMMENT_TABLE);
    for (String catalog : listCatalogs(session, metadata, accessControl, catalogFilter).keySet()) {
        QualifiedTablePrefix prefix = tablePrefix(catalog, schemaFilter, tableFilter);
        Set<SchemaTableName> names = ImmutableSet.of();
        Map<SchemaTableName, ViewInfo> views = ImmutableMap.of();
        Map<SchemaTableName, ViewInfo> materializedViews = ImmutableMap.of();
        try {
            materializedViews = getMaterializedViews(session, metadata, accessControl, prefix);
            views = getViews(session, metadata, accessControl, prefix);
            // Some connectors like blackhole, accumulo and raptor don't return views in listTables
            // Materialized views are consistently returned in listTables by the relevant connectors
            names = union(listTables(session, metadata, accessControl, prefix), views.keySet());
        } catch (TrinoException e) {
            // listTables throws an exception if cannot connect the database
            LOG.warn(e, "Failed to get tables for catalog: %s", catalog);
        }
        for (SchemaTableName name : names) {
            Optional<String> comment = Optional.empty();
            try {
                comment = getComment(session, prefix, name, views, materializedViews);
            } catch (RuntimeException e) {
                // getTableHandle may throw an exception (e.g. Cassandra connector doesn't allow case insensitive column names)
                LOG.warn(e, "Failed to get metadata for table: %s", name);
            }
            table.addRow(prefix.getCatalogName(), name.getSchemaName(), name.getTableName(), comment.orElse(null));
        }
    }
    return table.build().cursor();
}
Also used : QualifiedTablePrefix(io.trino.metadata.QualifiedTablePrefix) TableMetadataBuilder.tableMetadataBuilder(io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder) Builder(io.trino.spi.connector.InMemoryRecordSet.Builder) TrinoException(io.trino.spi.TrinoException) SchemaTableName(io.trino.spi.connector.SchemaTableName) FullConnectorSession(io.trino.FullConnectorSession) ConnectorSession(io.trino.spi.connector.ConnectorSession) Session(io.trino.Session) FullConnectorSession(io.trino.FullConnectorSession) ViewInfo(io.trino.metadata.ViewInfo)

Aggregations

FullConnectorSession (io.trino.FullConnectorSession)2 Session (io.trino.Session)2 TableMetadataBuilder.tableMetadataBuilder (io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder)2 QualifiedTablePrefix (io.trino.metadata.QualifiedTablePrefix)2 ViewInfo (io.trino.metadata.ViewInfo)2 TrinoException (io.trino.spi.TrinoException)2 ConnectorSession (io.trino.spi.connector.ConnectorSession)2 Builder (io.trino.spi.connector.InMemoryRecordSet.Builder)2 SchemaTableName (io.trino.spi.connector.SchemaTableName)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets.union (com.google.common.collect.Sets.union)1 Logger (io.airlift.log.Logger)1 FilterUtil.tablePrefix (io.trino.connector.system.jdbc.FilterUtil.tablePrefix)1 FilterUtil.tryGetSingleVarcharValue (io.trino.connector.system.jdbc.FilterUtil.tryGetSingleVarcharValue)1 Metadata (io.trino.metadata.Metadata)1 MetadataListing.getMaterializedViews (io.trino.metadata.MetadataListing.getMaterializedViews)1 MetadataListing.getViews (io.trino.metadata.MetadataListing.getViews)1 MetadataListing.listCatalogs (io.trino.metadata.MetadataListing.listCatalogs)1 MetadataListing.listTables (io.trino.metadata.MetadataListing.listTables)1