Skip to content

Commit

Permalink
[Enhancement] (nereids)implement alterCatalogCommentCommand in nereids (
Browse files Browse the repository at this point in the history
apache#45160)

Issue Number: close apache#42790
  • Loading branch information
msridhar78 authored Dec 16, 2024
1 parent 830b74c commit b0ab7f1
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,24 +314,30 @@ 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 {
writeUnlock();
}
}

/**
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitAlterCatalogCommentCommand(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Original file line number Diff line number Diff line change
@@ -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}")
}
}

0 comments on commit b0ab7f1

Please sign in to comment.