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 view in new planner #5920

Merged
merged 2 commits into from
Jun 13, 2022
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
19 changes: 11 additions & 8 deletions common/ast/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,8 @@ pub enum Statement<'a> {

// Views
CreateView(CreateViewStmt<'a>),
DropView {
if_exists: bool,
catalog: Option<Identifier<'a>>,
database: Option<Identifier<'a>>,
view: Identifier<'a>,
},
AlterView(AlterViewStmt<'a>),
DropView(DropViewStmt<'a>),

// User
CreateUser(CreateUserStmt),
Expand Down Expand Up @@ -361,6 +356,14 @@ pub struct AlterViewStmt<'a> {
pub query: Box<Query<'a>>,
}

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

#[derive(Debug, Clone, PartialEq)]
pub struct SQLProperty {
pub name: String,
Expand Down Expand Up @@ -901,12 +904,12 @@ impl<'a> Display for Statement<'a> {
write_period_separated_list(f, catalog.iter().chain(database).chain(Some(view)))?;
write!(f, " AS {query}")?;
}
Statement::DropView {
Statement::DropView(DropViewStmt {
if_exists,
catalog,
database,
view,
} => {
}) => {
write!(f, "DROP VIEW ")?;
if *if_exists {
write!(f, "IF EXISTS ")?;
Expand Down
12 changes: 7 additions & 5 deletions common/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,13 @@ pub fn statement(i: Input) -> IResult<Statement> {
rule! {
DROP ~ VIEW ~ ( IF ~ EXISTS )? ~ #peroid_separated_idents_1_to_3
},
|(_, _, opt_if_exists, (catalog, database, view))| Statement::DropView {
if_exists: opt_if_exists.is_some(),
catalog,
database,
view,
|(_, _, opt_if_exists, (catalog, database, view))| {
Statement::DropView(DropViewStmt {
if_exists: opt_if_exists.is_some(),
catalog,
database,
view,
})
},
);
let alter_view = map(
Expand Down
1 change: 1 addition & 0 deletions common/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn test_statement() {
r#"create table c(a DateTime null, b DateTime(3));"#,
r#"create view v as select number % 3 as a from numbers(1000);"#,
r#"alter view v as select number % 3 as a from numbers(1000);"#,
r#"drop view v;"#,
r#"rename table d.t to e.s;"#,
r#"truncate table test;"#,
r#"truncate table test_db.test;"#,
Expand Down
19 changes: 19 additions & 0 deletions common/ast/tests/it/testdata/statement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,25 @@ AlterView(
)


---------- Input ----------
drop view v;
---------- Output ---------
DROP VIEW v
---------- AST ------------
DropView(
DropViewStmt {
if_exists: false,
catalog: None,
database: None,
view: Identifier {
name: "v",
quote: None,
span: Ident(10..11),
},
},
)


---------- Input ----------
rename table d.t to e.s;
---------- Output ---------
Expand Down
2 changes: 2 additions & 0 deletions query/src/interpreters/interpreter_factory_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl InterpreterFactoryV2 {
| DfStatement::CreateTable(_)
| DfStatement::CreateView(_)
| DfStatement::AlterView(_)
| DfStatement::DropView(_)
| DfStatement::ShowMetrics(_)
| DfStatement::ShowProcessList(_)
| DfStatement::ShowSettings(_)
Expand Down Expand Up @@ -94,6 +95,7 @@ impl InterpreterFactoryV2 {
Plan::AlterView(alter_view) => {
AlterViewInterpreter::try_create(ctx, *alter_view.clone())
}
Plan::DropView(drop_view) => DropViewInterpreter::try_create(ctx, *drop_view.clone()),

// Users
Plan::CreateUser(create_user) => {
Expand Down
34 changes: 34 additions & 0 deletions query/src/sql/planner/binder/ddl/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

use common_ast::ast::AlterViewStmt;
use common_ast::ast::CreateViewStmt;
use common_ast::ast::DropViewStmt;
use common_exception::Result;
use common_planners::AlterViewPlan;
use common_planners::CreateViewPlan;
use common_planners::DropViewPlan;

use crate::sql::binder::Binder;
use crate::sql::plans::Plan;
Expand Down Expand Up @@ -89,4 +91,36 @@ impl<'a> Binder {
};
Ok(Plan::AlterView(Box::new(plan)))
}

pub(in crate::sql::planner::binder) async fn bind_drop_view(
&mut self,
stmt: &DropViewStmt<'a>,
) -> Result<Plan> {
let DropViewStmt {
if_exists,
catalog,
database,
view,
} = stmt;

let tenant = self.ctx.get_tenant();
let catalog = catalog
.as_ref()
.map(|ident| ident.name.to_lowercase())
.unwrap_or_else(|| self.ctx.get_current_catalog());
let database = database
.as_ref()
.map(|ident| ident.name.to_lowercase())
.unwrap_or_else(|| self.ctx.get_current_database());
let viewname = view.name.to_lowercase();

let plan = DropViewPlan {
if_exists: *if_exists,
tenant,
catalog,
database,
viewname,
};
Ok(Plan::DropView(Box::new(plan)))
}
}
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 @@ -141,6 +141,10 @@ impl<'a> Binder {
let plan = self.bind_alter_view(stmt).await?;
Ok(plan)
}
Statement::DropView(stmt) => {
let plan = self.bind_drop_view(stmt).await?;
Ok(plan)
}

// Users
Statement::CreateUser(stmt) => {
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 @@ -42,6 +42,7 @@ impl Plan {
// Views
Plan::CreateView(create_view) => Ok(format!("{:?}", create_view)),
Plan::AlterView(alter_view) => Ok(format!("{:?}", alter_view)),
Plan::DropView(drop_view) => Ok(format!("{:?}", drop_view)),

// Users
Plan::CreateUser(create_user) => Ok(format!("{:?}", create_user)),
Expand Down
1 change: 1 addition & 0 deletions query/src/sql/planner/plans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub enum Plan {
// Views
CreateView(Box<CreateViewPlan>),
AlterView(Box<AlterViewPlan>),
DropView(Box<DropViewPlan>),

// DCL
AlterUser(Box<AlterUserPlan>),
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions tests/suites/0_stateless/05_ddl/05_0020_ddl_drop_view_v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set enable_planner_v2 = 1;

DROP VIEW IF EXISTS tmp_view;
CREATE VIEW tmp_view AS SELECT number % 3 AS a, avg(number) FROM numbers(1000) GROUP BY a ORDER BY a;
DROP TABLE tmp_view; -- {ErrorCode 1054}
DROP VIEW tmp_view;
SELECT * FROM tmp_view; -- {ErrorCode 1025}