use of io.trino.spi.StandardErrorCode.INVALID_VIEW in project trino by trinodb.
the class MetadataManager method listTableColumns.
@Override
public List<TableColumnsMetadata> listTableColumns(Session session, QualifiedTablePrefix prefix) {
requireNonNull(prefix, "prefix is null");
Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName());
// Track column metadata for every object name to resolve ties between table and view
Map<SchemaTableName, Optional<List<ColumnMetadata>>> tableColumns = new HashMap<>();
if (catalog.isPresent()) {
CatalogMetadata catalogMetadata = catalog.get();
SchemaTablePrefix tablePrefix = prefix.asSchemaTablePrefix();
for (CatalogName catalogName : catalogMetadata.listConnectorIds()) {
ConnectorMetadata metadata = catalogMetadata.getMetadataFor(session, catalogName);
ConnectorSession connectorSession = session.toConnectorSession(catalogName);
// Collect column metadata from tables
metadata.streamTableColumns(connectorSession, tablePrefix).forEach(columnsMetadata -> tableColumns.put(columnsMetadata.getTable(), columnsMetadata.getColumns()));
// Collect column metadata from views. if table and view names overlap, the view wins
for (Entry<QualifiedObjectName, ViewInfo> entry : getViews(session, prefix).entrySet()) {
ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
for (ViewColumn column : entry.getValue().getColumns()) {
try {
columns.add(new ColumnMetadata(column.getName(), typeManager.getType(column.getType())));
} catch (TypeNotFoundException e) {
throw new TrinoException(INVALID_VIEW, format("Unknown type '%s' for column '%s' in view: %s", column.getType(), column.getName(), entry.getKey()));
}
}
tableColumns.put(entry.getKey().asSchemaTableName(), Optional.of(columns.build()));
}
// if view and materialized view names overlap, the materialized view wins
for (Entry<QualifiedObjectName, ViewInfo> entry : getMaterializedViews(session, prefix).entrySet()) {
ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
for (ViewColumn column : entry.getValue().getColumns()) {
try {
columns.add(new ColumnMetadata(column.getName(), typeManager.getType(column.getType())));
} catch (TypeNotFoundException e) {
throw new TrinoException(INVALID_VIEW, format("Unknown type '%s' for column '%s' in materialized view: %s", column.getType(), column.getName(), entry.getKey()));
}
}
tableColumns.put(entry.getKey().asSchemaTableName(), Optional.of(columns.build()));
}
}
}
return tableColumns.entrySet().stream().map(entry -> new TableColumnsMetadata(entry.getKey(), entry.getValue())).collect(toImmutableList());
}
Aggregations