Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Optional<ConnectorTableSchema> getRelationMetadata(ConnectorSession conne

Optional<TableHandle> tableHandle = metadata.getTableHandle(session, qualifiedName);
if (tableHandle.isPresent()) {
return Optional.of(metadata.getTableSchema(session, tableHandle.get()).getTableSchema());
return Optional.of(metadata.getTableSchema(session, tableHandle.get()).tableSchema());
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private RelationComment getTableCommentRedirectionAware(Session session, Qualifi
}
}

return new RelationComment(true, metadata.getTableMetadata(session, redirectionAware.tableHandle().get()).getMetadata().getComment());
return new RelationComment(true, metadata.getTableMetadata(session, redirectionAware.tableHandle().get()).metadata().getComment());
}

private record RelationComment(boolean found, Optional<String> comment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ else if (element instanceof LikeClause likeClause) {
throw semanticException(NOT_SUPPORTED, statement, "Only one LIKE clause can specify INCLUDING PROPERTIES");
}
includingProperties = true;
inheritedProperties = likeTableMetadata.getMetadata().getProperties();
inheritedProperties = likeTableMetadata.metadata().getProperties();
}

try {
accessControl.checkCanSelectFromColumns(
session.toSecurityContext(),
likeTableName,
likeTableMetadata.getColumns().stream()
likeTableMetadata.columns().stream()
.map(ColumnMetadata::getName)
.collect(toImmutableSet()));
}
Expand All @@ -263,7 +263,7 @@ else if (element instanceof LikeClause likeClause) {
}
}

