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 CreateRoleCommand in nereids #44814

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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 @@ -181,6 +181,7 @@ supportedCreateStatement
| CREATE (EXTERNAL)? TABLE (IF NOT EXISTS)? name=multipartIdentifier
LIKE existedTable=multipartIdentifier
(WITH ROLLUP (rollupNames=identifierList)?)? #createTableLike
| CREATE ROLE (IF NOT EXISTS)? name=identifier (COMMENT STRING_LITERAL)? #createRole
| CREATE ROW POLICY (IF NOT EXISTS)? name=identifier
ON table=multipartIdentifier
AS type=(RESTRICTIVE | PERMISSIVE)
Expand Down Expand Up @@ -749,7 +750,6 @@ unsupportedCreateStatement
(SUPERUSER | DEFAULT ROLE role=STRING_LITERAL)?
passwordOption (COMMENT STRING_LITERAL)? #createUser
| CREATE (READ ONLY)? REPOSITORY name=identifier WITH storageBackend #createRepository
| CREATE ROLE (IF NOT EXISTS)? name=identifier (COMMENT STRING_LITERAL)? #createRole
| CREATE FILE name=STRING_LITERAL
((FROM | IN) database=identifier)? properties=propertyClause #createFile
| CREATE INDEX (IF NOT EXISTS)? name=identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ public void createRole(CreateRoleStmt stmt) throws DdlException {
createRoleInternal(stmt.getRole(), stmt.isSetIfNotExists(), stmt.getComment(), false);
}

public void createRole(String role, boolean ignoreIfExists, String comment) throws DdlException {
createRoleInternal(role, ignoreIfExists, comment, false);
}

public void alterRole(AlterRoleStmt stmt) throws DdlException {
alterRoleInternal(stmt.getRole(), stmt.getComment(), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import org.apache.doris.nereids.DorisParser.ConstantContext;
import org.apache.doris.nereids.DorisParser.CreateMTMVContext;
import org.apache.doris.nereids.DorisParser.CreateProcedureContext;
import org.apache.doris.nereids.DorisParser.CreateRoleContext;
import org.apache.doris.nereids.DorisParser.CreateRoutineLoadContext;
import org.apache.doris.nereids.DorisParser.CreateRowPolicyContext;
import org.apache.doris.nereids.DorisParser.CreateSqlBlockRuleContext;
Expand Down Expand Up @@ -480,6 +481,7 @@
import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateRoleCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableLikeCommand;
Expand Down Expand Up @@ -4669,6 +4671,13 @@ public LogicalPlan visitAlterRole(AlterRoleContext ctx) {
return new AlterRoleCommand(ctx.role.getText(), comment);
}

@Override
public LogicalPlan visitCreateRole(CreateRoleContext ctx) {
String comment = ctx.STRING_LITERAL() == null ? "" : LogicalPlanBuilderAssistant.escapeBackSlash(
ctx.STRING_LITERAL().getText().substring(1, ctx.STRING_LITERAL().getText().length() - 1));
return new CreateRoleCommand(ctx.EXISTS() != null, ctx.name.getText(), comment);
}

@Override
public LogicalPlan visitShowFrontends(ShowFrontendsContext ctx) {
String detail = (ctx.name != null) ? ctx.name.getText() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public enum PlanType {
SHOW_CREATE_PROCEDURE_COMMAND,
CREATE_VIEW_COMMAND,
CLEAN_ALL_PROFILE_COMMAND,
CREATE_ROLE_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,75 @@
// 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.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.common.FeNameFormat;
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;

import com.google.common.base.Strings;

/**
* Create role command
*/
public class CreateRoleCommand extends Command implements ForwardWithSync {
private boolean ifNotExists;
private String role;
private String comment;

/**
* ctor of this command.
*/
public CreateRoleCommand(boolean ifNotExists, String role, String comment) {
super(PlanType.CREATE_ROLE_COMMAND);
this.ifNotExists = ifNotExists;
this.role = role;
this.comment = Strings.nullToEmpty(comment);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitCreateRoleCommand(this, context);
}

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
if (Config.access_controller_type.equalsIgnoreCase("ranger-doris")) {
throw new AnalysisException("Create role is prohibited when Ranger is enabled.");
}
FeNameFormat.checkRoleName(role, false /* can not be admin */, "Can not create role");
// check if current user has GRANT priv on GLOBAL level.
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.GRANT)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "CREATE ROLE");
}
Env.getCurrentEnv().getAuth().createRole(role, ifNotExists, comment);
}

@Override
public StmtType stmtType() {
return StmtType.CREATE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.StmtExecutor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* drop roles command
*/
public class DropRoleCommand extends DropCommand {
public static final Logger LOG = LogManager.getLogger(DropRoleCommand.class);
private final boolean ifExists;
private final String role;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateRoleCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableLikeCommand;
Expand Down Expand Up @@ -481,6 +482,10 @@ default R visitCreateSqlBlockRuleCommand(CreateSqlBlockRuleCommand dropRoleComma
return visitCommand(dropRoleCommand, context);
}

default R visitCreateRoleCommand(CreateRoleCommand createRoleCommand, C context) {
return visitCommand(createRoleCommand, context);
}

default R visitDropRoleCommand(DropRoleCommand dropRoleCommand, C context) {
return visitCommand(dropRoleCommand, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.junit.Assert;

suite("test_nereids_role") {
def role= 'nereids_account_role_test'
def user = 'acount_role_user_test'
def user = 'nereids_acount_role_user_test'
def dbName = 'nereids_account_role_test_db'
def pwd = 'C123_567p'

Expand All @@ -28,7 +28,7 @@ suite("test_nereids_role") {
sql """DROP DATABASE IF EXISTS ${dbName}"""
sql """CREATE DATABASE ${dbName}"""

sql """CREATE ROLE ${role}"""
checkNereidsExecute("CREATE ROLE ${role}")
sql """GRANT SELECT_PRIV ON ${context.config.defaultDb} TO ROLE '${role}'"""
sql """GRANT SELECT_PRIV ON ${dbName} TO ROLE '${role}'"""
sql """CREATE USER '${user}' IDENTIFIED BY '${pwd}' DEFAULT ROLE '${role}'"""
Expand Down

This file was deleted.

Loading