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 DropUserCommand in nereids #44431

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -198,6 +198,7 @@ supportedDropStatement
: DROP CATALOG RECYCLE BIN WHERE idType=STRING_LITERAL EQ id=INTEGER_VALUE #dropCatalogRecycleBin
| DROP ROLE (IF EXISTS)? name=identifier #dropRole
| DROP SQL_BLOCK_RULE (IF EXISTS)? identifierSeq #dropSqlBlockRule
| DROP USER (IF EXISTS)? userIdentify #dropUser
;

supportedShowStatement
Expand Down Expand Up @@ -661,7 +662,6 @@ unsupportedDropStatement
| DROP (GLOBAL | SESSION | LOCAL)? FUNCTION (IF EXISTS)?
functionIdentifier LEFT_PAREN functionArguments? RIGHT_PAREN #dropFunction
| DROP TABLE (IF EXISTS)? name=multipartIdentifier FORCE? #dropTable
| DROP USER (IF EXISTS)? userIdentify #dropUser
| DROP VIEW (IF EXISTS)? name=multipartIdentifier #dropView
| DROP REPOSITORY name=identifier #dropRepository
| DROP FILE name=STRING_LITERAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@ private void createUserInternal(UserIdentity userIdent, String roleName, byte[]
}
}

public void dropUser(UserIdentity userIdent, boolean ignoreIfNonExists) throws DdlException {
dropUserInternal(userIdent, ignoreIfNonExists, false);
}

// drop user
public void dropUser(DropUserStmt stmt) throws DdlException {
dropUserInternal(stmt.getUserIdentity(), stmt.isSetIfExists(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
import org.apache.doris.nereids.DorisParser.DropProcedureContext;
import org.apache.doris.nereids.DorisParser.DropRoleContext;
import org.apache.doris.nereids.DorisParser.DropSqlBlockRuleContext;
import org.apache.doris.nereids.DorisParser.DropUserContext;
import org.apache.doris.nereids.DorisParser.ElementAtContext;
import org.apache.doris.nereids.DorisParser.ExceptContext;
import org.apache.doris.nereids.DorisParser.ExceptOrReplaceContext;
Expand Down Expand Up @@ -446,6 +447,7 @@
import org.apache.doris.nereids.trees.plans.commands.DropProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.DropRoleCommand;
import org.apache.doris.nereids.trees.plans.commands.DropSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.DropUserCommand;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
import org.apache.doris.nereids.trees.plans.commands.ExportCommand;
Expand Down Expand Up @@ -4333,6 +4335,12 @@ public LogicalPlan visitDropSqlBlockRule(DropSqlBlockRuleContext ctx) {
return new DropSqlBlockRuleCommand(visitIdentifierSeq(ctx.identifierSeq()), ctx.EXISTS() != null);
}

@Override
public LogicalPlan visitDropUser(DropUserContext ctx) {
UserIdentity userIdent = visitUserIdentify(ctx.userIdentify());
return new DropUserCommand(userIdent, ctx.EXISTS() != null);
}

@Override
public LogicalPlan visitShowTableId(ShowTableIdContext ctx) {
long tableId = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public enum PlanType {
PREPARED_COMMAND,
EXECUTE_COMMAND,
DROP_SQL_BLOCK_RULE_COMMAND,
DROP_USER_COMMAND,
SHOW_BACKENDS_COMMAND,
SHOW_BLOCK_RULE_COMMAND,
SHOW_CONFIG_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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.UserIdentity;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.Config;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.mysql.authenticate.AuthenticateType;
import org.apache.doris.mysql.privilege.PrivPredicate;
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;

/**
* drop user command
*/
public class DropUserCommand extends DropCommand {
private final boolean ifExists;
private final UserIdentity userIdent;

/**
* constructor
*/
public DropUserCommand(UserIdentity userIdent, boolean ifExists) {
super(PlanType.DROP_USER_COMMAND);
this.userIdent = userIdent;
this.ifExists = ifExists;
}

@Override
public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
if (Config.access_controller_type.equalsIgnoreCase("ranger-doris")
&& AuthenticateType.getAuthTypeConfig() == AuthenticateType.LDAP) {
throw new AnalysisException("Drop user is prohibited when Ranger and LDAP are enabled at same time.");
}

userIdent.analyze();

if (userIdent.isRootUser()) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_COMMON_ERROR, "Can not drop root user");
}

// only user with GLOBAL level's GRANT_PRIV can drop user.
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.GRANT)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "DROP USER");
}
Env.getCurrentEnv().getAuth().dropUser(userIdent, ifExists);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitDropUserCommand(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.doris.nereids.trees.plans.commands.DropProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.DropRoleCommand;
import org.apache.doris.nereids.trees.plans.commands.DropSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.DropUserCommand;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
import org.apache.doris.nereids.trees.plans.commands.ExportCommand;
import org.apache.doris.nereids.trees.plans.commands.LoadCommand;
Expand Down Expand Up @@ -398,6 +399,10 @@ default R visitDropSqlBlockRuleCommand(DropSqlBlockRuleCommand dropSqlBlockRuleC
return visitCommand(dropSqlBlockRuleCommand, context);
}

default R visitDropUserCommand(DropUserCommand dropUserCommand, C context) {
return visitCommand(dropUserCommand, context);
}

default R visitShowTableIdCommand(ShowTableIdCommand showTableIdCommand, C context) {
return visitCommand(showTableIdCommand, context);
}
Expand Down
44 changes: 44 additions & 0 deletions regression-test/suites/account_p0/test_nereids_account.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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.

import org.junit.Assert;

suite("test_nereids_account") {
// test comment
def user = "ttest_nereids_account_comment_user";
sql """drop user if exists ${user}"""
// create user with comment
sql """create user ${user} comment 'ttest_nereids_account_comment_user_comment_create'"""
def user_create = sql "show grants for ${user}"
logger.info("user_create: " + user_create.toString())
assertTrue(user_create.toString().contains("ttest_nereids_account_comment_user_comment_create"))
// alter user comment
sql """alter user ${user} comment 'ttest_nereids_account_comment_user_comment_alter'"""
def user_alter = sql "show grants for ${user}"
logger.info("user_alter: " + user_alter.toString())
assertTrue(user_alter.toString().contains("ttest_nereids_account_comment_user_comment_alter"))
// drop user
checkNereidsExecute("""drop user ${user}""")
checkNereidsExecute("""drop user if exists ${user}""")
try {
sql "show grants for ${user}"
fail()
} catch (Exception e) {
log.info(e.getMessage())
assertTrue(e.getMessage().contains('not exist'))
}
}
Loading