likeTableMetadata.getColumns().stream()
likeTableMetadata.columns().stream()
.filter(column -> !column.isHidden())
.forEach(column -> {
if (columns.containsKey(column.getName().toLowerCase(Locale.ENGLISH))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ListenableFuture<Void> execute(
throw semanticException(NOT_SUPPORTED, statement, "Cannot drop hidden column");
}
if (fieldPath.isEmpty()) {
if (metadata.getTableMetadata(session, tableHandle).getColumns().stream()
if (metadata.getTableMetadata(session, tableHandle).columns().stream()
.filter(info -> !info.isHidden()).count() <= 1) {
throw semanticException(NOT_SUPPORTED, statement, "Cannot drop the only column in a table");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private static Map<SchemaTableName, List<ColumnMetadata>> doListTableColumns(Ses
throw e;
}

List<ColumnMetadata> columns = metadata.getTableMetadata(session, targetTableHandle).getColumns();
List<ColumnMetadata> columns = metadata.getTableMetadata(session, targetTableHandle).columns();

Set<String> allowedColumns = accessControl.filterColumns(
session.toSecurityContext(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public List<TableColumnsMetadata> listTableColumns(Session session, QualifiedTab
return Optional.of(RelationColumnsMetadata.forRedirectedTable(schemaTableName));
}
if (redirectionAware.tableHandle().isPresent()) {
return Optional.of(RelationColumnsMetadata.forTable(schemaTableName, getTableMetadata(session, redirectionAware.tableHandle().get()).getColumns()));
return Optional.of(RelationColumnsMetadata.forTable(schemaTableName, getTableMetadata(session, redirectionAware.tableHandle().get()).columns()));
}
}
catch (RuntimeException e) {
Expand Down
30 changes: 7 additions & 23 deletions core/trino-main/src/main/java/io/trino/metadata/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,32 @@

import static java.util.Objects.requireNonNull;

public class TableMetadata
public record TableMetadata(CatalogName catalogName, ConnectorTableMetadata metadata)
{
private final CatalogName catalogName;
private final ConnectorTableMetadata metadata;

public TableMetadata(CatalogName catalogName, ConnectorTableMetadata metadata)
public TableMetadata
{
requireNonNull(catalogName, "catalogName is null");
requireNonNull(metadata, "metadata is null");

this.catalogName = catalogName;
this.metadata = metadata;
}

public QualifiedObjectName getQualifiedName()
public QualifiedObjectName qualifiedName()
{
return new QualifiedObjectName(catalogName.toString(), metadata.getTable().getSchemaName(), metadata.getTable().getTableName());
}

public CatalogName getCatalogName()
{
return catalogName;
}

public ConnectorTableMetadata getMetadata()
{
return metadata;
}

public SchemaTableName getTable()
public SchemaTableName table()
{
return metadata.getTable();
}

public List<ColumnMetadata> getColumns()
public List<ColumnMetadata> columns()
{
return metadata.getColumns();
}

public ColumnMetadata getColumn(String name)
public ColumnMetadata column(String name)
{
return getColumns().stream()
return columns().stream()
.filter(columnMetadata -> columnMetadata.getName().equals(name))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid column name: " + name));
Expand Down
28 changes: 6 additions & 22 deletions core/trino-main/src/main/java/io/trino/metadata/TableSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,30 @@
import static com.google.common.collect.MoreCollectors.toOptional;
import static java.util.Objects.requireNonNull;

public final class TableSchema
public record TableSchema(CatalogName catalogName, ConnectorTableSchema tableSchema)
{
private final CatalogName catalogName;
private final ConnectorTableSchema tableSchema;

public TableSchema(CatalogName catalogName, ConnectorTableSchema tableSchema)
public TableSchema
{
requireNonNull(catalogName, "catalogName is null");
requireNonNull(tableSchema, "metadata is null");

this.catalogName = catalogName;
this.tableSchema = tableSchema;
}

public QualifiedObjectName getQualifiedName()
public QualifiedObjectName qualifiedName()
{
return new QualifiedObjectName(catalogName.toString(), tableSchema.getTable().getSchemaName(), tableSchema.getTable().getTableName());
}

public CatalogName getCatalogName()
{
return catalogName;
}

public ConnectorTableSchema getTableSchema()
{
return tableSchema;
}

public SchemaTableName getTable()
public SchemaTableName table()
{
return tableSchema.getTable();
}

public List<ColumnSchema> getColumns()
public List<ColumnSchema> columns()
{
return tableSchema.getColumns();
}

public ColumnSchema getColumn(String name)
public ColumnSchema column(String name)
{
return tableSchema.getColumns().stream()
.filter(columnMetadata -> columnMetadata.getName().equals(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,10 @@ protected Scope visitInsert(Insert insert, Optional<Scope> scope)

TableSchema tableSchema = metadata.getTableSchema(session, targetTableHandle.get());

List<ColumnSchema> columns = tableSchema.getColumns().stream()
List<ColumnSchema> columns = tableSchema.columns().stream()
.filter(column -> !column.isHidden())
.collect(toImmutableList());
List<String> checkConstraints = tableSchema.getTableSchema().getCheckConstraints();
List<String> checkConstraints = tableSchema.tableSchema().getCheckConstraints();

for (ColumnSchema column : columns) {
if (accessControl.getColumnMask(session.toSecurityContext(), targetTable, column.getName(), column.getType()).isPresent()) {
Expand Down Expand Up @@ -649,7 +649,7 @@ protected Scope visitInsert(Insert insert, Optional<Scope> scope)
newTableLayout));

List<Type> tableTypes = insertColumns.stream()
.map(insertColumn -> tableSchema.getColumn(insertColumn).getType())
.map(insertColumn -> tableSchema.column(insertColumn).getType())
.collect(toImmutableList());

List<Type> queryTypes = queryScope.getRelationType().getVisibleFields().stream()
Expand Down Expand Up @@ -721,7 +721,7 @@ protected Scope visitRefreshMaterializedView(RefreshMaterializedView refreshMate
analysis.setSkipMaterializedViewRefresh(metadata.getMaterializedViewFreshness(session, name).getFreshness() == FRESH);

TableMetadata tableMetadata = metadata.getTableMetadata(session, targetTableHandle);
List<String> insertColumns = tableMetadata.getColumns().stream()
List<String> insertColumns = tableMetadata.columns().stream()
.filter(column -> !column.isHidden())
.map(ColumnMetadata::getName)
.collect(toImmutableList());
Expand All @@ -734,7 +734,7 @@ protected Scope visitRefreshMaterializedView(RefreshMaterializedView refreshMate
insertColumns.stream().map(columnHandles::get).collect(toImmutableList())));

List<Type> tableTypes = insertColumns.stream()
.map(insertColumn -> tableMetadata.getColumn(insertColumn).getType())
.map(insertColumn -> tableMetadata.column(insertColumn).getType())
.collect(toImmutableList());

Stream<Column> columns = Streams.zip(
Expand Down Expand Up @@ -830,7 +830,7 @@ protected Scope visitDelete(Delete node, Optional<Scope> scope)
accessControl.checkCanDeleteFromTable(session.toSecurityContext(), tableName);

TableSchema tableSchema = metadata.getTableSchema(session, handle);
for (ColumnSchema tableColumn : tableSchema.getColumns()) {
for (ColumnSchema tableColumn : tableSchema.columns()) {
if (accessControl.getColumnMask(session.toSecurityContext(), tableName, tableColumn.getName(), tableColumn.getType()).isPresent()) {
throw semanticException(NOT_SUPPORTED, node, "Delete from table with column mask");
}
Expand All @@ -851,7 +851,7 @@ protected Scope visitDelete(Delete node, Optional<Scope> scope)
.withRelationType(RelationId.anonymous(), analysis.getScope(table).getRelationType())
.build();
analyzeFiltersAndMasks(table, tableName, analysis.getScope(table).getRelationType(), accessControlScope);
analyzeCheckConstraints(table, tableName, accessControlScope, tableSchema.getTableSchema().getCheckConstraints());
analyzeCheckConstraints(table, tableName, accessControlScope, tableSchema.tableSchema().getCheckConstraints());
analysis.registerTable(table, Optional.of(handle), tableName, session.getIdentity().getUser(), accessControlScope, Optional.empty());

createMergeAnalysis(table, handle, tableSchema, tableScope, tableScope, ImmutableList.of());
Expand Down Expand Up @@ -1257,7 +1257,7 @@ protected Scope visitTableExecute(TableExecute node, Optional<Scope> scope)
}

TableMetadata tableMetadata = metadata.getTableMetadata(session, tableHandle);
for (ColumnMetadata tableColumn : tableMetadata.getColumns()) {
for (ColumnMetadata tableColumn : tableMetadata.columns()) {
if (accessControl.getColumnMask(session.toSecurityContext(), tableName, tableColumn.getName(), tableColumn.getType()).isPresent()) {
throw semanticException(NOT_SUPPORTED, node, "ALTER TABLE EXECUTE is not supported for table with column masks");
}
Expand Down Expand Up @@ -2642,7 +2642,7 @@ private List<Field> analyzeTableOutputFields(Table table, QualifiedObjectName ta
{
// TODO: discover columns lazily based on where they are needed (to support connectors that can't enumerate all tables)
ImmutableList.Builder<Field> fields = ImmutableList.builder();
for (ColumnSchema column : tableSchema.getColumns()) {
for (ColumnSchema column : tableSchema.columns()) {
Field field = Field.newQualified(
table.getName(),
Optional.of(column.getName()),
Expand Down Expand Up @@ -3366,7 +3366,7 @@ protected Scope visitUpdate(Update update, Optional<Scope> scope)

TableSchema tableSchema = metadata.getTableSchema(session, handle);

List<ColumnSchema> allColumns = tableSchema.getColumns();
List<ColumnSchema> allColumns = tableSchema.columns();
Map<String, ColumnSchema> columns = allColumns.stream()
.collect(toImmutableMap(ColumnSchema::getName, Function.identity()));

Expand Down Expand Up @@ -3411,7 +3411,7 @@ protected Scope visitUpdate(Update update, Optional<Scope> scope)

Scope tableScope = analyzer.analyzeForUpdate(table, scope, UpdateKind.UPDATE);
update.getWhere().ifPresent(where -> analyzeWhere(update, tableScope, where));
analyzeCheckConstraints(table, tableName, tableScope, tableSchema.getTableSchema().getCheckConstraints());
analyzeCheckConstraints(table, tableName, tableScope, tableSchema.tableSchema().getCheckConstraints());
analysis.registerTable(table, redirection.tableHandle(), tableName, session.getIdentity().getUser(), tableScope, Optional.empty());

ImmutableList.Builder<ExpressionAnalysis> analysesBuilder = ImmutableList.builder();
Expand Down Expand Up @@ -3499,7 +3499,7 @@ protected Scope visitMerge(Merge merge, Optional<Scope> scope)

TableSchema tableSchema = metadata.getTableSchema(session, targetTableHandle);

List<ColumnSchema> dataColumnSchemas = tableSchema.getColumns().stream()
List<ColumnSchema> dataColumnSchemas = tableSchema.columns().stream()
.filter(column -> !column.isHidden())
.collect(toImmutableList());

Expand Down Expand Up @@ -3532,7 +3532,7 @@ protected Scope visitMerge(Merge merge, Optional<Scope> scope)
Scope targetTableScope = analyzer.analyzeForUpdate(relation, Optional.of(mergeScope), UpdateKind.MERGE);
Scope sourceTableScope = process(merge.getSource(), mergeScope);
Scope joinScope = createAndAssignScope(merge, Optional.of(mergeScope), targetTableScope.getRelationType().joinWith(sourceTableScope.getRelationType()));
analyzeCheckConstraints(table, tableName, targetTableScope, tableSchema.getTableSchema().getCheckConstraints());
analyzeCheckConstraints(table, tableName, targetTableScope, tableSchema.tableSchema().getCheckConstraints());
analysis.registerTable(table, redirection.tableHandle(), tableName, session.getIdentity().getUser(), targetTableScope, Optional.empty());

for (ColumnSchema column : dataColumnSchemas) {
Expand Down Expand Up @@ -3657,7 +3657,7 @@ private void createMergeAnalysis(Table table, TableHandle handle, TableSchema ta
}
Map<ColumnHandle, Integer> columnHandleFieldNumbers = columnHandleFieldNumbersBuilder.buildOrThrow();

List<ColumnSchema> dataColumnSchemas = tableSchema.getColumns().stream()
List<ColumnSchema> dataColumnSchemas = tableSchema.columns().stream()
.filter(column -> !column.isHidden())
.collect(toImmutableList());
Optional<TableLayout> insertLayout = metadata.getInsertLayout(session, handle);
Expand All @@ -3682,7 +3682,7 @@ private void createMergeAnalysis(Table table, TableHandle handle, TableSchema ta
.map(fieldIndexes::get)
.collect(toImmutableList());

Set<ColumnHandle> nonNullableColumnHandles = metadata.getTableMetadata(session, handle).getColumns().stream()
Set<ColumnHandle> nonNullableColumnHandles = metadata.getTableMetadata(session, handle).columns().stream()
.filter(column -> !column.isNullable())
.map(ColumnMetadata::getName)
.map(allColumnHandles::get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ private RelationPlan createAnalyzePlan(Analysis analysis, Analyze analyzeStateme
ImmutableMap.Builder<Symbol, ColumnHandle> symbolToColumnHandle = ImmutableMap.builder();
ImmutableMap.Builder<String, Symbol> columnNameToSymbol = ImmutableMap.builder();
TableMetadata tableMetadata = metadata.getTableMetadata(session, targetTable);
for (ColumnMetadata column : tableMetadata.getColumns()) {
for (ColumnMetadata column : tableMetadata.columns()) {
Symbol symbol = symbolAllocator.newSymbol(column.getName(), column.getType());
tableScanOutputs.add(symbol);
symbolToColumnHandle.put(symbol, columnHandles.get(column.getName()));
Expand Down Expand Up @@ -522,7 +522,7 @@ private RelationPlan getInsertPlan(
boolean supportsMissingColumnsOnInsert = metadata.supportsMissingColumnsOnInsert(session, tableHandle);
ImmutableList.Builder<ColumnMetadata> insertedColumnsBuilder = ImmutableList.builder();

for (ColumnMetadata column : tableMetadata.getColumns()) {
for (ColumnMetadata column : tableMetadata.columns()) {
if (column.isHidden()) {
continue;
}
Expand Down Expand Up @@ -588,7 +588,7 @@ private RelationPlan getInsertPlan(
.map(ColumnMetadata::getName)
.collect(toImmutableList());

TableStatisticsMetadata statisticsMetadata = metadata.getStatisticsCollectionMetadataForWrite(session, tableHandle.getCatalogHandle(), tableMetadata.getMetadata());
TableStatisticsMetadata statisticsMetadata = metadata.getStatisticsCollectionMetadataForWrite(session, tableHandle.getCatalogHandle(), tableMetadata.metadata());

if (materializedViewRefreshWriterTarget.isPresent()) {
return createTableWriterPlan(
Expand Down Expand Up @@ -969,7 +969,7 @@ private RelationPlan createTableExecutePlan(Analysis analysis, TableExecute stat
PlanNode sourcePlanRoot = sourcePlanBuilder.getRoot();

TableMetadata tableMetadata = metadata.getTableMetadata(session, tableHandle);
List<String> columnNames = tableMetadata.getColumns().stream()
List<String> columnNames = tableMetadata.columns().stream()
.filter(column -> !column.isHidden()) // todo this filter is redundant
.map(ColumnMetadata::getName)
.collect(toImmutableList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public PlanNode plan(Delete node)
TableMetadata tableMetadata = plannerContext.getMetadata().getTableMetadata(session, handle);
ImmutableList.Builder<Type> typeBuilder = ImmutableList.builder();
ImmutableList.Builder<String> namesBuilder = ImmutableList.builder();
tableMetadata.getMetadata().getColumns().stream()
tableMetadata.metadata().getColumns().stream()
.filter(column -> !column.isHidden())
.forEach(columnMetadata -> {
typeBuilder.add(columnMetadata.getType());
Expand Down Expand Up @@ -569,7 +569,7 @@ public PlanNode plan(Delete node)
new MergeTarget(
handle,
Optional.empty(),
tableMetadata.getTable(),
tableMetadata.table(),
paradigmAndTypes),
projectNode.getOutputSymbols(),
partitioningScheme,
Expand Down
Loading