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 274a354b63bc03..044ed7771a4aa1 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 @@ -206,6 +206,7 @@ supportedAlterStatement | ALTER WORKLOAD POLICY name=identifierOrText properties=propertyClause? #alterWorkloadPolicy | ALTER SQL_BLOCK_RULE name=identifier properties=propertyClause? #alterSqlBlockRule + | ALTER CATALOG name=identifier MODIFY COMMENT comment=STRING_LITERAL #alterCatalogComment ; supportedDropStatement @@ -582,7 +583,6 @@ unsupportedAlterStatement | ALTER CATALOG name=identifier RENAME newName=identifier #alterCatalogRename | ALTER CATALOG name=identifier SET PROPERTIES LEFT_PAREN propertyItemList RIGHT_PAREN #alterCatalogProperties - | ALTER CATALOG name=identifier MODIFY COMMENT comment=STRING_LITERAL #alterCatalogComment | ALTER RESOURCE name=identifierOrText properties=propertyClause? #alterResource | ALTER COLOCATE GROUP name=multipartIdentifier SET LEFT_PAREN propertyItemList RIGHT_PAREN #alterColocateGroup 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 1d1a44be7b4834..070d31c071e930 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 @@ -314,17 +314,16 @@ public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException { } } - /** - * Modify the catalog comment to a new one and write the meta log. - */ - public void alterCatalogComment(AlterCatalogCommentStmt stmt) throws UserException { + public void alterCatalogComment(String catalogName, String comment) 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); } - CatalogLog log = CatalogFactory.createCatalogLog(catalog.getId(), stmt); + CatalogLog log = new CatalogLog(); + log.setCatalogId(catalog.getId()); + log.setComment(comment); replayAlterCatalogComment(log); Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_COMMENT, log); } finally { @@ -332,6 +331,13 @@ public void alterCatalogComment(AlterCatalogCommentStmt stmt) throws UserExcepti } } + /** + * Modify the catalog comment to a new one and write the meta log. + */ + public void alterCatalogComment(AlterCatalogCommentStmt stmt) throws UserException { + alterCatalogComment(stmt.getCatalogName(), stmt.getComment()); + } + /** * Modify the catalog property and write the meta log. */ 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 56de50d620eb6d..e6fee894b41fa9 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 @@ -60,6 +60,7 @@ import org.apache.doris.nereids.DorisParser.AggStateDataTypeContext; 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.AlterMTMVContext; import org.apache.doris.nereids.DorisParser.AlterRoleContext; import org.apache.doris.nereids.DorisParser.AlterSqlBlockRuleContext; @@ -482,6 +483,7 @@ import org.apache.doris.nereids.trees.plans.commands.AdminCleanTrashCommand; import org.apache.doris.nereids.trees.plans.commands.AdminCompactTableCommand; 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.AlterMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand; import org.apache.doris.nereids.trees.plans.commands.AlterSqlBlockRuleCommand; @@ -4824,6 +4826,13 @@ public LogicalPlan visitShowWhitelist(ShowWhitelistContext ctx) { return new ShowWhiteListCommand(); } + @Override + public LogicalPlan visitAlterCatalogComment(AlterCatalogCommentContext ctx) { + String catalogName = stripQuotes(ctx.name.getText()); + String comment = stripQuotes(ctx.comment.getText()); + return new AlterCatalogCommentCommand(catalogName, comment); + } + @Override public LogicalPlan visitShowDynamicPartition(ShowDynamicPartitionContext ctx) { String dbName = null; 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 204d4b07111bec..e8ce4b3e2d88db 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 @@ -200,6 +200,7 @@ public enum PlanType { DROP_USER_COMMAND, DROP_WORKLOAD_GROUP_NAME, DROP_WORKLOAD_POLICY_COMMAND, + ALTER_CATALOG_COMMENT_COMMAND, ALTER_SQL_BLOCK_RULE_COMMAND, SHOW_BACKENDS_COMMAND, SHOW_BLOCK_RULE_COMMAND, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogCommand.java new file mode 100644 index 00000000000000..83ef02e8890375 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogCommand.java @@ -0,0 +1,58 @@ +// 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.common.ErrorCode; +import org.apache.doris.common.ErrorReport; +import org.apache.doris.common.util.Util; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.nereids.trees.plans.PlanType; +import org.apache.doris.qe.ConnectContext; + +import java.util.Objects; + +/** + * Base class for AlterCatalog commands. + */ +public abstract class AlterCatalogCommand extends AlterCommand { + protected final String catalogName; + + public AlterCatalogCommand(PlanType type, String catalogName) { + super(type); + this.catalogName = Objects.requireNonNull(catalogName, "Catalog name cannot be null"); + } + + protected void validate(ConnectContext ctx) throws Exception { + Util.checkCatalogAllRules(catalogName); + + if (!Env.getCurrentEnv().getAccessManager().checkCtlPriv(ctx, catalogName, PrivPredicate.ALTER)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_CATALOG_ACCESS_DENIED, + ctx.getQualifiedUser(), catalogName); + } + + if (catalogName.equals(InternalCatalog.INTERNAL_CATALOG_NAME)) { + throw new Exception("Internal catalog cannot be altered."); + } + } + + public String getCatalogName() { + return catalogName; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogCommentCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogCommentCommand.java new file mode 100644 index 00000000000000..eafe1f7c2c1c4d --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterCatalogCommentCommand.java @@ -0,0 +1,58 @@ +// 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.common.AnalysisException; +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; + +import com.google.common.base.Strings; + +import java.util.Objects; + +/** + * Represents the command for ALTER CATALOG MODIFY COMMENT. + */ +public class AlterCatalogCommentCommand extends AlterCatalogCommand { + + private final String comment; + + public AlterCatalogCommentCommand(String catalogName, String comment) { + super(PlanType.ALTER_CATALOG_COMMENT_COMMAND, catalogName); + this.comment = Objects.requireNonNull(comment, "Comment cannot be null"); + } + + @Override + public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { + // Validate the catalog name + if (Strings.isNullOrEmpty(comment)) { + throw new AnalysisException("New comment is not set."); + } + + // Fetch and modify the catalog's comment + Env.getCurrentEnv().getCatalogMgr().alterCatalogComment(catalogName, comment); + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitAlterCatalogCommentCommand(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 e2ac1c863e2f5a..0341ed2f57d7ec 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 @@ -22,6 +22,7 @@ import org.apache.doris.nereids.trees.plans.commands.AdminCleanTrashCommand; import org.apache.doris.nereids.trees.plans.commands.AdminCompactTableCommand; 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.AlterJobStatusCommand; import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand; @@ -324,6 +325,10 @@ default R visitAlterViewCommand(AlterViewCommand alterViewCommand, C context) { return visitCommand(alterViewCommand, context); } + default R visitAlterCatalogCommentCommand(AlterCatalogCommentCommand alterCatalogCommentCommand, C context) { + return visitCommand(alterCatalogCommentCommand, context); + } + default R visitDropCatalogRecycleBinCommand(DropCatalogRecycleBinCommand dropCatalogRecycleBinCommand, C context) { return visitCommand(dropCatalogRecycleBinCommand, context); } diff --git a/regression-test/data/nereids_p0/test_alter_catalog_comment_command.out b/regression-test/data/nereids_p0/test_alter_catalog_comment_command.out new file mode 100644 index 00000000000000..646f4cb278174b --- /dev/null +++ b/regression-test/data/nereids_p0/test_alter_catalog_comment_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_comment \nCREATE CATALOG `test_alter_catalog_comment`\nCOMMENT "Initial catalog comment"\n PROPERTIES (\n"type" = "es",\n"hosts" = "http://127.0.0.1:9200"\n); + +-- !cmd -- +test_alter_catalog_comment \nCREATE CATALOG `test_alter_catalog_comment`\nCOMMENT "Updated catalog comment"\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_comment_command.groovy b/regression-test/suites/nereids_p0/test_alter_catalog_comment_command.groovy new file mode 100644 index 00000000000000..389675aff9af23 --- /dev/null +++ b/regression-test/suites/nereids_p0/test_alter_catalog_comment_command.groovy @@ -0,0 +1,54 @@ +// 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_comment_command", "nereids_p0") { + def catalogName = "test_alter_catalog_comment" + def catalogProperties = "\"type\"=\"es\", \"hosts\"=\"http://127.0.0.1:9200\"" + + try { + // Drop catalog if it already exists + sql("DROP CATALOG IF EXISTS ${catalogName}") + + // Create a new catalog + sql( + """ + CREATE CATALOG ${catalogName} + COMMENT 'Initial catalog comment' + PROPERTIES (${catalogProperties}) + """ + ) + // Verify the catalog was created + checkNereidsExecute("""show create catalog ${catalogName}""") + qt_cmd("""show create catalog ${catalogName}""") + + + // Alter the catalog comment + checkNereidsExecute( + """ + ALTER CATALOG ${catalogName} MODIFY COMMENT 'Updated catalog comment' + """ + ) + // Verify the comment was changed + checkNereidsExecute("""show create catalog ${catalogName}""") + qt_cmd("""show create catalog ${catalogName}""") + + } finally { + // Clean up + sql("DROP CATALOG IF EXISTS ${catalogName}") + } +}