Skip to content

Commit b0ab7f1

Browse files
authored
[Enhancement] (nereids)implement alterCatalogCommentCommand in nereids (#45160)
Issue Number: close #42790
1 parent 830b74c commit b0ab7f1

9 files changed

Lines changed: 206 additions & 8 deletions

File tree

‎fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ supportedAlterStatement
206206
| ALTER WORKLOAD POLICY name=identifierOrText
207207
properties=propertyClause? #alterWorkloadPolicy
208208
| ALTER SQL_BLOCK_RULE name=identifier properties=propertyClause? #alterSqlBlockRule
209+
| ALTER CATALOG name=identifier MODIFY COMMENT comment=STRING_LITERAL #alterCatalogComment
209210
;
210211

211212
supportedDropStatement
@@ -582,7 +583,6 @@ unsupportedAlterStatement
582583
| ALTER CATALOG name=identifier RENAME newName=identifier #alterCatalogRename
583584
| ALTER CATALOG name=identifier SET PROPERTIES
584585
LEFT_PAREN propertyItemList RIGHT_PAREN #alterCatalogProperties
585-
| ALTER CATALOG name=identifier MODIFY COMMENT comment=STRING_LITERAL #alterCatalogComment
586586
| ALTER RESOURCE name=identifierOrText properties=propertyClause? #alterResource
587587
| ALTER COLOCATE GROUP name=multipartIdentifier
588588
SET LEFT_PAREN propertyItemList RIGHT_PAREN #alterColocateGroup

‎fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,24 +314,30 @@ public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException {
314314
}
315315
}
316316

317-
/**
318-
* Modify the catalog comment to a new one and write the meta log.
319-
*/
320-
public void alterCatalogComment(AlterCatalogCommentStmt stmt) throws UserException {
317+
public void alterCatalogComment(String catalogName, String comment) throws UserException {
321318
writeLock();
322319
try {
323-
CatalogIf catalog = nameToCatalog.get(stmt.getCatalogName());
320+
CatalogIf catalog = nameToCatalog.get(catalogName);
324321
if (catalog == null) {
325-
throw new DdlException("No catalog found with name: " + stmt.getCatalogName());
322+
throw new DdlException("No catalog found with name: " + catalogName);
326323
}
327-
CatalogLog log = CatalogFactory.createCatalogLog(catalog.getId(), stmt);
324+
CatalogLog log = new CatalogLog();
325+
log.setCatalogId(catalog.getId());
326+
log.setComment(comment);
328327
replayAlterCatalogComment(log);
329328
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_COMMENT, log);
330329
} finally {
331330
writeUnlock();
332331
}
333332
}
334333

334+
/**
335+
* Modify the catalog comment to a new one and write the meta log.
336+
*/
337+
public void alterCatalogComment(AlterCatalogCommentStmt stmt) throws UserException {
338+
alterCatalogComment(stmt.getCatalogName(), stmt.getComment());
339+
}
340+
335341
/**
336342
* Modify the catalog property and write the meta log.
337343
*/

‎fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.apache.doris.nereids.DorisParser.AggStateDataTypeContext;
6161
import org.apache.doris.nereids.DorisParser.AliasQueryContext;
6262
import org.apache.doris.nereids.DorisParser.AliasedQueryContext;
63+
import org.apache.doris.nereids.DorisParser.AlterCatalogCommentContext;
6364
import org.apache.doris.nereids.DorisParser.AlterMTMVContext;
6465
import org.apache.doris.nereids.DorisParser.AlterRoleContext;
6566
import org.apache.doris.nereids.DorisParser.AlterSqlBlockRuleContext;
@@ -482,6 +483,7 @@
482483
import org.apache.doris.nereids.trees.plans.commands.AdminCleanTrashCommand;
483484
import org.apache.doris.nereids.trees.plans.commands.AdminCompactTableCommand;
484485
import org.apache.doris.nereids.trees.plans.commands.AdminShowReplicaStatusCommand;
486+
import org.apache.doris.nereids.trees.plans.commands.AlterCatalogCommentCommand;
485487
import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand;
486488
import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand;
487489
import org.apache.doris.nereids.trees.plans.commands.AlterSqlBlockRuleCommand;
@@ -4824,6 +4826,13 @@ public LogicalPlan visitShowWhitelist(ShowWhitelistContext ctx) {
48244826
return new ShowWhiteListCommand();
48254827
}
48264828

