diff --git a/core/trino-main/src/main/java/io/trino/execution/RenameTableTask.java b/core/trino-main/src/main/java/io/trino/execution/RenameTableTask.java index 04e72d05dcbc..45e0957f1b32 100644 --- a/core/trino-main/src/main/java/io/trino/execution/RenameTableTask.java +++ b/core/trino-main/src/main/java/io/trino/execution/RenameTableTask.java @@ -19,6 +19,7 @@ import io.trino.execution.warnings.WarningCollector; import io.trino.metadata.Metadata; import io.trino.metadata.QualifiedObjectName; +import io.trino.metadata.RedirectionAwareTableHandle; import io.trino.metadata.TableHandle; import io.trino.security.AccessControl; import io.trino.spi.TrinoException; @@ -29,7 +30,6 @@ import javax.inject.Inject; import java.util.List; -import java.util.Optional; import static com.google.common.util.concurrent.Futures.immediateVoidFuture; import static io.trino.metadata.MetadataUtil.createQualifiedObjectName; @@ -91,27 +91,29 @@ public ListenableFuture execute( return immediateVoidFuture(); } - Optional tableHandle = metadata.getTableHandle(session, tableName); - if (tableHandle.isEmpty()) { + RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, tableName); + if (redirectionAwareTableHandle.getTableHandle().isEmpty()) { if (!statement.isExists()) { throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName); } return immediateVoidFuture(); } - QualifiedObjectName target = createTargetQualifiedObjectName(tableName, statement.getTarget()); + TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get(); + QualifiedObjectName source = redirectionAwareTableHandle.getRedirectedTableName().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()); } if (metadata.getTableHandle(session, target).isPresent()) { throw semanticException(TABLE_ALREADY_EXISTS, statement, "Target table '%s' already exists", target); } - if (!tableName.getCatalogName().equals(target.getCatalogName())) { + if (!tableHandle.getCatalogName().getCatalogName().equals(target.getCatalogName())) { throw semanticException(NOT_SUPPORTED, statement, "Table rename across catalogs is not supported"); } - accessControl.checkCanRenameTable(session.toSecurityContext(), tableName, target); + accessControl.checkCanRenameTable(session.toSecurityContext(), source, target); - metadata.renameTable(session, tableHandle.get(), target); + metadata.renameTable(session, tableHandle, target); return immediateVoidFuture(); } diff --git a/testing/trino-product-tests/src/main/java/io/trino/tests/product/hive/TestHiveRedirectionToIceberg.java b/testing/trino-product-tests/src/main/java/io/trino/tests/product/hive/TestHiveRedirectionToIceberg.java index eaa5b1473e2f..879b87b67317 100644 --- a/testing/trino-product-tests/src/main/java/io/trino/tests/product/hive/TestHiveRedirectionToIceberg.java +++ b/testing/trino-product-tests/src/main/java/io/trino/tests/product/hive/TestHiveRedirectionToIceberg.java @@ -305,11 +305,21 @@ public void testAlterTableRename() createIcebergTable(icebergTableName, false); - //TODO restore test assertions after adding redirection awareness to the RenameTableTask - assertQueryFailure(() -> onTrino().executeQuery("ALTER TABLE " + hiveTableName + " RENAME TO " + tableName + "_new")) - .hasMessageMatching("\\QQuery failed (#\\E\\S+\\Q): Cannot query Iceberg table 'default." + tableName + "'"); + assertQueryFailure(() -> onTrino().executeQuery("ALTER TABLE " + hiveTableName + " RENAME TO hive.default." + tableName + "_new")) + .hasMessageMatching("\\QQuery failed (#\\E\\S+\\Q): line 1:1: Table rename across catalogs is not supported"); - onTrino().executeQuery("DROP TABLE " + icebergTableName); + String newTableNameWithoutCatalogWithoutSchema = tableName + "_new_without_catalog_without_schema"; + onTrino().executeQuery("ALTER TABLE " + hiveTableName + " RENAME TO " + newTableNameWithoutCatalogWithoutSchema); + String newTableNameWithoutCatalogWithSchema = tableName + "_new_without_catalog_with_schema"; + onTrino().executeQuery("ALTER TABLE hive.default." + newTableNameWithoutCatalogWithoutSchema + " RENAME TO default." + newTableNameWithoutCatalogWithSchema); + String newTableNameWithCatalogWithSchema = tableName + "_new_with_catalog_with_schema"; + onTrino().executeQuery("ALTER TABLE hive.default." + newTableNameWithoutCatalogWithSchema + " RENAME TO iceberg.default." + newTableNameWithCatalogWithSchema); + + assertResultsEqual( + onTrino().executeQuery("TABLE " + icebergTableName + "_new_with_catalog_with_schema"), + onTrino().executeQuery("TABLE " + hiveTableName + "_new_with_catalog_with_schema")); + + onTrino().executeQuery("DROP TABLE " + icebergTableName + "_new_with_catalog_with_schema"); } @Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS})