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 64ff9209bfe6f2e..65c816f7e6cff98 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 @@ -204,6 +204,7 @@ supportedAlterStatement : ALTER VIEW name=multipartIdentifier (LEFT_PAREN cols=simpleColumnDefs RIGHT_PAREN)? AS query #alterView | ALTER STORAGE VAULT name=multipartIdentifier properties=propertyClause #alterStorageVault + | ALTER CATALOG name=identifier RENAME newName=identifier #alterCatalogRename | ALTER ROLE role=identifier commentSpec #alterRole | ALTER WORKLOAD GROUP name=identifierOrText properties=propertyClause? #alterWorkloadGroup @@ -584,7 +585,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 0203aa7020b090e..37a0504291948a5 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 @@ -299,28 +299,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); } } @@ -329,6 +328,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 951c8b342d5d9e2..e256d90a76f3600 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 @@ -63,6 +63,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.AlterRoleContext; import org.apache.doris.nereids.DorisParser.AlterSqlBlockRuleContext; @@ -491,6 +492,7 @@ import org.apache.doris.nereids.trees.plans.commands.AdminRebalanceDiskCommand; 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; @@ -4950,6 +4952,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 visitDropEncryptkey(DropEncryptkeyContext ctx) { List nameParts = visitMultipartIdentifier(ctx.name); 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 9ed408dfe05b450..adc3f0c13284b79 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 @@ -176,6 +176,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 000000000000000..89e6148ac9187e8 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogRenameCommand.java @@ -0,0 +1,50 @@ +// 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 catalogName; + private final String newCatalogName; + + public AlterCatalogRenameCommand(String catalogName, String newName) { + super(PlanType.ALTER_CATALOG_RENAME_COMMAND, catalogName); + this.catalogName = 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 eb764be57c0097f..29df1048270b124 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 @@ -25,6 +25,7 @@ import org.apache.doris.nereids.trees.plans.commands.AdminRebalanceDiskCommand; 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; @@ -379,6 +380,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 000000000000000..b262a0a1b264199 --- /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 000000000000000..fc835c829ee4fa3 --- /dev/null +++ b/regression-test/suites/nereids_p0/test_alter_catalog_rename_command.groovy @@ -0,0 +1,56 @@ +// 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, +// 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}") + } +}