Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] (nereids)implement alterCatalogRenameCommand in nereids #45163

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -585,7 +586,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,25 +310,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);
}
}
Expand All @@ -337,6 +339,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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;
Expand Down Expand Up @@ -502,6 +503,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;
Expand Down Expand Up @@ -5011,6 +5013,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<String> nameParts = visitMultipartIdentifier(ctx.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitAlterCatalogRenameCommand(this, context);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -398,6 +399,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);
}
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_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);

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