diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index 12f266d48b0446..a6ea80c42a5be8 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -203,7 +203,9 @@ supportedCreateStatement supportedAlterStatement : ALTER VIEW name=multipartIdentifier (LEFT_PAREN cols=simpleColumnDefs RIGHT_PAREN)? - AS query #alterView + AS query #alterView + | ALTER CATALOG name=identifier RENAME newName=identifier #alterCatalogRename + | ALTER ROLE role=identifier commentSpec #alterRole | ALTER STORAGE VAULT name=multipartIdentifier properties=propertyClause #alterStorageVault | ALTER ROLE role=identifier commentSpec #alterRole | ALTER WORKLOAD GROUP name=identifierOrText @@ -593,7 +595,6 @@ unsupportedAlterStatement | ALTER DATABASE name=identifier RENAME newName=identifier #alterDatabaseRename | ALTER DATABASE name=identifier SET PROPERTIES LEFT_PAREN propertyItemList RIGHT_PAREN #alterDatabaseProperties - | ALTER CATALOG name=identifier RENAME newName=identifier #alterCatalogRename | ALTER CATALOG name=identifier SET PROPERTIES LEFT_PAREN propertyItemList RIGHT_PAREN #alterCatalogProperties | ALTER RESOURCE name=identifierOrText properties=propertyClause? #alterResource diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java index 96bce5a48f04e3..d73d2b85b26280 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java @@ -311,25 +311,27 @@ public void dropCatalog(DropCatalogStmt stmt) throws UserException { /** * Modify the catalog name into a new one and write the meta log. */ - public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException { + public void alterCatalogName(String catalogName, String newCatalogName) throws UserException { writeLock(); try { - CatalogIf catalog = nameToCatalog.get(stmt.getCatalogName()); + CatalogIf catalog = nameToCatalog.get(catalogName); if (catalog == null) { - throw new DdlException("No catalog found with name: " + stmt.getCatalogName()); + throw new DdlException("No catalog found with name: " + catalogName); } - if (nameToCatalog.get(stmt.getNewCatalogName()) != null) { - throw new DdlException("Catalog with name " + stmt.getNewCatalogName() + " already exist"); + if (nameToCatalog.get(newCatalogName) != null) { + throw new DdlException("Catalog with name " + newCatalogName + " already exist"); } - CatalogLog log = CatalogFactory.createCatalogLog(catalog.getId(), stmt); + CatalogLog log = new CatalogLog(); + log.setCatalogId(catalog.getId()); + log.setNewCatalogName(newCatalogName); replayAlterCatalogName(log); Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_NAME, log); ConnectContext ctx = ConnectContext.get(); if (ctx != null) { - String db = ctx.getLastDBOfCatalog(stmt.getCatalogName()); + String db = ctx.getLastDBOfCatalog(catalogName); if (db != null) { - ctx.removeLastDBOfCatalog(stmt.getCatalogName()); + ctx.removeLastDBOfCatalog(catalogName); ctx.addLastDBOfCatalog(log.getNewCatalogName(), db); } } @@ -338,6 +340,16 @@ public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException { } } + /** + * Modify the catalog name into a new one and write the meta log. + */ + public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException { + alterCatalogName(stmt.getCatalogName(), stmt.getNewCatalogName()); + } + + /** + * Modify the catalog comment to a new one and write the meta log. + */ public void alterCatalogComment(String catalogName, String comment) throws UserException { writeLock(); try { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index b8fea4049caabf..27e141379f38c8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -70,6 +70,7 @@ import org.apache.doris.nereids.DorisParser.AliasQueryContext; import org.apache.doris.nereids.DorisParser.AliasedQueryContext; import org.apache.doris.nereids.DorisParser.AlterCatalogCommentContext; +import org.apache.doris.nereids.DorisParser.AlterCatalogRenameContext; import org.apache.doris.nereids.DorisParser.AlterMTMVContext; import org.apache.doris.nereids.DorisParser.AlterMultiPartitionClauseContext; import org.apache.doris.nereids.DorisParser.AlterRoleContext; @@ -483,6 +484,7 @@ import org.apache.doris.nereids.trees.plans.commands.AdminSetTableStatusCommand; import org.apache.doris.nereids.trees.plans.commands.AdminShowReplicaStatusCommand; import org.apache.doris.nereids.trees.plans.commands.AlterCatalogCommentCommand; +import org.apache.doris.nereids.trees.plans.commands.AlterCatalogRenameCommand; import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand; import org.apache.doris.nereids.trees.plans.commands.AlterSqlBlockRuleCommand; @@ -5120,6 +5122,13 @@ public LogicalPlan visitCreateEncryptkey(CreateEncryptkeyContext ctx) { stripQuotes(ctx.STRING_LITERAL().getText())); } + @Override + public LogicalPlan visitAlterCatalogRename(AlterCatalogRenameContext ctx) { + String catalogName = stripQuotes(ctx.name.getText()); + String newName = stripQuotes(ctx.newName.getText()); + return new AlterCatalogRenameCommand(catalogName, newName); + } + @Override public LogicalPlan visitDropStoragePolicy(DropStoragePolicyContext ctx) { String policyName = ctx.name.getText(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java index c71a2c8b442fcf..9c7c82a337c4b7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java @@ -178,6 +178,7 @@ public enum PlanType { CLEAN_ALL_PROFILE_COMMAND, CLEAN_TRASH_COMMAND, CREATE_ROLE_COMMAND, + ALTER_CATALOG_RENAME_COMMAND, ALTER_ROLE_COMMAND, ALTER_VIEW_COMMAND, ALTER_STORAGE_VAULT, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogRenameCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogRenameCommand.java new file mode 100644 index 00000000000000..76604bd43afde5 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogRenameCommand.java @@ -0,0 +1,48 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.trees.plans.commands; + +import org.apache.doris.catalog.Env; +import org.apache.doris.nereids.trees.plans.PlanType; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.StmtExecutor; + +/** + * Represents the command for ALTER CATALOG RENAME. + */ +public class AlterCatalogRenameCommand extends AlterCatalogCommand { + private final String newCatalogName; + + public AlterCatalogRenameCommand(String catalogName, String newName) { + super(PlanType.ALTER_CATALOG_RENAME_COMMAND, catalogName); + this.newCatalogName = newName; + } + + @Override + public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { + validate(ctx); + Env.getCurrentEnv().getCatalogMgr().alterCatalogName(catalogName, newCatalogName); + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitAlterCatalogRenameCommand(this, context); + } +} + diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java index 85d5d320f50afe..c0be169ef30fe7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java @@ -26,6 +26,7 @@ import org.apache.doris.nereids.trees.plans.commands.AdminSetTableStatusCommand; import org.apache.doris.nereids.trees.plans.commands.AdminShowReplicaStatusCommand; import org.apache.doris.nereids.trees.plans.commands.AlterCatalogCommentCommand; +import org.apache.doris.nereids.trees.plans.commands.AlterCatalogRenameCommand; import org.apache.doris.nereids.trees.plans.commands.AlterJobStatusCommand; import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand; @@ -410,6 +411,10 @@ default R visitSetUserPropertiesCommand(SetUserPropertiesCommand setUserProperti return visitCommand(setUserPropertiesCommand, context); } + default R visitAlterCatalogRenameCommand(AlterCatalogRenameCommand alterCatalogRenameCommand, C context) { + return visitCommand(alterCatalogRenameCommand, context); + } + default R visitSetDefaultStorageVault(SetDefaultStorageVaultCommand setDefaultStorageVaultCommand, C context) { return visitCommand(setDefaultStorageVaultCommand, context); } diff --git a/regression-test/data/nereids_p0/test_alter_catalog_rename_command.out b/regression-test/data/nereids_p0/test_alter_catalog_rename_command.out new file mode 100644 index 00000000000000..b262a0a1b26419 --- /dev/null +++ b/regression-test/data/nereids_p0/test_alter_catalog_rename_command.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !cmd -- +test_alter_catalog_rename \nCREATE CATALOG `test_alter_catalog_rename`\nCOMMENT "Catalog for renaming test"\n PROPERTIES (\n"type" = "es",\n"hosts" = "http://127.0.0.1:9200"\n); + +-- !cmd -- +test_altered_catalog \nCREATE CATALOG `test_altered_catalog`\nCOMMENT "Catalog for renaming test"\n PROPERTIES (\n"type" = "es",\n"hosts" = "http://127.0.0.1:9200"\n); + diff --git a/regression-test/suites/nereids_p0/test_alter_catalog_rename_command.groovy b/regression-test/suites/nereids_p0/test_alter_catalog_rename_command.groovy new file mode 100644 index 00000000000000..2939eb94f9785c --- /dev/null +++ b/regression-test/suites/nereids_p0/test_alter_catalog_rename_command.groovy @@ -0,0 +1,55 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_alter_catalog_rename_command", "nereids_p0") { + def originalCatalogName = "test_alter_catalog_rename" + def renamedCatalogName = "test_altered_catalog" + def catalogProperties = "\"type\"=\"es\", \"hosts\"=\"http://127.0.0.1:9200\"" + + try { + // Drop catalogs if they already exist + sql("DROP CATALOG IF EXISTS ${originalCatalogName}") + sql("DROP CATALOG IF EXISTS ${renamedCatalogName}") + + // Create the original catalog + sql( + """ + CREATE CATALOG ${originalCatalogName} + COMMENT 'Catalog for renaming test' + PROPERTIES (${catalogProperties}) + """ + ) + // Verify the catalog was created + checkNereidsExecute("""SHOW CREATE CATALOG ${originalCatalogName}""") + qt_cmd("""SHOW CREATE CATALOG ${originalCatalogName}""") + + // Rename the catalog + checkNereidsExecute( + """ + ALTER CATALOG ${originalCatalogName} RENAME ${renamedCatalogName} + """ + ) + // Verify the catalog was renamed + checkNereidsExecute("""SHOW CREATE CATALOG ${renamedCatalogName}""") + qt_cmd("""SHOW CREATE CATALOG ${renamedCatalogName}""") + + } finally { + // Clean up + sql("DROP CATALOG IF EXISTS ${originalCatalogName}") + sql("DROP CATALOG IF EXISTS ${renamedCatalogName}") + } +}