diff --git a/core/trino-main/src/main/java/io/trino/execution/RenameColumnTask.java b/core/trino-main/src/main/java/io/trino/execution/RenameColumnTask.java index e6a2d19bf498..b5d98dc8aaac 100644 --- a/core/trino-main/src/main/java/io/trino/execution/RenameColumnTask.java +++ b/core/trino-main/src/main/java/io/trino/execution/RenameColumnTask.java @@ -18,6 +18,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.connector.ColumnHandle; @@ -28,7 +29,6 @@ import java.util.List; import java.util.Map; -import java.util.Optional; import static com.google.common.util.concurrent.Futures.immediateVoidFuture; import static io.trino.metadata.MetadataUtil.createQualifiedObjectName; @@ -67,20 +67,20 @@ public ListenableFuture execute( WarningCollector warningCollector) { Session session = stateMachine.getSession(); - QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTable()); - Optional tableHandleOptional = metadata.getTableHandle(session, tableName); - if (tableHandleOptional.isEmpty()) { + QualifiedObjectName originalTableName = createQualifiedObjectName(session, statement, statement.getTable()); + RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, originalTableName); + if (redirectionAwareTableHandle.getTableHandle().isEmpty()) { if (!statement.isTableExists()) { - throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName); + throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", originalTableName); } return immediateVoidFuture(); } - TableHandle tableHandle = tableHandleOptional.get(); + TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get(); String source = statement.getSource().getValue().toLowerCase(ENGLISH); String target = statement.getTarget().getValue().toLowerCase(ENGLISH); - accessControl.checkCanRenameColumn(session.toSecurityContext(), tableName); + accessControl.checkCanRenameColumn(session.toSecurityContext(), redirectionAwareTableHandle.getRedirectedTableName().orElse(originalTableName)); Map columnHandles = metadata.getColumnHandles(session, tableHandle); ColumnHandle columnHandle = columnHandles.get(source); 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 d2146c4f504f..ad0bbe435e8d 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 @@ -352,6 +352,26 @@ public void testAlterTableDropColumn() onTrino().executeQuery("DROP TABLE " + icebergTableName); } + @Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS}) + public void testAlterTableRenameColumn() + { + String tableName = "iceberg_alter_table_" + randomTableSuffix(); + String hiveTableName = "hive.default." + tableName; + String icebergTableName = "iceberg.default." + tableName; + + createIcebergTable(icebergTableName, false); + + onTrino().executeQuery("ALTER TABLE " + hiveTableName + " RENAME COLUMN nationkey TO nation_key"); + + Assertions.assertThat(onTrino().executeQuery("DESCRIBE " + icebergTableName).column(1)) + .containsOnly("nation_key", "name", "regionkey", "comment"); + + assertResultsEqual( + onTrino().executeQuery("TABLE " + icebergTableName), + onTrino().executeQuery("SELECT nationkey as nation_key, name, regionkey, comment FROM tpch.tiny.nation")); + onTrino().executeQuery("DROP TABLE " + icebergTableName); + } + @Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS}) public void testCommentTable() { diff --git a/testing/trino-product-tests/src/main/java/io/trino/tests/product/iceberg/TestIcebergRedirectionToHive.java b/testing/trino-product-tests/src/main/java/io/trino/tests/product/iceberg/TestIcebergRedirectionToHive.java index e699b89af8b3..163805412569 100644 --- a/testing/trino-product-tests/src/main/java/io/trino/tests/product/iceberg/TestIcebergRedirectionToHive.java +++ b/testing/trino-product-tests/src/main/java/io/trino/tests/product/iceberg/TestIcebergRedirectionToHive.java @@ -315,6 +315,22 @@ public void testAlterTableAddColumn() onTrino().executeQuery("DROP TABLE " + hiveTableName); } + @Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS}) + public void testAlterTableRenameColumn() + { + String tableName = "hive_rename_column_" + randomTableSuffix(); + String hiveTableName = "hive.default." + tableName; + String icebergTableName = "iceberg.default." + tableName; + + createHiveTable(hiveTableName, false); + + // TODO: support redirects from Iceberg to Hive + assertQueryFailure(() -> onTrino().executeQuery("ALTER TABLE " + icebergTableName + " RENAME COLUMN nationkey TO nation_key")) + .hasMessageMatching("\\QQuery failed (#\\E\\S+\\Q): Not an Iceberg table: default." + tableName); + + onTrino().executeQuery("DROP TABLE " + hiveTableName); + } + @Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS}) public void testAlterTableDropColumn() {