Skip to content

Commit

Permalink
Convert RedirectionAwareTableHandle to a record
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Aug 3, 2023
1 parent d0193cd commit 2cbd054
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private Set<QualifiedTablePrefix> calculatePrefixesWithTableName(
// Table redirection to enumerate columns from target table happens later in
// MetadataListing#listTableColumns, but also applying it here to avoid incorrect
// filtering in case the source table does not exist or there is a problem with redirection.
return metadata.getRedirectionAwareTableHandle(session, objectName).getTableHandle().isPresent();
return metadata.getRedirectionAwareTableHandle(session, objectName).tableHandle().isPresent();
}
catch (TrinoException e) {
if (e.getErrorCode().equals(TABLE_REDIRECTION_ERROR.toErrorCode())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private Optional<String> getComment(
return viewInfo.getComment();
}
QualifiedObjectName tableName = new QualifiedObjectName(prefix.getCatalogName(), name.getSchemaName(), name.getTableName());
return metadata.getRedirectionAwareTableHandle(session, tableName).getTableHandle()
return metadata.getRedirectionAwareTableHandle(session, tableName).tableHandle()
.map(handle -> metadata.getTableMetadata(session, handle))
.map(metadata -> metadata.getMetadata().getComment())
.orElseGet(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ public ListenableFuture<Void> execute(
Session session = stateMachine.getSession();
QualifiedObjectName originalTableName = createQualifiedObjectName(session, statement, statement.getName());
RedirectionAwareTableHandle redirectionAwareTableHandle = plannerContext.getMetadata().getRedirectionAwareTableHandle(session, originalTableName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
if (!statement.isTableExists()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", originalTableName);
}
return immediateVoidFuture();
}
TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();
CatalogHandle catalogHandle = tableHandle.getCatalogHandle();

QualifiedObjectName qualifiedTableName = redirectionAwareTableHandle.getRedirectedTableName().orElse(originalTableName);
QualifiedObjectName qualifiedTableName = redirectionAwareTableHandle.redirectedTableName().orElse(originalTableName);

Map<String, ColumnHandle> columnHandles = plannerContext.getMetadata().getColumnHandles(session, tableHandle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ private void commentOnTable(Comment statement, Session session)
}

RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, originalTableName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table does not exist: %s", originalTableName);
}

accessControl.checkCanSetTableComment(session.toSecurityContext(), redirectionAwareTableHandle.getRedirectedTableName().orElse(originalTableName));
TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
accessControl.checkCanSetTableComment(session.toSecurityContext(), redirectionAwareTableHandle.redirectedTableName().orElse(originalTableName));
TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();
metadata.setTableComment(session, tableHandle, statement.getComment());
}

Expand Down Expand Up @@ -152,18 +152,18 @@ else if (metadata.isMaterializedView(session, originalObjectName)) {
}
else {
RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, originalObjectName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table does not exist: %s", originalObjectName);
}
TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();

String columnName = statement.getName().getSuffix();
Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
if (!columnHandles.containsKey(columnName)) {
throw semanticException(COLUMN_NOT_FOUND, statement, "Column does not exist: %s", columnName);
}

accessControl.checkCanSetColumnComment(session.toSecurityContext(), redirectionAwareTableHandle.getRedirectedTableName().orElse(originalObjectName));
accessControl.checkCanSetColumnComment(session.toSecurityContext(), redirectionAwareTableHandle.redirectedTableName().orElse(originalObjectName));

metadata.setColumnComment(session, tableHandle, columnHandles.get(columnName), statement.getComment());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ else if (element instanceof LikeClause likeClause) {
}

RedirectionAwareTableHandle redirection = plannerContext.getMetadata().getRedirectionAwareTableHandle(session, originalLikeTableName);
TableHandle likeTable = redirection.getTableHandle()
TableHandle likeTable = redirection.tableHandle()
.orElseThrow(() -> semanticException(TABLE_NOT_FOUND, statement, "LIKE table '%s' does not exist", originalLikeTableName));

LikeClause.PropertiesOption propertiesOption = likeClause.getPropertiesOption().orElse(EXCLUDING);
QualifiedObjectName likeTableName = redirection.getRedirectedTableName().orElse(originalLikeTableName);
QualifiedObjectName likeTableName = redirection.redirectedTableName().orElse(originalLikeTableName);
if (propertiesOption == INCLUDING && !catalogName.equals(likeTableName.getCatalogName())) {
if (!originalLikeTableName.equals(likeTableName)) {
throw semanticException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ private static void executeDenyOnTable(Session session, Deny statement, Metadata
{
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getName());
RedirectionAwareTableHandle redirection = metadata.getRedirectionAwareTableHandle(session, tableName);
if (redirection.getTableHandle().isEmpty()) {
if (redirection.tableHandle().isEmpty()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName);
}
if (redirection.getRedirectedTableName().isPresent()) {
throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and DENY is not supported with table redirections", tableName, redirection.getRedirectedTableName().get());
if (redirection.redirectedTableName().isPresent()) {
throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and DENY is not supported with table redirections", tableName, redirection.redirectedTableName().get());
}

Set<Privilege> privileges = parseStatementPrivileges(statement, statement.getPrivileges());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ public ListenableFuture<Void> execute(
Session session = stateMachine.getSession();
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTable());
RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, tableName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
if (!statement.isTableExists()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName);
}
return immediateVoidFuture();
}
TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();

// Use getParts method because the column name should be lowercase
String column = statement.getField().getParts().get(0);

QualifiedObjectName qualifiedTableName = redirectionAwareTableHandle.getRedirectedTableName().orElse(tableName);
QualifiedObjectName qualifiedTableName = redirectionAwareTableHandle.redirectedTableName().orElse(tableName);
accessControl.checkCanDropColumn(session.toSecurityContext(), qualifiedTableName);

ColumnHandle columnHandle = metadata.getColumnHandles(session, tableHandle).get(column);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ public ListenableFuture<Void> execute(
}

RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, originalTableName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
if (!statement.isExists()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", originalTableName);
}
return immediateVoidFuture();
}
QualifiedObjectName tableName = redirectionAwareTableHandle.getRedirectedTableName().orElse(originalTableName);
QualifiedObjectName tableName = redirectionAwareTableHandle.redirectedTableName().orElse(originalTableName);
accessControl.checkCanDropTable(session.toSecurityContext(), tableName);

metadata.dropTable(session, redirectionAwareTableHandle.getTableHandle().get(), tableName.asCatalogSchemaTableName());
metadata.dropTable(session, redirectionAwareTableHandle.tableHandle().get(), tableName.asCatalogSchemaTableName());

return immediateVoidFuture();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ private void executeGrantOnTable(Session session, Grant statement)
{
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getName());
RedirectionAwareTableHandle redirection = metadata.getRedirectionAwareTableHandle(session, tableName);
if (redirection.getTableHandle().isEmpty()) {
if (redirection.tableHandle().isEmpty()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName);
}
if (redirection.getRedirectedTableName().isPresent()) {
throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and GRANT is not supported with table redirections", tableName, redirection.getRedirectedTableName().get());
if (redirection.redirectedTableName().isPresent()) {
throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and GRANT is not supported with table redirections", tableName, redirection.redirectedTableName().get());
}

Set<Privilege> privileges = parseStatementPrivileges(statement, statement.getPrivileges());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ public ListenableFuture<Void> execute(
Session session = stateMachine.getSession();
QualifiedObjectName originalTableName = createQualifiedObjectName(session, statement, statement.getTable());
RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, originalTableName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
if (!statement.isTableExists()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", originalTableName);
}
return immediateVoidFuture();
}
TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();

String source = statement.getSource().getParts().get(0).toLowerCase(ENGLISH);
String target = statement.getTarget().getValue().toLowerCase(ENGLISH);

QualifiedObjectName qualifiedTableName = redirectionAwareTableHandle.getRedirectedTableName().orElse(originalTableName);
QualifiedObjectName qualifiedTableName = redirectionAwareTableHandle.redirectedTableName().orElse(originalTableName);

Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
ColumnHandle columnHandle = columnHandles.get(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public ListenableFuture<Void> execute(
}

RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, tableName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
if (!statement.isExists()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName);
}
return immediateVoidFuture();
}

TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
QualifiedObjectName source = redirectionAwareTableHandle.getRedirectedTableName().orElse(tableName);
TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();
QualifiedObjectName source = redirectionAwareTableHandle.redirectedTableName().orElse(tableName);
QualifiedObjectName target = createTargetQualifiedObjectName(source, statement.getTarget());
if (metadata.getCatalogHandle(session, target.getCatalogName()).isEmpty()) {
throw semanticException(CATALOG_NOT_FOUND, statement, "Target catalog '%s' does not exist", target.getCatalogName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ private void executeRevokeOnTable(Session session, Revoke statement)
{
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getName());
RedirectionAwareTableHandle redirection = metadata.getRedirectionAwareTableHandle(session, tableName);
if (redirection.getTableHandle().isEmpty()) {
if (redirection.tableHandle().isEmpty()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName);
}
if (redirection.getRedirectedTableName().isPresent()) {
throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and REVOKE is not supported with table redirections", tableName, redirection.getRedirectedTableName().get());
if (redirection.redirectedTableName().isPresent()) {
throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and REVOKE is not supported with table redirections", tableName, redirection.redirectedTableName().get());
}

Set<Privilege> privileges = parseStatementPrivileges(statement, statement.getPrivileges());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ListenableFuture<Void> execute(
Session session = stateMachine.getSession();
QualifiedObjectName qualifiedObjectName = createQualifiedObjectName(session, statement, statement.getTableName());
RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, qualifiedObjectName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (redirectionAwareTableHandle.tableHandle().isEmpty()) {
String exceptionMessage = format("Table '%s' does not exist", qualifiedObjectName);
if (metadata.getMaterializedView(session, qualifiedObjectName).isPresent()) {
exceptionMessage += ", but a materialized view with that name exists.";
Expand All @@ -98,9 +98,9 @@ else if (metadata.getView(session, qualifiedObjectName).isPresent()) {
return immediateVoidFuture();
}

accessControl.checkCanAlterColumn(session.toSecurityContext(), redirectionAwareTableHandle.getRedirectedTableName().orElse(qualifiedObjectName));
accessControl.checkCanAlterColumn(session.toSecurityContext(), redirectionAwareTableHandle.redirectedTableName().orElse(qualifiedObjectName));

TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
TableHandle tableHandle = redirectionAwareTableHandle.tableHandle().get();
Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
String columnName = statement.getColumnName().getParts().get(0).toLowerCase(ENGLISH);
ColumnHandle column = columnHandles.get(columnName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public ListenableFuture<Void> execute(

getRequiredCatalogHandle(metadata, session, statement, tableName.getCatalogName());
RedirectionAwareTableHandle redirection = metadata.getRedirectionAwareTableHandle(session, tableName);
if (redirection.getTableHandle().isEmpty()) {
if (redirection.tableHandle().isEmpty()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName);
}
if (redirection.getRedirectedTableName().isPresent()) {
throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and SET TABLE AUTHORIZATION is not supported with table redirections", tableName, redirection.getRedirectedTableName().get());
if (redirection.redirectedTableName().isPresent()) {
throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and SET TABLE AUTHORIZATION is not supported with table redirections", tableName, redirection.redirectedTableName().get());
}

TrinoPrincipal principal = createPrincipal(statement.getPrincipal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ private static Map<SchemaTableName, List<ColumnMetadata>> doListTableColumns(Ses
// For redirected tables, column listing requires special handling, because the column metadata is unavailable
// at the source table, and needs to be fetched from the target table.
RedirectionAwareTableHandle redirection = metadata.getRedirectionAwareTableHandle(session, originalTableName);
targetTableName = redirection.getRedirectedTableName();
targetTableName = redirection.redirectedTableName();

// The target table name should be non-empty. If it is empty, it means that there is an
// inconsistency in the connector's implementation of ConnectorMetadata#streamTableColumns and
// ConnectorMetadata#redirectTable.
if (targetTableName.isPresent()) {
redirectionSucceeded = true;
targetTableHandle = redirection.getTableHandle().orElseThrow();
targetTableHandle = redirection.tableHandle().orElseThrow();
}
}
catch (TrinoException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ private Optional<Boolean> isExistingRelationForListing(Session session, Qualifie

// TODO: consider a better way to resolve relation names: https://github.com/trinodb/trino/issues/9400
try {
return Optional.of(getRedirectionAwareTableHandle(session, name).getTableHandle().isPresent());
return Optional.of(getRedirectionAwareTableHandle(session, name).tableHandle().isPresent());
}
catch (TrinoException e) {
// ignore redirection errors for consistency with listing
Expand Down
Loading

0 comments on commit 2cbd054

Please sign in to comment.