4829+
@Override
4830+
public LogicalPlan visitAlterCatalogComment(AlterCatalogCommentContext ctx) {
4831+
String catalogName = stripQuotes(ctx.name.getText());
4832+
String comment = stripQuotes(ctx.comment.getText());
4833+
return new AlterCatalogCommentCommand(catalogName, comment);
4834+
}
4835+
48274836
@Override
48284837
public LogicalPlan visitShowDynamicPartition(ShowDynamicPartitionContext ctx) {
48294838
String dbName = null;

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ public enum PlanType {
200200
DROP_USER_COMMAND,
201201
DROP_WORKLOAD_GROUP_NAME,
202202
DROP_WORKLOAD_POLICY_COMMAND,
203+
ALTER_CATALOG_COMMENT_COMMAND,
203204
ALTER_SQL_BLOCK_RULE_COMMAND,
204205
SHOW_BACKENDS_COMMAND,
205206
SHOW_BLOCK_RULE_COMMAND,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.commands;
19+
20+
import org.apache.doris.catalog.Env;
21+
import org.apache.doris.common.ErrorCode;
22+
import org.apache.doris.common.ErrorReport;
23+
import org.apache.doris.common.util.Util;
24+
import org.apache.doris.datasource.InternalCatalog;
25+
import org.apache.doris.mysql.privilege.PrivPredicate;
26+
import org.apache.doris.nereids.trees.plans.PlanType;
27+
import org.apache.doris.qe.ConnectContext;
28+
29+
import java.util.Objects;
30+
31+
/**
32+
* Base class for AlterCatalog commands.
33+
*/
34+
public abstract class AlterCatalogCommand extends AlterCommand {
35+
protected final String catalogName;
36+
37+
public AlterCatalogCommand(PlanType type, String catalogName) {
38+
super(type);
39+
this.catalogName = Objects.requireNonNull(catalogName, "Catalog name cannot be null");
40+
}
41+
42+
protected void validate(ConnectContext ctx) throws Exception {
43+
Util.checkCatalogAllRules(catalogName);
44+
45+
if (!Env.getCurrentEnv().getAccessManager().checkCtlPriv(ctx, catalogName, PrivPredicate.ALTER)) {
46+
ErrorReport.reportAnalysisException(ErrorCode.ERR_CATALOG_ACCESS_DENIED,
47+
ctx.getQualifiedUser(), catalogName);
48+
}
49+
50+
if (catalogName.equals(InternalCatalog.INTERNAL_CATALOG_NAME)) {
51+
throw new Exception("Internal catalog cannot be altered.");
52+
}
53+
}
54+
55+
public String getCatalogName() {
56+
return catalogName;
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.commands;
19+
20+
import org.apache.doris.catalog.Env;
21+
import org.apache.doris.common.AnalysisException;
22+
import org.apache.doris.nereids.trees.plans.PlanType;
23+
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
24+
import org.apache.doris.qe.ConnectContext;
25+
import org.apache.doris.qe.StmtExecutor;
26+
27+
import com.google.common.base.Strings;
28+
29+
import java.util.Objects;
30+
31+
/**
32+
* Represents the command for ALTER CATALOG MODIFY COMMENT.
33+
*/
34+
public class AlterCatalogCommentCommand extends AlterCatalogCommand {
35+
36+
private final String comment;
37+
38+
public AlterCatalogCommentCommand(String catalogName, String comment) {
39+
super(PlanType.ALTER_CATALOG_COMMENT_COMMAND, catalogName);
40+
this.comment = Objects.requireNonNull(comment, "Comment cannot be null");
41+
}
42+
43+
@Override
44+
public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
45+
// Validate the catalog name
46+
if (Strings.isNullOrEmpty(comment)) {
47+
throw new AnalysisException("New comment is not set.");
48+
}
49+
50+
// Fetch and modify the catalog's comment
51+
Env.getCurrentEnv().getCatalogMgr().alterCatalogComment(catalogName, comment);
52+
}
53+
54+
@Override
55+
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
56+
return visitor.visitAlterCatalogCommentCommand(this, context);
57+
}
58+
}

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.doris.nereids.trees.plans.commands.AdminCleanTrashCommand;
2323
import org.apache.doris.nereids.trees.plans.commands.AdminCompactTableCommand;
2424
import org.apache.doris.nereids.trees.plans.commands.AdminShowReplicaStatusCommand;
25+
import org.apache.doris.nereids.trees.plans.commands.AlterCatalogCommentCommand;
2526
import org.apache.doris.nereids.trees.plans.commands.AlterJobStatusCommand;
2627
import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand;
2728
import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand;
@@ -324,6 +325,10 @@ default R visitAlterViewCommand(AlterViewCommand alterViewCommand, C context) {
324325
return visitCommand(alterViewCommand, context);
325326
}
326327

328+
default R visitAlterCatalogCommentCommand(AlterCatalogCommentCommand alterCatalogCommentCommand, C context) {
329+
return visitCommand(alterCatalogCommentCommand, context);
330+
}
331+
327332
default R visitDropCatalogRecycleBinCommand(DropCatalogRecycleBinCommand dropCatalogRecycleBinCommand, C context) {
328333
return visitCommand(dropCatalogRecycleBinCommand, context);
329334
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !cmd --
3+
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);
4+
5+
-- !cmd --
6+
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);
7+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
19+
suite("test_alter_catalog_comment_command", "nereids_p0") {
20+
def catalogName = "test_alter_catalog_comment"
21+
def catalogProperties = "\"type\"=\"es\", \"hosts\"=\"http://127.0.0.1:9200\""
22+
23+
try {
24+
// Drop catalog if it already exists
25+
sql("DROP CATALOG IF EXISTS ${catalogName}")
26+
27+
// Create a new catalog
28+
sql(
29+
"""
30+
CREATE CATALOG ${catalogName}
31+
COMMENT 'Initial catalog comment'
32+
PROPERTIES (${catalogProperties})
33+
"""
34+
)
35+
// Verify the catalog was created
36+
checkNereidsExecute("""show create catalog ${catalogName}""")
37+
qt_cmd("""show create catalog ${catalogName}""")
38+
39+
40+
// Alter the catalog comment
41+
checkNereidsExecute(
42+
"""
43+
ALTER CATALOG ${catalogName} MODIFY COMMENT 'Updated catalog comment'
44+
"""
45+
)
46+
// Verify the comment was changed
47+
checkNereidsExecute("""show create catalog ${catalogName}""")
48+
qt_cmd("""show create catalog ${catalogName}""")
49+
50+
} finally {
51+
// Clean up
52+
sql("DROP CATALOG IF EXISTS ${catalogName}")
53+
}
54+
}

0 commit comments

Comments
 (0)