morrySnow commented on code in PR #42935: URL: https://github.com/apache/doris/pull/42935#discussion_r1823706043
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropDatabaseCommand.java: ########## @@ -0,0 +1,95 @@ +// 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.analysis.StmtType; +import org.apache.doris.catalog.DatabaseIf; +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.MysqlCompatibleDatabase; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; +import org.apache.doris.common.UserException; +import org.apache.doris.common.util.InternalDatabaseUtil; +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.nereids.trees.plans.commands.info.DropDatabaseInfo; +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 org.apache.commons.lang3.StringUtils; + +/** + * drop database command + */ +public class DropDatabaseCommand extends Command implements ForwardWithSync { + private final DropDatabaseInfo dropDatabaseInfo; + + public DropDatabaseCommand(DropDatabaseInfo dropDatabaseInfo) { + super(PlanType.DROP_DATABASE_COMMAND); + this.dropDatabaseInfo = dropDatabaseInfo; + } + + private void analyze(ConnectContext ctx) throws UserException { + String databaseName = dropDatabaseInfo.getDatabaseName(); + String catalogName = dropDatabaseInfo.getCatalogName(); + if (Strings.isNullOrEmpty(databaseName)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_DB_NAME, databaseName); + } + + InternalDatabaseUtil.checkDatabase(databaseName, ctx); + + DatabaseIf db = Env.getCurrentInternalCatalog().getDbNullable(databaseName); + if (db != null && db instanceof MysqlCompatibleDatabase) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_DBACCESS_DENIED_ERROR, + ctx.getQualifiedUser(), databaseName); + } + + String effectiveCatalog = StringUtils.isEmpty(catalogName) + ? InternalCatalog.INTERNAL_CATALOG_NAME : catalogName; + + if (!Env.getCurrentEnv().getAccessManager() + .checkDbPriv(ctx, effectiveCatalog, databaseName, PrivPredicate.DROP)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_DBACCESS_DENIED_ERROR, + ctx.getQualifiedUser(), databaseName); + } + } + + @Override + public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { + try { + analyze(ctx); + Env.getCurrentEnv().dropDb(dropDatabaseInfo.toDropDbStmt()); Review Comment: we should not reuse DropDbStmt anymore, reimplement Env#dropDb without it ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropDatabaseCommand.java: ########## @@ -0,0 +1,95 @@ +// 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.analysis.StmtType; +import org.apache.doris.catalog.DatabaseIf; +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.MysqlCompatibleDatabase; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; +import org.apache.doris.common.UserException; +import org.apache.doris.common.util.InternalDatabaseUtil; +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.nereids.trees.plans.commands.info.DropDatabaseInfo; +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 org.apache.commons.lang3.StringUtils; + +/** + * drop database command + */ +public class DropDatabaseCommand extends Command implements ForwardWithSync { + private final DropDatabaseInfo dropDatabaseInfo; + + public DropDatabaseCommand(DropDatabaseInfo dropDatabaseInfo) { + super(PlanType.DROP_DATABASE_COMMAND); + this.dropDatabaseInfo = dropDatabaseInfo; + } + + private void analyze(ConnectContext ctx) throws UserException { + String databaseName = dropDatabaseInfo.getDatabaseName(); + String catalogName = dropDatabaseInfo.getCatalogName(); + if (Strings.isNullOrEmpty(databaseName)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_DB_NAME, databaseName); + } + + InternalDatabaseUtil.checkDatabase(databaseName, ctx); + + DatabaseIf db = Env.getCurrentInternalCatalog().getDbNullable(databaseName); + if (db != null && db instanceof MysqlCompatibleDatabase) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_DBACCESS_DENIED_ERROR, + ctx.getQualifiedUser(), databaseName); + } + + String effectiveCatalog = StringUtils.isEmpty(catalogName) + ? InternalCatalog.INTERNAL_CATALOG_NAME : catalogName; Review Comment: if catalogName is empty, we should use currentCatalog? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/DropDatabaseInfo.java: ########## @@ -0,0 +1,70 @@ +// 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.info; + +import org.apache.doris.analysis.DbName; +import org.apache.doris.analysis.DropDbStmt; +import org.apache.doris.common.AnalysisException; + +import java.util.List; + +/** + * drop database info + */ +public class DropDatabaseInfo { + private final boolean ifExists; + private final String catalogName; + private final String databaseName; + private final boolean force; + + /** + * Constructor for DropDatabaseInfo. + */ + public DropDatabaseInfo(boolean ifExists, List<String> databaseNameParts, boolean force) { + this.ifExists = ifExists; + if (databaseNameParts.size() > 1) { + this.catalogName = databaseNameParts.get(databaseNameParts.size() - 2); + this.databaseName = databaseNameParts.get(databaseNameParts.size() - 1); + } else { Review Comment: should only allow databaseNameParts == 2 || databaseNameParts == 1 ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropDatabaseCommand.java: ########## @@ -0,0 +1,95 @@ +// 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.analysis.StmtType; +import org.apache.doris.catalog.DatabaseIf; +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.MysqlCompatibleDatabase; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; +import org.apache.doris.common.UserException; +import org.apache.doris.common.util.InternalDatabaseUtil; +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.nereids.trees.plans.commands.info.DropDatabaseInfo; +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 org.apache.commons.lang3.StringUtils; + +/** + * drop database command + */ +public class DropDatabaseCommand extends Command implements ForwardWithSync { + private final DropDatabaseInfo dropDatabaseInfo; + + public DropDatabaseCommand(DropDatabaseInfo dropDatabaseInfo) { + super(PlanType.DROP_DATABASE_COMMAND); + this.dropDatabaseInfo = dropDatabaseInfo; + } + + private void analyze(ConnectContext ctx) throws UserException { + String databaseName = dropDatabaseInfo.getDatabaseName(); + String catalogName = dropDatabaseInfo.getCatalogName(); + if (Strings.isNullOrEmpty(databaseName)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_DB_NAME, databaseName); + } + + InternalDatabaseUtil.checkDatabase(databaseName, ctx); + + DatabaseIf db = Env.getCurrentInternalCatalog().getDbNullable(databaseName); + if (db != null && db instanceof MysqlCompatibleDatabase) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_DBACCESS_DENIED_ERROR, + ctx.getQualifiedUser(), databaseName); + } + + String effectiveCatalog = StringUtils.isEmpty(catalogName) + ? InternalCatalog.INTERNAL_CATALOG_NAME : catalogName; + + if (!Env.getCurrentEnv().getAccessManager() + .checkDbPriv(ctx, effectiveCatalog, databaseName, PrivPredicate.DROP)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_DBACCESS_DENIED_ERROR, + ctx.getQualifiedUser(), databaseName); + } + } + + @Override + public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { + try { + analyze(ctx); + Env.getCurrentEnv().dropDb(dropDatabaseInfo.toDropDbStmt()); + } catch (UserException e) { + throw new AnalysisException(e.getMessage(), e); + } Review Comment: why do this try catch? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org