diff --git a/core/trino-main/src/main/java/io/trino/execution/DropMaterializedViewTask.java b/core/trino-main/src/main/java/io/trino/execution/DropMaterializedViewTask.java index 033c85beaeac..41b49a3db267 100644 --- a/core/trino-main/src/main/java/io/trino/execution/DropMaterializedViewTask.java +++ b/core/trino-main/src/main/java/io/trino/execution/DropMaterializedViewTask.java @@ -18,7 +18,6 @@ import io.trino.execution.warnings.WarningCollector; import io.trino.metadata.Metadata; import io.trino.metadata.QualifiedObjectName; -import io.trino.metadata.TableHandle; import io.trino.security.AccessControl; import io.trino.sql.tree.DropMaterializedView; import io.trino.sql.tree.Expression; @@ -26,11 +25,10 @@ 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; -import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; import static io.trino.sql.analyzer.SemanticExceptions.semanticException; import static java.util.Objects.requireNonNull; @@ -64,21 +62,20 @@ public ListenableFuture execute( QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName()); if (!metadata.isMaterializedView(session, name)) { + if (metadata.isView(session, name)) { + throw semanticException( + GENERIC_USER_ERROR, + statement, + "Materialized view '%s' does not exist, but a view with that name exists. Did you mean DROP VIEW %s?", name, name); + } + if (metadata.getTableHandle(session, name).isPresent()) { + throw semanticException( + GENERIC_USER_ERROR, + statement, + "Materialized view '%s' does not exist, but a table with that name exists. Did you mean DROP TABLE %s?", name, name); + } if (!statement.isExists()) { - if (metadata.isView(session, name)) { - throw semanticException( - TABLE_NOT_FOUND, - statement, - "Materialized view '%s' does not exist, but a view with that name exists. Did you mean DROP VIEW %s?", name, name); - } - Optional table = metadata.getTableHandle(session, name); - if (table.isPresent()) { - throw semanticException( - TABLE_NOT_FOUND, - statement, - "Materialized view '%s' does not exist, but a table with that name exists. Did you mean DROP TABLE %s?", name, name); - } - throw semanticException(TABLE_NOT_FOUND, statement, "Materialized view '%s' does not exist", name); + throw semanticException(GENERIC_USER_ERROR, statement, "Materialized view '%s' does not exist", name); } return immediateVoidFuture(); } diff --git a/core/trino-main/src/main/java/io/trino/execution/DropTableTask.java b/core/trino-main/src/main/java/io/trino/execution/DropTableTask.java index cd485ec1b213..a44e4957d92d 100644 --- a/core/trino-main/src/main/java/io/trino/execution/DropTableTask.java +++ b/core/trino-main/src/main/java/io/trino/execution/DropTableTask.java @@ -29,6 +29,7 @@ import static com.google.common.util.concurrent.Futures.immediateVoidFuture; import static io.trino.metadata.MetadataUtil.createQualifiedObjectName; +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; import static io.trino.sql.analyzer.SemanticExceptions.semanticException; import static java.util.Objects.requireNonNull; @@ -62,23 +63,17 @@ public ListenableFuture execute( Session session = stateMachine.getSession(); QualifiedObjectName originalTableName = createQualifiedObjectName(session, statement, statement.getTableName()); if (metadata.isMaterializedView(session, originalTableName)) { - if (!statement.isExists()) { - throw semanticException( - TABLE_NOT_FOUND, - statement, - "Table '%s' does not exist, but a materialized view with that name exists. Did you mean DROP MATERIALIZED VIEW %s?", originalTableName, originalTableName); - } - return immediateVoidFuture(); + throw semanticException( + GENERIC_USER_ERROR, + statement, + "Table '%s' does not exist, but a materialized view with that name exists. Did you mean DROP MATERIALIZED VIEW %s?", originalTableName, originalTableName); } if (metadata.isView(session, originalTableName)) { - if (!statement.isExists()) { - throw semanticException( - TABLE_NOT_FOUND, - statement, - "Table '%s' does not exist, but a view with that name exists. Did you mean DROP VIEW %s?", originalTableName, originalTableName); - } - return immediateVoidFuture(); + throw semanticException( + GENERIC_USER_ERROR, + statement, + "Table '%s' does not exist, but a view with that name exists. Did you mean DROP VIEW %s?", originalTableName, originalTableName); } RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, originalTableName); diff --git a/core/trino-main/src/main/java/io/trino/execution/DropViewTask.java b/core/trino-main/src/main/java/io/trino/execution/DropViewTask.java index 4878d470eb52..520cb7a2a8f3 100644 --- a/core/trino-main/src/main/java/io/trino/execution/DropViewTask.java +++ b/core/trino-main/src/main/java/io/trino/execution/DropViewTask.java @@ -18,7 +18,6 @@ import io.trino.execution.warnings.WarningCollector; import io.trino.metadata.Metadata; import io.trino.metadata.QualifiedObjectName; -import io.trino.metadata.TableHandle; import io.trino.security.AccessControl; import io.trino.sql.tree.DropView; import io.trino.sql.tree.Expression; @@ -26,11 +25,10 @@ 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; -import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; import static io.trino.sql.analyzer.SemanticExceptions.semanticException; import static java.util.Objects.requireNonNull; @@ -64,25 +62,21 @@ public ListenableFuture execute( QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName()); if (metadata.isMaterializedView(session, name)) { - if (!statement.isExists()) { - throw semanticException( - TABLE_NOT_FOUND, - statement, - "View '%s' does not exist, but a materialized view with that name exists. Did you mean DROP MATERIALIZED VIEW %s?", name, name); - } - return immediateVoidFuture(); + throw semanticException( + GENERIC_USER_ERROR, + statement, + "View '%s' does not exist, but a materialized view with that name exists. Did you mean DROP MATERIALIZED VIEW %s?", name, name); } if (!metadata.isView(session, name)) { + if (metadata.getTableHandle(session, name).isPresent()) { + throw semanticException( + GENERIC_USER_ERROR, + statement, + "View '%s' does not exist, but a table with that name exists. Did you mean DROP TABLE %s?", name, name); + } if (!statement.isExists()) { - Optional table = metadata.getTableHandle(session, name); - if (table.isPresent()) { - throw semanticException( - TABLE_NOT_FOUND, - statement, - "View '%s' does not exist, but a table with that name exists. Did you mean DROP TABLE %s?", name, name); - } - throw semanticException(TABLE_NOT_FOUND, statement, "View '%s' does not exist", name); + throw semanticException(GENERIC_USER_ERROR, statement, "View '%s' does not exist", name); } return immediateVoidFuture(); } 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 3fa91dca5c0a..7f36d84cb1ca 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 @@ -34,6 +34,7 @@ import static com.google.common.util.concurrent.Futures.immediateVoidFuture; import static io.trino.metadata.MetadataUtil.createQualifiedObjectName; import static io.trino.spi.StandardErrorCode.CATALOG_NOT_FOUND; +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; import static io.trino.spi.StandardErrorCode.SYNTAX_ERROR; import static io.trino.spi.StandardErrorCode.TABLE_ALREADY_EXISTS; @@ -72,23 +73,17 @@ public ListenableFuture execute( QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getSource()); if (metadata.isMaterializedView(session, tableName)) { - if (!statement.isExists()) { - throw semanticException( - TABLE_NOT_FOUND, - statement, - "Table '%s' does not exist, but a materialized view with that name exists. Did you mean ALTER MATERIALIZED VIEW %s RENAME TO ...?", tableName, tableName); - } - return immediateVoidFuture(); + throw semanticException( + GENERIC_USER_ERROR, + statement, + "Table '%s' does not exist, but a materialized view with that name exists. Did you mean ALTER MATERIALIZED VIEW %s RENAME TO ...?", tableName, tableName); } if (metadata.isView(session, tableName)) { - if (!statement.isExists()) { - throw semanticException( - TABLE_NOT_FOUND, - statement, - "Table '%s' does not exist, but a view with that name exists. Did you mean ALTER VIEW %s RENAME TO ...?", tableName, tableName); - } - return immediateVoidFuture(); + throw semanticException( + GENERIC_USER_ERROR, + statement, + "Table '%s' does not exist, but a view with that name exists. Did you mean ALTER VIEW %s RENAME TO ...?", tableName, tableName); } RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, tableName); diff --git a/core/trino-main/src/test/java/io/trino/execution/TestDropMaterializedViewTask.java b/core/trino-main/src/test/java/io/trino/execution/TestDropMaterializedViewTask.java index 4af5002b33aa..acaaacb74876 100644 --- a/core/trino-main/src/test/java/io/trino/execution/TestDropMaterializedViewTask.java +++ b/core/trino-main/src/test/java/io/trino/execution/TestDropMaterializedViewTask.java @@ -23,7 +23,7 @@ import org.testng.annotations.Test; import static io.airlift.concurrent.MoreFutures.getFutureValue; -import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; import static io.trino.testing.assertions.TrinoExceptionAssert.assertTrinoExceptionThrownBy; import static org.assertj.core.api.Assertions.assertThat; @@ -48,7 +48,7 @@ public void testDropNotExistingMaterializedView() QualifiedName viewName = qualifiedName("not_existing_materialized_view"); assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropMaterializedView(viewName, false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("Materialized view '%s' does not exist", viewName); } @@ -67,8 +67,8 @@ public void testDropMaterializedViewOnTable() QualifiedObjectName tableName = qualifiedObjectName("existing_table"); metadata.createTable(testSession, CATALOG_NAME, someTable(tableName), false); - assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropMaterializedView(asQualifiedName(tableName), false))) - .hasErrorCode(TABLE_NOT_FOUND) + assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropMaterializedView(asQualifiedName(tableName), true))) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("Materialized view '%s' does not exist, but a table with that name exists. Did you mean DROP TABLE %s?", tableName, tableName); } @@ -78,8 +78,9 @@ public void testDropMaterializedViewOnTableIfExists() QualifiedObjectName tableName = qualifiedObjectName("existing_table"); metadata.createTable(testSession, CATALOG_NAME, someTable(tableName), false); - getFutureValue(executeDropMaterializedView(asQualifiedName(tableName), true)); - // no exception + assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropMaterializedView(asQualifiedName(tableName), true))) + .hasErrorCode(GENERIC_USER_ERROR) + .hasMessage("Materialized view '%s' does not exist, but a table with that name exists. Did you mean DROP TABLE %s?", tableName, tableName); } @Test @@ -89,7 +90,7 @@ public void testDropMaterializedViewOnView() metadata.createView(testSession, asQualifiedObjectName(viewName), someView(), false); assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropMaterializedView(viewName, false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("Materialized view '%s' does not exist, but a view with that name exists. Did you mean DROP VIEW %s?", viewName, viewName); } @@ -99,8 +100,9 @@ public void testDropMaterializedViewOnViewIfExists() QualifiedName viewName = qualifiedName("existing_view"); metadata.createView(testSession, asQualifiedObjectName(viewName), someView(), false); - getFutureValue(executeDropMaterializedView(viewName, true)); - // no exception + assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropMaterializedView(viewName, false))) + .hasErrorCode(GENERIC_USER_ERROR) + .hasMessage("Materialized view '%s' does not exist, but a view with that name exists. Did you mean DROP VIEW %s?", viewName, viewName); } private ListenableFuture executeDropMaterializedView(QualifiedName viewName, boolean exists) diff --git a/core/trino-main/src/test/java/io/trino/execution/TestDropTableTask.java b/core/trino-main/src/test/java/io/trino/execution/TestDropTableTask.java index 70597df3f409..1b499599c778 100644 --- a/core/trino-main/src/test/java/io/trino/execution/TestDropTableTask.java +++ b/core/trino-main/src/test/java/io/trino/execution/TestDropTableTask.java @@ -23,6 +23,7 @@ import org.testng.annotations.Test; import static io.airlift.concurrent.MoreFutures.getFutureValue; +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; import static io.trino.testing.assertions.TrinoExceptionAssert.assertTrinoExceptionThrownBy; import static org.assertj.core.api.Assertions.assertThat; @@ -68,7 +69,7 @@ public void testDropTableOnView() metadata.createView(testSession, asQualifiedObjectName(viewName), someView(), false); assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropTable(viewName, false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("Table '%s' does not exist, but a view with that name exists. Did you mean DROP VIEW %s?", viewName, viewName); } @@ -78,8 +79,9 @@ public void testDropTableOnViewIfExists() QualifiedName viewName = qualifiedName("existing_view"); metadata.createView(testSession, asQualifiedObjectName(viewName), someView(), false); - getFutureValue(executeDropTable(viewName, true)); - // no exception + assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropTable(viewName, true))) + .hasErrorCode(GENERIC_USER_ERROR) + .hasMessage("Table '%s' does not exist, but a view with that name exists. Did you mean DROP VIEW %s?", viewName, viewName); } @Test @@ -89,7 +91,7 @@ public void testDropTableOnMaterializedView() metadata.createMaterializedView(testSession, asQualifiedObjectName(viewName), someMaterializedView(), false, false); assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropTable(viewName, false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("Table '%s' does not exist, but a materialized view with that name exists. Did you mean DROP MATERIALIZED VIEW %s?", viewName, viewName); } @@ -99,8 +101,9 @@ public void testDropTableOnMaterializedViewIfExists() QualifiedName viewName = qualifiedName("existing_materialized_view"); metadata.createMaterializedView(testSession, asQualifiedObjectName(viewName), someMaterializedView(), false, false); - getFutureValue(executeDropTable(viewName, true)); - // no exception + assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropTable(viewName, true))) + .hasErrorCode(GENERIC_USER_ERROR) + .hasMessage("Table '%s' does not exist, but a materialized view with that name exists. Did you mean DROP MATERIALIZED VIEW %s?", viewName, viewName); } private ListenableFuture executeDropTable(QualifiedName tableName, boolean exists) diff --git a/core/trino-main/src/test/java/io/trino/execution/TestDropViewTask.java b/core/trino-main/src/test/java/io/trino/execution/TestDropViewTask.java index 630f90c65def..bdbddda26982 100644 --- a/core/trino-main/src/test/java/io/trino/execution/TestDropViewTask.java +++ b/core/trino-main/src/test/java/io/trino/execution/TestDropViewTask.java @@ -23,7 +23,7 @@ import org.testng.annotations.Test; import static io.airlift.concurrent.MoreFutures.getFutureValue; -import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; import static io.trino.testing.assertions.TrinoExceptionAssert.assertTrinoExceptionThrownBy; import static org.assertj.core.api.Assertions.assertThat; @@ -48,7 +48,7 @@ public void testDropNotExistingView() QualifiedName viewName = qualifiedName("not_existing_view"); assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropView(viewName, false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("View '%s' does not exist", viewName); } @@ -68,7 +68,7 @@ public void testDropViewOnTable() metadata.createTable(testSession, CATALOG_NAME, someTable(tableName), false); assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropView(asQualifiedName(tableName), false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("View '%s' does not exist, but a table with that name exists. Did you mean DROP TABLE %s?", tableName, tableName); } @@ -78,8 +78,9 @@ public void testDropViewOnTableIfExists() QualifiedObjectName tableName = qualifiedObjectName("existing_table"); metadata.createTable(testSession, CATALOG_NAME, someTable(tableName), false); - getFutureValue(executeDropView(asQualifiedName(tableName), true)); - // no exception + assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropView(asQualifiedName(tableName), true))) + .hasErrorCode(GENERIC_USER_ERROR) + .hasMessage("View '%s' does not exist, but a table with that name exists. Did you mean DROP TABLE %s?", tableName, tableName); } @Test @@ -89,7 +90,7 @@ public void testDropViewOnMaterializedView() metadata.createMaterializedView(testSession, QualifiedObjectName.valueOf(viewName.toString()), someMaterializedView(), false, false); assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropView(viewName, false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("View '%s' does not exist, but a materialized view with that name exists. Did you mean DROP MATERIALIZED VIEW %s?", viewName, viewName); } @@ -99,8 +100,9 @@ public void testDropViewOnMaterializedViewIfExists() QualifiedName viewName = qualifiedName("existing_materialized_view"); metadata.createMaterializedView(testSession, QualifiedObjectName.valueOf(viewName.toString()), someMaterializedView(), false, false); - getFutureValue(executeDropView(viewName, true)); - // no exception + assertTrinoExceptionThrownBy(() -> getFutureValue(executeDropView(viewName, true))) + .hasErrorCode(GENERIC_USER_ERROR) + .hasMessage("View '%s' does not exist, but a materialized view with that name exists. Did you mean DROP MATERIALIZED VIEW %s?", viewName, viewName); } private ListenableFuture executeDropView(QualifiedName viewName, boolean exists) diff --git a/core/trino-main/src/test/java/io/trino/execution/TestRenameTableTask.java b/core/trino-main/src/test/java/io/trino/execution/TestRenameTableTask.java index 90f7dd69b357..4233b467591d 100644 --- a/core/trino-main/src/test/java/io/trino/execution/TestRenameTableTask.java +++ b/core/trino-main/src/test/java/io/trino/execution/TestRenameTableTask.java @@ -23,6 +23,7 @@ import org.testng.annotations.Test; import static io.airlift.concurrent.MoreFutures.getFutureValue; +import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR; import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; import static io.trino.testing.assertions.TrinoExceptionAssert.assertTrinoExceptionThrownBy; import static org.assertj.core.api.Assertions.assertThat; @@ -69,7 +70,7 @@ public void testRenameTableOnView() metadata.createView(testSession, viewName, someView(), false); assertTrinoExceptionThrownBy(() -> getFutureValue(executeRenameTable(asQualifiedName(viewName), qualifiedName("existing_view_new"), false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("Table '%s' does not exist, but a view with that name exists. Did you mean ALTER VIEW %s RENAME TO ...?", viewName, viewName); } @@ -79,8 +80,9 @@ public void testRenameTableOnViewIfExists() QualifiedObjectName viewName = qualifiedObjectName("existing_view"); metadata.createView(testSession, viewName, someView(), false); - getFutureValue(executeRenameTable(asQualifiedName(viewName), qualifiedName("existing_view_new"), true)); - // no exception + assertTrinoExceptionThrownBy(() -> getFutureValue(executeRenameTable(asQualifiedName(viewName), qualifiedName("existing_view_new"), true))) + .hasErrorCode(GENERIC_USER_ERROR) + .hasMessage("Table '%s' does not exist, but a view with that name exists. Did you mean ALTER VIEW %s RENAME TO ...?", viewName, viewName); } @Test @@ -90,7 +92,7 @@ public void testRenameTableOnMaterializedView() metadata.createMaterializedView(testSession, QualifiedObjectName.valueOf(viewName.toString()), someMaterializedView(), false, false); assertTrinoExceptionThrownBy(() -> getFutureValue(executeRenameTable(viewName, qualifiedName("existing_materialized_view_new"), false))) - .hasErrorCode(TABLE_NOT_FOUND) + .hasErrorCode(GENERIC_USER_ERROR) .hasMessage("Table '%s' does not exist, but a materialized view with that name exists. Did you mean ALTER MATERIALIZED VIEW catalog.schema.existing_materialized_view RENAME TO ...?", viewName); } @@ -100,8 +102,9 @@ public void testRenameTableOnMaterializedViewIfExists() QualifiedName viewName = qualifiedName("existing_materialized_view"); metadata.createMaterializedView(testSession, QualifiedObjectName.valueOf(viewName.toString()), someMaterializedView(), false, false); - getFutureValue(executeRenameTable(viewName, qualifiedName("existing_materialized_view_new"), true)); - // no exception + assertTrinoExceptionThrownBy(() -> getFutureValue(executeRenameTable(viewName, qualifiedName("existing_materialized_view_new"), true))) + .hasErrorCode(GENERIC_USER_ERROR) + .hasMessage("Table '%s' does not exist, but a materialized view with that name exists. Did you mean ALTER MATERIALIZED VIEW catalog.schema.existing_materialized_view RENAME TO ...?", viewName); } private ListenableFuture executeRenameTable(QualifiedName source, QualifiedName target, boolean exists)