Skip to content

Commit

Permalink
Merge pull request #6687 from sundy-li/new-planner-users
Browse files Browse the repository at this point in the history
refactor(query): users/roles/grants DDL statements default to use new planner
  • Loading branch information
mergify[bot] authored Jul 20, 2022
2 parents f5a7cf2 + 40423da commit c2df4cf
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 91 deletions.
19 changes: 6 additions & 13 deletions common/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,13 +962,14 @@ pub fn grant_level(i: Input) -> IResult<AccountMgrLevel> {
},
|(database, _)| AccountMgrLevel::Database(database.map(|(database, _)| database.name)),
);
// db.table

// `db01`.'tb1' or `db01`.`tb1` or `db01`.tb1
let table = map(
rule! {
( #ident ~ "." )? ~ #ident
( #ident ~ "." )? ~ #parameter_to_string
},
|(database, table)| {
AccountMgrLevel::Table(database.map(|(database, _)| database.name), table.name)
AccountMgrLevel::Table(database.map(|(database, _)| database.name), table)
},
);

Expand Down Expand Up @@ -1257,7 +1258,7 @@ pub fn role_option(i: Input) -> IResult<RoleOption> {
pub fn user_identity(i: Input) -> IResult<UserIdentity> {
map(
rule! {
#literal_string ~ ( "@" ~ #literal_string )?
#parameter_to_string ~ ( "@" ~ #literal_string )?
},
|(username, opt_hostname)| UserIdentity {
username,
Expand All @@ -1278,15 +1279,7 @@ pub fn auth_type(i: Input) -> IResult<AuthType> {
}

pub fn ident_to_string(i: Input) -> IResult<String> {
map_res(ident, |ident| {
if ident.quote.is_none() {
Ok(ident.to_string())
} else {
Err(ErrorKind::Other(
"unexpected quoted identifier, try to remove the quote",
))
}
})(i)
map_res(ident, |ident| Ok(ident.name))(i)
}

pub fn u64_to_string(i: Input) -> IResult<String> {
Expand Down
4 changes: 1 addition & 3 deletions common/ast/tests/it/testdata/statement-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ error:
--> SQL:1:27
|
1 | GRANT ROLE 'test' TO ROLE test-user;
| ----- ^^^^ expected <QuotedString>
| |
| while parsing `GRANT { ROLE <role_name> | schemaObjectPrivileges | ALL [ PRIVILEGES ] ON <privileges_level> } TO { [ROLE <role_name>] | [USER] <user> }`
| ^^^^ expected <QuotedString>, `@`, `FORMAT`, or `;`


---------- Input ----------
Expand Down
11 changes: 11 additions & 0 deletions common/planners/src/plan_show_grants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common_datavalues::DataField;
use common_datavalues::DataSchemaRef;
use common_datavalues::DataSchemaRefExt;
use common_datavalues::ToDataType;
use common_datavalues::Vu8;
use common_meta_types::PrincipalIdentity;

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct ShowGrantsPlan {
pub principal: Option<PrincipalIdentity>,
}

impl ShowGrantsPlan {
pub fn schema(&self) -> DataSchemaRef {
DataSchemaRefExt::create(vec![DataField::new("Grants", Vu8::to_data_type())])
}
}
47 changes: 20 additions & 27 deletions query/src/interpreters/interpreter_factory_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ impl InterpreterFactoryV2 {
matches!(
stmt,
// DfStatement::Query(_)
// | DfStatement::Explain(_)
// | DfStatement::InsertQuery(_)
DfStatement::Copy(_)
// | DfStatement::Explain(_)
| DfStatement::CreateStage(_)
| DfStatement::CreateStage(_)
| DfStatement::ShowStages(_)
| DfStatement::DescribeStage(_)
| DfStatement::List(_)
Expand Down Expand Up @@ -126,23 +127,23 @@ impl InterpreterFactoryV2 {
| DfStatement::ShowProcessList(_)
| DfStatement::ShowSettings(_)
| DfStatement::CreateDatabase(_)
| DfStatement::DropDatabase(_) /* | DfStatement::InsertQuery(_)
* | DfStatement::ShowUsers(_)
* | DfStatement::CreateUser(_)
* | DfStatement::ShowRoles(_)
* | DfStatement::AlterDatabase(_)
* | DfStatement::CreateUDF(_)
* | DfStatement::DropUser(_)
* | DfStatement::AlterUser(_)
* | DfStatement::CreateRole(_)
* | DfStatement::DropRole(_)
* | DfStatement::GrantPrivilege(_)
* | DfStatement::GrantRole(_)
* | DfStatement::ShowGrants(_)
* | DfStatement::RevokeRole(_)
* | DfStatement::RevokePrivilege(_)
* | DfStatement::Call(_)
* | DfStatement::SetVariable(_) */
| DfStatement::DropDatabase(_)
| DfStatement::ShowUsers(_)
| DfStatement::CreateUser(_)
| DfStatement::ShowRoles(_)
| DfStatement::AlterDatabase(_)
| DfStatement::CreateUDF(_)
| DfStatement::DropUser(_)
| DfStatement::AlterUser(_)
| DfStatement::CreateRole(_)
| DfStatement::DropRole(_)
| DfStatement::GrantPrivilege(_)
| DfStatement::GrantRole(_)
| DfStatement::ShowGrants(_)
| DfStatement::RevokeRole(_)
| DfStatement::RevokePrivilege(_)
| DfStatement::Call(_)
| DfStatement::SetVariable(_)
)
}

Expand Down Expand Up @@ -182,11 +183,6 @@ impl InterpreterFactoryV2 {
*copy_plan.clone(),
)?)),

// Shows
Plan::ShowMetrics => Ok(Arc::new(ShowMetricsInterpreter::try_create(ctx)?)),
Plan::ShowProcessList => Ok(Arc::new(ShowProcessListInterpreter::try_create(ctx)?)),
Plan::ShowSettings => Ok(Arc::new(ShowSettingsInterpreter::try_create(ctx)?)),

// Databases
Plan::ShowCreateDatabase(show_create_database) => Ok(Arc::new(
ShowCreateDatabaseInterpreter::try_create(ctx, *show_create_database.clone())?,
Expand Down Expand Up @@ -257,7 +253,6 @@ impl InterpreterFactoryV2 {
)?)),

// Users
Plan::ShowUsers => Ok(Arc::new(ShowUsersInterpreter::try_create(ctx)?)),
Plan::CreateUser(create_user) => Ok(Arc::new(CreateUserInterpreter::try_create(
ctx,
*create_user.clone(),
Expand All @@ -279,7 +274,6 @@ impl InterpreterFactoryV2 {
)?)),

// Roles
Plan::ShowRoles => Ok(Arc::new(ShowRolesInterpreter::try_create(ctx)?)),
Plan::CreateRole(create_role) => Ok(Arc::new(CreateRoleInterpreter::try_create(
ctx,
*create_role.clone(),
Expand All @@ -290,7 +284,6 @@ impl InterpreterFactoryV2 {
)?)),

// Stages
Plan::ShowStages => Ok(Arc::new(ShowStagesInterpreter::try_create(ctx)?)),
Plan::ListStage(s) => Ok(Arc::new(ListInterpreter::try_create(ctx, *s.clone())?)),
Plan::DescribeStage(s) => Ok(Arc::new(DescribeUserStageInterpreter::try_create(
ctx,
Expand Down
9 changes: 6 additions & 3 deletions query/src/interpreters/interpreter_show_grants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl Interpreter for ShowGrantsInterpreter {
&self,
_input_stream: Option<SendableDataBlockStream>,
) -> Result<SendableDataBlockStream> {
let schema = DataSchemaRefExt::create(vec![DataField::new("Grants", Vu8::to_data_type())]);
let tenant = self.ctx.get_tenant();
let user_mgr = self.ctx.get_user_manager();
let role_cache_mgr = self.ctx.get_role_cache_manager();
Expand Down Expand Up @@ -80,7 +79,11 @@ impl Interpreter for ShowGrantsInterpreter {
.map(|e| format!("{} TO {}", e, identity).into_bytes())
.collect::<Vec<_>>();

let block = DataBlock::create(schema.clone(), vec![Series::from_data(grant_list)]);
Ok(Box::pin(DataBlockStream::create(schema, None, vec![block])))
let block = DataBlock::create(self.plan.schema(), vec![Series::from_data(grant_list)]);
Ok(Box::pin(DataBlockStream::create(
self.plan.schema(),
None,
vec![block],
)))
}
}
33 changes: 28 additions & 5 deletions query/src/sql/planner/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub use aggregate::AggregateInfo;
pub use bind_context::BindContext;
pub use bind_context::ColumnBinding;
use common_ast::ast::Statement;
use common_ast::parser::parse_sql;
use common_ast::parser::tokenize_sql;
use common_ast::Backtrace;
use common_datavalues::DataTypeImpl;
use common_exception::ErrorCode;
use common_exception::Result;
Expand Down Expand Up @@ -118,8 +121,17 @@ impl<'a> Binder {

Statement::Copy(stmt) => self.bind_copy(bind_context, stmt).await?,

Statement::ShowMetrics => Plan::ShowMetrics,
Statement::ShowProcessList => Plan::ShowProcessList,
Statement::ShowMetrics => {
self.bind_rewrite_to_query(
bind_context,
"SELECT metric, kind, labels, value FROM system.metrics",
)
.await?
}
Statement::ShowProcessList => {
self.bind_rewrite_to_query(bind_context, "SELECT * FROM system.processes")
.await?
}
Statement::ShowSettings { like } => self.bind_show_settings(bind_context, like).await?,

// Databases
Expand Down Expand Up @@ -156,11 +168,11 @@ impl<'a> Binder {
if_exists: *if_exists,
user: user.clone(),
})),
Statement::ShowUsers => Plan::ShowUsers,
Statement::ShowUsers => self.bind_rewrite_to_query(bind_context, "SELECT name, hostname, auth_type, auth_string FROM system.users ORDER BY name").await?,
Statement::AlterUser(stmt) => self.bind_alter_user(stmt).await?,

// Roles
Statement::ShowRoles => Plan::ShowRoles,
Statement::ShowRoles => self.bind_rewrite_to_query(bind_context, "SELECT name, inherited_roles FROM system.roles ORDER BY name").await?,
Statement::CreateRole {
if_not_exists,
role_name,
Expand All @@ -177,7 +189,7 @@ impl<'a> Binder {
})),

// Stages
Statement::ShowStages => Plan::ShowStages,
Statement::ShowStages => self.bind_rewrite_to_query(bind_context, "SELECT name, stage_type, number_of_files, creator, comment FROM system.stages ORDER BY name").await?,
Statement::ListStage { location, pattern } => {
self.bind_list_stage(location, pattern).await?
}
Expand Down Expand Up @@ -276,6 +288,17 @@ impl<'a> Binder {
Ok(plan)
}

async fn bind_rewrite_to_query(
&mut self,
bind_context: &BindContext,
query: &str,
) -> Result<Plan> {
let tokens = tokenize_sql(query)?;
let backtrace = Backtrace::new();
let (stmt, _) = parse_sql(&tokens, &backtrace)?;
self.bind_statement(bind_context, &stmt).await
}

/// Create a new ColumnBinding with assigned index
pub(super) fn create_column_binding(
&mut self,
Expand Down
13 changes: 2 additions & 11 deletions query/src/sql/planner/binder/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
// limitations under the License.

use common_ast::ast::ShowLimit;
use common_ast::parser::parse_sql;
use common_ast::parser::tokenize_sql;
use common_ast::Backtrace;
use common_exception::Result;

use crate::sql::plans::Plan;
Expand Down Expand Up @@ -47,10 +44,7 @@ impl<'a> Binder {
}
}
);
let tokens = tokenize_sql(query.as_str())?;
let backtrace = Backtrace::new();
let (stmt, _) = parse_sql(&tokens, &backtrace)?;
self.bind_statement(bind_context, &stmt).await
self.bind_rewrite_to_query(bind_context, &query).await
}

pub(in crate::sql::planner::binder) async fn bind_show_settings(
Expand All @@ -67,9 +61,6 @@ impl<'a> Binder {
sub_query
);

let tokens = tokenize_sql(query.as_str())?;
let backtrace = Backtrace::new();
let (stmt, _) = parse_sql(&tokens, &backtrace)?;
self.bind_statement(bind_context, &stmt).await
self.bind_rewrite_to_query(bind_context, &query).await
}
}
8 changes: 0 additions & 8 deletions query/src/sql/planner/format/display_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,24 @@ impl Plan {
Plan::Delete(delete) => Ok(format!("{:?}", delete)),

// Stages
Plan::ShowStages => Ok("SHOW STAGES".to_string()),
Plan::ListStage(s) => Ok(format!("{:?}", s)),
Plan::DescribeStage(s) => Ok(format!("{:?}", s)),
Plan::CreateStage(create_stage) => Ok(format!("{:?}", create_stage)),
Plan::DropStage(s) => Ok(format!("{:?}", s)),
Plan::RemoveStage(s) => Ok(format!("{:?}", s)),

// Shows
Plan::ShowMetrics => Ok("SHOW METRICS".to_string()),
Plan::ShowProcessList => Ok("SHOW PROCESSLIST".to_string()),
Plan::ShowSettings => Ok("SHOW SETTINGS".to_string()),

// Account
Plan::GrantRole(grant_role) => Ok(format!("{:?}", grant_role)),
Plan::GrantPriv(grant_priv) => Ok(format!("{:?}", grant_priv)),
Plan::ShowGrants(show_grants) => Ok(format!("{:?}", show_grants)),
Plan::RevokePriv(revoke_priv) => Ok(format!("{:?}", revoke_priv)),
Plan::RevokeRole(revoke_role) => Ok(format!("{:?}", revoke_role)),
Plan::ShowUsers => Ok("SHOW USERS".to_string()),
Plan::CreateUser(create_user) => Ok(format!("{:?}", create_user)),
Plan::DropUser(drop_user) => Ok(format!("{:?}", drop_user)),
Plan::CreateUDF(create_user_udf) => Ok(format!("{:?}", create_user_udf)),
Plan::AlterUDF(alter_user_udf) => Ok(format!("{alter_user_udf:?}")),
Plan::DropUDF(drop_udf) => Ok(format!("{drop_udf:?}")),
Plan::AlterUser(alter_user) => Ok(format!("{:?}", alter_user)),
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
Loading

1 comment on commit c2df4cf

@vercel
Copy link

@vercel vercel bot commented on c2df4cf Jul 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

databend – ./

databend.rs
databend.vercel.app
databend-git-main-databend.vercel.app
databend-databend.vercel.app

Please sign in to comment.