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

feat(planner): support drop database in new planner #5846

Merged
merged 3 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 19 additions & 12 deletions common/ast/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ pub enum Statement<'a> {
database: Identifier<'a>,
},
CreateDatabase(CreateDatabaseStmt<'a>),
DropDatabase {
if_exists: bool,
database: Identifier<'a>,
},
DropDatabase(DropDatabaseStmt<'a>),
AlterDatabase {
if_exists: bool,
database: Identifier<'a>,
Expand Down Expand Up @@ -206,10 +203,17 @@ pub struct CreateDatabaseStmt<'a> {
pub if_not_exists: bool,
pub catalog: Option<Identifier<'a>>,
pub database: Identifier<'a>,
pub engine: DatabaseEngine,
pub engine: Option<DatabaseEngine>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We still need DatabaseEngine::Default to distinguish none-engine and default engine,

because explicitly specifying engine is allowed in planner v1, like:
CREATE DATABASE t engine=default;

cc: @andylokandy

pub options: Vec<SQLProperty>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct DropDatabaseStmt<'a> {
pub if_exists: bool,
pub catalog: Option<Identifier<'a>>,
pub database: Identifier<'a>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CreateTableStmt<'a> {
pub if_not_exists: bool,
Expand Down Expand Up @@ -473,18 +477,21 @@ impl<'a> Display for Statement<'a> {
write!(f, "IF NOT EXISTS ")?;
}
write_period_separated_list(f, catalog.iter().chain(Some(database)))?;
write!(f, " ENGINE = {engine}")?;
if let Some(engine) = engine {
write!(f, " ENGINE = {engine}")?;
}
// TODO(leiysky): display rest information
}
Statement::DropDatabase {
database,
Statement::DropDatabase(DropDatabaseStmt {
if_exists,
} => {
write!(f, "DROP DATABASE")?;
catalog,
database,
}) => {
write!(f, "DROP DATABASE ")?;
if *if_exists {
write!(f, " IF EXISTS")?;
write!(f, "IF EXISTS ")?;
}
write!(f, " {database}")?;
write_period_separated_list(f, catalog.iter().chain(Some(database)))?;
}
Statement::AlterDatabase {
if_exists,
Expand Down
15 changes: 9 additions & 6 deletions common/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,26 @@ pub fn statement(i: Input) -> IResult<Statement> {
rule! {
CREATE ~ ( DATABASE | SCHEMA ) ~ ( IF ~ NOT ~ EXISTS )? ~ ( #ident ~ "." )? ~ #ident ~ #database_engine?
},
|(_, _, opt_if_not_exists, opt_catalog, database, opt_engine)| {
|(_, _, opt_if_not_exists, opt_catalog, database, engine)| {
Statement::CreateDatabase(CreateDatabaseStmt {
if_not_exists: opt_if_not_exists.is_some(),
catalog: opt_catalog.map(|(catalog, _)| catalog),
database,
engine: opt_engine.unwrap_or(DatabaseEngine::Default),
engine,
options: vec![],
})
},
);
let drop_database = map(
rule! {
DROP ~ ( DATABASE | SCHEMA ) ~ ( IF ~ EXISTS )? ~ #ident
DROP ~ ( DATABASE | SCHEMA ) ~ ( IF ~ EXISTS )? ~ ( #ident ~ "." )? ~ #ident
},
|(_, _, opt_if_exists, database)| Statement::DropDatabase {
if_exists: opt_if_exists.is_some(),
database,
|(_, _, opt_if_exists, opt_catalog, database)| {
Statement::DropDatabase(DropDatabaseStmt {
if_exists: opt_if_exists.is_some(),
catalog: opt_catalog.map(|(catalog, _)| catalog),
database,
})
},
);
let alter_database = map(
Expand Down
2 changes: 2 additions & 0 deletions common/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ fn test_statement() {
r#"create database catalog.t engine = Default;"#,
r#"create database t engine = Github(token='123456');"#,
r#"create database t engine = Default;"#,
r#"drop database catalog.t;"#,
r#"drop database if exists t;"#,
r#"create table c(a DateTime null, b DateTime(3));"#,
r#"create view v as select number % 3 as a from numbers(1000);"#,
r#"truncate table test;"#,
Expand Down
77 changes: 64 additions & 13 deletions common/ast/tests/it/testdata/statement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ UseDatabase {
---------- Input ----------
create database if not exists a;
---------- Output ---------
CREATE DATABASE IF NOT EXISTS a ENGINE = DEFAULT
CREATE DATABASE IF NOT EXISTS a
---------- AST ------------
CreateDatabase(
CreateDatabaseStmt {
Expand All @@ -519,7 +519,7 @@ CreateDatabase(
quote: None,
span: Ident(30..31),
},
engine: Default,
engine: None,
options: [],
},
)
Expand All @@ -545,7 +545,9 @@ CreateDatabase(
quote: None,
span: Ident(24..25),
},
engine: Default,
engine: Some(
Default,
),
options: [],
},
)
Expand All @@ -565,8 +567,10 @@ CreateDatabase(
quote: None,
span: Ident(16..17),
},
engine: Github(
"123456",
engine: Some(
Github(
"123456",
),
),
options: [],
},
Expand All @@ -587,12 +591,56 @@ CreateDatabase(
quote: None,
span: Ident(16..17),
},
engine: Default,
engine: Some(
Default,
),
options: [],
},
)


---------- Input ----------
drop database catalog.t;
---------- Output ---------
DROP DATABASE catalog.t
---------- AST ------------
DropDatabase(
DropDatabaseStmt {
if_exists: false,
catalog: Some(
Identifier {
name: "catalog",
quote: None,
span: Ident(14..21),
},
),
database: Identifier {
name: "t",
quote: None,
span: Ident(22..23),
},
},
)


---------- Input ----------
drop database if exists t;
---------- Output ---------
DROP DATABASE IF EXISTS t
---------- AST ------------
DropDatabase(
DropDatabaseStmt {
if_exists: true,
catalog: None,
database: Identifier {
name: "t",
quote: None,
span: Ident(24..25),
},
},
)


---------- Input ----------
create table c(a DateTime null, b DateTime(3));
---------- Output ---------
Expand Down Expand Up @@ -1002,14 +1050,17 @@ DROP database if exists db1;
---------- Output ---------
DROP DATABASE IF EXISTS db1
---------- AST ------------
DropDatabase {
if_exists: true,
database: Identifier {
name: "db1",
quote: None,
span: Ident(24..27),
DropDatabase(
DropDatabaseStmt {
if_exists: true,
catalog: None,
database: Identifier {
name: "db1",
quote: None,
span: Ident(24..27),
},
},
}
)


---------- Input ----------
Expand Down
5 changes: 5 additions & 0 deletions query/src/interpreters/interpreter_factory_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use common_exception::Result;
use super::CreateDatabaseInterpreter;
use super::CreateTableInterpreter;
use super::CreateViewInterpreter;
use super::DropDatabaseInterpreter;
use super::ExplainInterpreterV2;
use super::InterpreterPtr;
use super::SelectInterpreterV2;
Expand Down Expand Up @@ -50,6 +51,7 @@ impl InterpreterFactoryV2 {
| DfStatement::ShowProcessList(_)
| DfStatement::ShowSettings(_)
| DfStatement::CreateDatabase(_)
| DfStatement::DropDatabase(_)
)
}

Expand All @@ -74,6 +76,9 @@ impl InterpreterFactoryV2 {
Plan::CreateDatabase(create_database) => {
CreateDatabaseInterpreter::try_create(ctx, create_database.clone())
}
Plan::DropDatabase(drop_database) => {
DropDatabaseInterpreter::try_create(ctx, drop_database.clone())
}
Plan::ShowMetrics => ShowMetricsInterpreter::try_create(ctx),
Plan::ShowProcessList => ShowProcessListInterpreter::try_create(ctx),
Plan::ShowSettings => ShowSettingsInterpreter::try_create(ctx),
Expand Down
1 change: 1 addition & 0 deletions query/src/sql/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub fn optimize(plan: Plan) -> Result<Plan> {
| Plan::ShowProcessList
| Plan::ShowSettings
| Plan::CreateDatabase(_)
| Plan::DropDatabase(_)
| Plan::CreateTable(_)
| Plan::CreateUser(_)
| Plan::CreateView(_)
Expand Down
27 changes: 26 additions & 1 deletion query/src/sql/planner/binder/ddl/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,38 @@ use std::collections::BTreeMap;

use common_ast::ast::CreateDatabaseStmt;
use common_ast::ast::DatabaseEngine;
use common_ast::ast::DropDatabaseStmt;
use common_exception::Result;
use common_meta_app::schema::DatabaseMeta;
use common_planners::CreateDatabasePlan;
use common_planners::DropDatabasePlan;

use crate::sql::binder::Binder;
use crate::sql::plans::Plan;

impl<'a> Binder {
pub(in crate::sql::planner::binder) async fn bind_drop_database(
&self,
stmt: &DropDatabaseStmt<'a>,
) -> Result<Plan> {
let catalog = stmt
.catalog
.as_ref()
.map(|catalog| catalog.name.clone())
.unwrap_or_else(|| self.ctx.get_current_catalog());

let tenant = self.ctx.get_tenant();
let db = stmt.database.name.clone();
let if_exists = stmt.if_exists;

Ok(Plan::DropDatabase(DropDatabasePlan {
tenant,
catalog,
db,
if_exists,
}))
}

pub(in crate::sql::planner::binder) async fn bind_create_database(
&self,
stmt: &CreateDatabaseStmt<'a>,
Expand Down Expand Up @@ -55,7 +79,8 @@ impl<'a> Binder {
.map(|property| (property.name.clone(), property.value.clone()))
.collect::<BTreeMap<String, String>>();

let (engine, engine_options) = match &stmt.engine {
let database_engine = stmt.engine.as_ref().unwrap_or(&DatabaseEngine::Default);
let (engine, engine_options) = match database_engine {
DatabaseEngine::Github(token) => {
let engine_options =
BTreeMap::from_iter(vec![("token".to_string(), token.clone())]);
Expand Down
4 changes: 4 additions & 0 deletions query/src/sql/planner/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ impl<'a> Binder {
let plan = self.bind_create_database(stmt).await?;
Ok(plan)
}
Statement::DropDatabase(stmt) => {
let plan = self.bind_drop_database(stmt).await?;
Ok(plan)
}

Statement::ShowMetrics => Ok(Plan::ShowMetrics),
Statement::ShowProcessList => Ok(Plan::ShowProcessList),
Expand Down
1 change: 1 addition & 0 deletions query/src/sql/planner/format/display_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl Plan {
}
Plan::CreateTable(create_table) => Ok(format!("{:?}", create_table)),
Plan::CreateDatabase(create_database) => Ok(format!("{:?}", create_database)),
Plan::DropDatabase(drop_database) => Ok(format!("{:?}", drop_database)),
Plan::ShowMetrics => Ok("SHOW METRICS".to_string()),
Plan::ShowProcessList => Ok("SHOW PROCESSLIST".to_string()),
Plan::ShowSettings => Ok("SHOW SETTINGS".to_string()),
Expand Down
2 changes: 2 additions & 0 deletions query/src/sql/planner/plans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use common_planners::CreateDatabasePlan;
use common_planners::CreateTablePlan;
use common_planners::CreateUserPlan;
use common_planners::CreateViewPlan;
use common_planners::DropDatabasePlan;
use common_planners::DropUserPlan;
pub use eval_scalar::EvalScalar;
pub use eval_scalar::ScalarItem;
Expand Down Expand Up @@ -77,6 +78,7 @@ pub enum Plan {
CreateTable(Box<CreateTablePlan>),
CreateDatabase(CreateDatabasePlan),
CreateView(Box<CreateViewPlan>),
DropDatabase(DropDatabasePlan),

// System
ShowMetrics,
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions tests/suites/0_stateless/05_ddl/05_0001_ddl_drop_database_v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set enable_planner_v2 = 1;

DROP DATABASE IF EXISTS db;

CREATE DATABASE db;
DROP DATABASE catalog_not_exist.db; -- {ErrorCode 1006}
DROP DATABASE db;
DROP DATABASE IF EXISTS db;
DROP DATABASE db; -- {ErrorCode 1003}

CREATE DATABASE db;
DROP DATABASE default.db;
DROP DATABASE IF EXISTS db;

DROP SCHEMA IF EXISTS db;
CREATE SCHEMA db;
DROP SCHEMA db;
DROP SCHEMA IF EXISTS db;
DROP SCHEMA db; -- {ErrorCode 1003}

set enable_planner_v2 = 0;