Skip to content

Commit

Permalink
Migrate show users/roles statement to new planner
Browse files Browse the repository at this point in the history
Signed-off-by: Ye Sijun <junnplus@gmail.com>
  • Loading branch information
junnplus committed Jun 16, 2022
1 parent f5e238e commit 7956788
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 12 deletions.
8 changes: 8 additions & 0 deletions common/ast/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub enum Statement<'a> {
DropView(DropViewStmt<'a>),

// User
ShowUsers,
CreateUser(CreateUserStmt),
AlterUser {
// None means current user
Expand All @@ -109,6 +110,7 @@ pub enum Statement<'a> {
if_exists: bool,
user: UserIdentity,
},
ShowRoles,
CreateRole {
if_not_exists: bool,
role_name: String,
Expand Down Expand Up @@ -924,6 +926,12 @@ impl<'a> Display for Statement<'a> {
}
write_period_separated_list(f, catalog.iter().chain(database).chain(Some(view)))?;
}
Statement::ShowUsers => {
write!(f, "SHOW USERS")?;
}
Statement::ShowRoles => {
write!(f, "SHOW ROLES")?;
}
Statement::CreateUser(CreateUserStmt {
if_not_exists,
user,
Expand Down
6 changes: 5 additions & 1 deletion common/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ pub fn statement(i: Input) -> IResult<Statement> {
})
},
);
let show_users = value(Statement::ShowUsers, rule! { SHOW ~ USERS });
let create_user = map(
rule! {
CREATE ~ USER ~ ( IF ~ NOT ~ EXISTS )?
Expand Down Expand Up @@ -421,6 +422,7 @@ pub fn statement(i: Input) -> IResult<Statement> {
user,
},
);
let show_roles = value(Statement::ShowRoles, rule! { SHOW ~ ROLES });
let create_role = map(
rule! {
CREATE ~ ROLE ~ ( IF ~ NOT ~ EXISTS )? ~ #literal_string
Expand Down Expand Up @@ -627,9 +629,11 @@ pub fn statement(i: Input) -> IResult<Statement> {
| #alter_view : "`ALTER VIEW [<database>.]<view> AS SELECT ...`"
),
rule!(
#create_user : "`CREATE USER [IF NOT EXISTS] '<username>'@'hostname' IDENTIFIED [WITH <auth_type>] [BY <password>] [WITH <role_option> ...]`"
#show_users : "`SHOW USERS`"
| #create_user : "`CREATE USER [IF NOT EXISTS] '<username>'@'hostname' IDENTIFIED [WITH <auth_type>] [BY <password>] [WITH <role_option> ...]`"
| #alter_user : "`ALTER USER ('<username>'@'hostname' | USER()) [IDENTIFIED [WITH <auth_type>] [BY <password>]] [WITH <role_option> ...]`"
| #drop_user : "`DROP USER [IF EXISTS] '<username>'@'hostname'`"
| #show_roles : "`SHOW ROLES`"
| #create_role : "`CREATE ROLE [IF NOT EXISTS] '<role_name>']`"
| #drop_role : "`DROP ROLE [IF EXISTS] '<role_name>'`"
| #create_udf : "`CREATE FUNCTION [IF NOT EXISTS] <udf_name> (<parameter>, ...) -> <definition expr> [DESC = <description>]`"
Expand Down
4 changes: 4 additions & 0 deletions common/ast/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ pub enum TokenKind {
KILL,
#[token("ROLE", ignore(ascii_case))]
ROLE,
#[token("ROLES", ignore(ascii_case))]
ROLES,
#[token("LEADING", ignore(ascii_case))]
LEADING,
#[token("LEFT", ignore(ascii_case))]
Expand Down Expand Up @@ -621,6 +623,8 @@ pub enum TokenKind {
USE,
#[token("USER", ignore(ascii_case))]
USER,
#[token("USERS", ignore(ascii_case))]
USERS,
#[token("USING", ignore(ascii_case))]
USING,
#[token("VALUES", ignore(ascii_case))]
Expand Down
8 changes: 2 additions & 6 deletions query/src/interpreters/interpreter_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,8 @@ impl InterpreterFactory {
PlanNode::Show(ShowPlan::ShowSettings(_)) => {
ShowSettingsInterpreter::try_create(ctx_clone)
}
PlanNode::Show(ShowPlan::ShowUsers(v)) => {
ShowUsersInterpreter::try_create(ctx_clone, v)
}
PlanNode::Show(ShowPlan::ShowRoles(v)) => {
ShowRolesInterpreter::try_create(ctx_clone, v)
}
PlanNode::Show(ShowPlan::ShowUsers(_)) => ShowUsersInterpreter::try_create(ctx_clone),
PlanNode::Show(ShowPlan::ShowRoles(_)) => ShowRolesInterpreter::try_create(ctx_clone),
PlanNode::Show(ShowPlan::ShowStages) => ShowStagesInterpreter::try_create(ctx_clone),

// Database related transforms.
Expand Down
8 changes: 8 additions & 0 deletions query/src/interpreters/interpreter_factory_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ impl InterpreterFactoryV2 {
| DfStatement::ShowSettings(_)
| DfStatement::CreateDatabase(_)
| DfStatement::DropDatabase(_)
| DfStatement::ShowUsers(_)
| DfStatement::CreateUser(_)
| DfStatement::DropUser(_)
| DfStatement::AlterUser(_)
| DfStatement::ShowRoles(_)
| DfStatement::CreateRole(_)
| DfStatement::DropRole(_)
| DfStatement::AlterDatabase(_)
)
}
Expand Down Expand Up @@ -150,6 +156,7 @@ impl InterpreterFactoryV2 {
Plan::DropView(drop_view) => DropViewInterpreter::try_create(ctx, *drop_view.clone()),

// Users
Plan::ShowUsers => ShowUsersInterpreter::try_create(ctx),
Plan::CreateUser(create_user) => {
CreateUserInterpreter::try_create(ctx, *create_user.clone())
}
Expand All @@ -159,6 +166,7 @@ impl InterpreterFactoryV2 {
}

// Roles
Plan::ShowRoles => ShowRolesInterpreter::try_create(ctx),
Plan::CreateRole(create_role) => {
CreateRoleInterpreter::try_create(ctx, *create_role.clone())
}
Expand Down
3 changes: 1 addition & 2 deletions query/src/interpreters/interpreter_show_roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::sync::Arc;
use common_exception::ErrorCode;
use common_exception::Result;
use common_planners::PlanNode;
use common_planners::ShowRolesPlan;
use common_streams::SendableDataBlockStream;

use crate::interpreters::Interpreter;
Expand All @@ -32,7 +31,7 @@ pub struct ShowRolesInterpreter {
}

impl ShowRolesInterpreter {
pub fn try_create(ctx: Arc<QueryContext>, _plan: ShowRolesPlan) -> Result<InterpreterPtr> {
pub fn try_create(ctx: Arc<QueryContext>) -> Result<InterpreterPtr> {
Ok(Arc::new(ShowRolesInterpreter { ctx }))
}

Expand Down
3 changes: 1 addition & 2 deletions query/src/interpreters/interpreter_show_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::sync::Arc;
use common_exception::ErrorCode;
use common_exception::Result;
use common_planners::PlanNode;
use common_planners::ShowUsersPlan;
use common_streams::SendableDataBlockStream;

use crate::interpreters::Interpreter;
Expand All @@ -32,7 +31,7 @@ pub struct ShowUsersInterpreter {
}

impl ShowUsersInterpreter {
pub fn try_create(ctx: Arc<QueryContext>, _plan: ShowUsersPlan) -> Result<InterpreterPtr> {
pub fn try_create(ctx: Arc<QueryContext>) -> Result<InterpreterPtr> {
Ok(Arc::new(ShowUsersInterpreter { ctx }))
}

Expand Down
2 changes: 2 additions & 0 deletions query/src/sql/planner/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl<'a> Binder {
if_exists: *if_exists,
user: user.clone(),
})),
Statement::ShowUsers => Plan::ShowUsers,
Statement::AlterUser {
user,
auth_option,
Expand All @@ -151,6 +152,7 @@ impl<'a> Binder {
}

// Roles
Statement::ShowRoles => Plan::ShowRoles,
Statement::CreateRole {
if_not_exists,
role_name,
Expand Down
2 changes: 2 additions & 0 deletions query/src/sql/planner/format/display_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ impl Plan {
Plan::DropView(drop_view) => Ok(format!("{:?}", drop_view)),

// Users
Plan::ShowUsers => Ok("SHOW USERS".to_string()),
Plan::CreateUser(create_user) => Ok(format!("{:?}", create_user)),
Plan::DropUser(drop_user) => Ok(format!("{:?}", drop_user)),
Plan::AlterUser(alter_user) => Ok(format!("{:?}", alter_user)),

// Roles
Plan::ShowRoles => Ok("SHOW ROLES".to_string()),
Plan::CreateRole(create_role) => Ok(format!("{:?}", create_role)),
Plan::DropRole(drop_role) => Ok(format!("{:?}", drop_role)),

Expand Down
6 changes: 5 additions & 1 deletion query/src/sql/planner/plans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ pub enum Plan {
AlterView(Box<AlterViewPlan>),
DropView(Box<DropViewPlan>),

// DCL
// Users
ShowUsers,
AlterUser(Box<AlterUserPlan>),
CreateUser(Box<CreateUserPlan>),
DropUser(Box<DropUserPlan>),

// Roles
ShowRoles,
CreateRole(Box<CreateRolePlan>),
DropRole(Box<DropRolePlan>),

Expand Down
1 change: 1 addition & 0 deletions tests/suites/0_stateless/06_show/06_0007_show_roles.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
CREATE ROLE 'test';
SHOW ROLES;
DROP ROLE 'test';
2 changes: 2 additions & 0 deletions tests/suites/0_stateless/06_show/06_0007_show_roles_v2.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
account_admin 0
test 0
4 changes: 4 additions & 0 deletions tests/suites/0_stateless/06_show/06_0007_show_roles_v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set enable_planner_v2 = 1;
CREATE ROLE 'test';
SHOW ROLES;
DROP ROLE 'test';

0 comments on commit 7956788

Please sign in to comment.