diff --git a/core/trino-main/src/main/java/io/trino/sql/rewrite/ShowQueriesRewrite.java b/core/trino-main/src/main/java/io/trino/sql/rewrite/ShowQueriesRewrite.java index 3021163521c7..b5b10cea214c 100644 --- a/core/trino-main/src/main/java/io/trino/sql/rewrite/ShowQueriesRewrite.java +++ b/core/trino-main/src/main/java/io/trino/sql/rewrite/ShowQueriesRewrite.java @@ -43,6 +43,7 @@ import io.trino.spi.TrinoException; import io.trino.spi.connector.CatalogSchemaName; import io.trino.spi.connector.ConnectorTableMetadata; +import io.trino.spi.connector.ConnectorViewDefinition; import io.trino.spi.connector.SchemaTableName; import io.trino.spi.security.PrincipalType; import io.trino.spi.security.TrinoPrincipal; @@ -297,17 +298,21 @@ protected Node visitShowTables(ShowTables showTables, Void context) @Override protected Node visitShowGrants(ShowGrants showGrants, Void context) { - // TODO: make this method redirection aware String catalogName = session.getCatalog().orElse(null); Optional predicate = Optional.empty(); Optional tableName = showGrants.getTableName(); if (tableName.isPresent()) { QualifiedObjectName qualifiedTableName = createQualifiedObjectName(session, showGrants, tableName.get()); - - if (!metadata.isView(session, qualifiedTableName) && - metadata.getTableHandle(session, qualifiedTableName).isEmpty()) { - throw semanticException(TABLE_NOT_FOUND, showGrants, "Table '%s' does not exist", tableName); + if (!metadata.isView(session, qualifiedTableName)) { + RedirectionAwareTableHandle redirection = metadata.getRedirectionAwareTableHandle(session, qualifiedTableName); + Optional tableHandle = redirection.getTableHandle(); + if (tableHandle.isEmpty()) { + throw semanticException(TABLE_NOT_FOUND, showGrants, "Table '%s' does not exist", tableName); + } + if (redirection.getRedirectedTableName().isPresent()) { + throw semanticException(NOT_SUPPORTED, showGrants, "Table %s is redirected to %s and SHOW GRANTS is not supported with table redirections", tableName.get(), redirection.getRedirectedTableName().get()); + } } catalogName = qualifiedTableName.getCatalogName(); 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 0ce30a4aa016..1284708c80d6 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 @@ -374,6 +374,20 @@ public void testCommentColumn() onTrino().executeQuery("DROP TABLE " + icebergTableName); } + @Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS}) + public void testShowGrants() + { + String tableName = "iceberg_show_grants_" + randomTableSuffix(); + String hiveTableName = "hive.default." + tableName; + String icebergTableName = "iceberg.default." + tableName; + createIcebergTable(icebergTableName, false); + + assertQueryFailure(() -> onTrino().executeQuery(format("SHOW GRANTS ON %s", hiveTableName))) + .hasMessageMatching("\\QQuery failed (#\\E\\S+\\Q): line 1:1: Table " + hiveTableName + " is redirected to " + icebergTableName + " and SHOW GRANTS is not supported with table redirections"); + + onTrino().executeQuery("DROP TABLE " + icebergTableName); + } + @Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS}) public void testInformationSchemaColumns() { 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 f11e21ca9620..4cbcfe0dd2ed 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 @@ -339,6 +339,23 @@ public void testCommentTable() onTrino().executeQuery("DROP TABLE " + hiveTableName); } + @Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS}) + public void testShowGrants() + { + String tableName = "hive_show_grants_" + randomTableSuffix(); + String hiveTableName = "hive.default." + tableName; + String icebergTableName = "iceberg.default." + tableName; + + createHiveTable(hiveTableName, true); + + // TODO: support redirects from Iceberg to Hive + assertQueryFailure(() -> onTrino().executeQuery("SHOW GRANTS ON " + icebergTableName)) + .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 testInformationSchemaColumns() {