Skip to content

Commit bb82a7f

Browse files
authored
Merge pull request #6987 from lichuang/share_sql
feat: add Drop Share sql support
2 parents 7d39378 + a2daa3e commit bb82a7f

File tree

15 files changed

+183
-4
lines changed

15 files changed

+183
-4
lines changed

common/ast/src/ast/statements/share.rs

+18
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,21 @@ impl Display for CreateShareStmt<'_> {
3737
Ok(())
3838
}
3939
}
40+
41+
#[derive(Debug, Clone, PartialEq, Eq)]
42+
pub struct DropShareStmt<'a> {
43+
pub if_exists: bool,
44+
pub share: Identifier<'a>,
45+
}
46+
47+
impl Display for DropShareStmt<'_> {
48+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49+
write!(f, "DROP SHARE ")?;
50+
if self.if_exists {
51+
write!(f, "IF EXISTS ")?;
52+
}
53+
write!(f, "{}", self.share)?;
54+
55+
Ok(())
56+
}
57+
}

common/ast/src/ast/statements/statement.rs

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ pub enum Statement<'a> {
160160

161161
// share
162162
CreateShare(CreateShareStmt<'a>),
163+
DropShare(DropShareStmt<'a>),
163164
}
164165

165166
#[derive(Debug, Clone, PartialEq)]
@@ -362,6 +363,7 @@ impl<'a> Display for Statement<'a> {
362363
Statement::Call(stmt) => write!(f, "{stmt}")?,
363364
Statement::Presign(stmt) => write!(f, "{stmt}")?,
364365
Statement::CreateShare(stmt) => write!(f, "{stmt}")?,
366+
Statement::DropShare(stmt) => write!(f, "{stmt}")?,
365367
}
366368
Ok(())
367369
}

common/ast/src/parser/statement.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,17 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
757757
})
758758
},
759759
);
760+
let drop_share = map(
761+
rule! {
762+
DROP ~ SHARE ~ (IF ~ EXISTS )? ~ #ident
763+
},
764+
|(_, _, opt_if_exists, share)| {
765+
Statement::DropShare(DropShareStmt {
766+
if_exists: opt_if_exists.is_some(),
767+
share,
768+
})
769+
},
770+
);
760771

761772
let statement_body = alt((
762773
rule!(
@@ -845,7 +856,8 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
845856
),
846857
// share
847858
rule!(
848-
#create_share: "`CREATE SHARE <share_name> [ COMMENT = '<string_literal>' ]`"
859+
#create_share: "`CREATE SHARE [IF NOT EXISTS] <share_name> [ COMMENT = '<string_literal>' ]`"
860+
| #drop_share: "`DROP SHARE [IF EXISTS] <share_name>`"
849861
),
850862
));
851863

common/ast/src/parser/token.rs

+1
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ impl TokenKind {
853853
| TokenKind::SELECT
854854
// | TokenKind::SESSION_USER
855855
// | TokenKind::SETOF
856+
| TokenKind::SHARE
856857
| TokenKind::SMALLINT
857858
| TokenKind::SOME
858859
| TokenKind::SUBSTRING

common/ast/tests/it/parser.rs

+2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ fn test_statement() {
258258
r#"PRESIGN UPLOAD @my_stage/path/to/file EXPIRE=7200"#,
259259
r#"CREATE SHARE t COMMENT='share comment';"#,
260260
r#"CREATE SHARE IF NOT EXISTS t;"#,
261+
r#"DROP SHARE a;"#,
262+
r#"DROP SHARE IF EXISTS a;"#,
261263
];
262264

263265
for case in cases {

common/ast/tests/it/testdata/statement-error.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ error:
9090
--> SQL:1:6
9191
|
9292
1 | drop a
93-
| ^ expected `DATABASE`, `SCHEMA`, `TABLE`, `VIEW`, `USER`, `ROLE`, or 2 more ...
93+
| ^ expected `DATABASE`, `SCHEMA`, `TABLE`, `VIEW`, `USER`, `ROLE`, or 3 more ...
9494

9595

9696
---------- Input ----------
@@ -144,7 +144,7 @@ error:
144144
--> SQL:1:6
145145
|
146146
1 | drop usar if exists 'test-j'@'localhost';
147-
| ^^^^ expected `DATABASE`, `SCHEMA`, `TABLE`, `VIEW`, `USER`, `ROLE`, or 2 more ...
147+
| ^^^^ expected `DATABASE`, `SCHEMA`, `TABLE`, `VIEW`, `USER`, `ROLE`, or 3 more ...
148148

149149

150150
---------- Input ----------

common/ast/tests/it/testdata/statement.txt

+34
Original file line numberDiff line numberDiff line change
@@ -5945,3 +5945,37 @@ CreateShare(
59455945
)
59465946

59475947

5948+
---------- Input ----------
5949+
DROP SHARE a;
5950+
---------- Output ---------
5951+
DROP SHARE a
5952+
---------- AST ------------
5953+
DropShare(
5954+
DropShareStmt {
5955+
if_exists: false,
5956+
share: Identifier {
5957+
name: "a",
5958+
quote: None,
5959+
span: Ident(11..12),
5960+
},
5961+
},
5962+
)
5963+
5964+
5965+
---------- Input ----------
5966+
DROP SHARE IF EXISTS a;
5967+
---------- Output ---------
5968+
DROP SHARE IF EXISTS a
5969+
---------- AST ------------
5970+
DropShare(
5971+
DropShareStmt {
5972+
if_exists: true,
5973+
share: Identifier {
5974+
name: "a",
5975+
quote: None,
5976+
span: Ident(21..22),
5977+
},
5978+
},
5979+
)
5980+
5981+

query/src/interpreters/interpreter_factory_v2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::interpreters::interpreter_presign::PresignInterpreter;
2626
use crate::interpreters::interpreter_table_create_v2::CreateTableInterpreterV2;
2727
use crate::interpreters::AlterUserInterpreter;
2828
use crate::interpreters::CreateShareInterpreter;
29+
use crate::interpreters::DropShareInterpreter;
2930
use crate::interpreters::DropUserInterpreter;
3031
use crate::sessions::QueryContext;
3132
use crate::sql::plans::Plan;
@@ -257,6 +258,7 @@ impl InterpreterFactoryV2 {
257258
ctx,
258259
*p.clone(),
259260
)?)),
261+
Plan::DropShare(p) => Ok(Arc::new(DropShareInterpreter::try_create(ctx, *p.clone())?)),
260262
}
261263
}
262264
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::sync::Arc;
16+
17+
use common_exception::Result;
18+
use common_meta_api::ShareApi;
19+
use common_streams::DataBlockStream;
20+
use common_streams::SendableDataBlockStream;
21+
22+
use crate::interpreters::Interpreter;
23+
use crate::sessions::QueryContext;
24+
use crate::sessions::TableContext;
25+
use crate::sql::plans::share::DropSharePlan;
26+
27+
pub struct DropShareInterpreter {
28+
ctx: Arc<QueryContext>,
29+
plan: DropSharePlan,
30+
}
31+
32+
impl DropShareInterpreter {
33+
pub fn try_create(ctx: Arc<QueryContext>, plan: DropSharePlan) -> Result<Self> {
34+
Ok(DropShareInterpreter { ctx, plan })
35+
}
36+
}
37+
38+
#[async_trait::async_trait]
39+
impl Interpreter for DropShareInterpreter {
40+
fn name(&self) -> &str {
41+
"DropShareInterpreter"
42+
}
43+
44+
async fn execute(&self) -> Result<SendableDataBlockStream> {
45+
let user_mgr = self.ctx.get_user_manager();
46+
let meta_api = user_mgr.get_meta_store_client();
47+
meta_api.drop_share(self.plan.clone().into()).await?;
48+
49+
Ok(Box::pin(DataBlockStream::create(
50+
self.plan.schema(),
51+
None,
52+
vec![],
53+
)))
54+
}
55+
}

query/src/interpreters/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ mod interpreter_select;
5050
mod interpreter_select_v2;
5151
mod interpreter_setting;
5252
mod interpreter_share_create;
53+
mod interpreter_share_drop;
5354
mod interpreter_show_databases;
5455
mod interpreter_show_functions;
5556
mod interpreter_show_grants;
@@ -131,6 +132,7 @@ pub use interpreter_select::SelectInterpreter;
131132
pub use interpreter_select_v2::SelectInterpreterV2;
132133
pub use interpreter_setting::SettingInterpreter;
133134
pub use interpreter_share_create::CreateShareInterpreter;
135+
pub use interpreter_share_drop::DropShareInterpreter;
134136
pub use interpreter_show_databases::ShowDatabasesInterpreter;
135137
pub use interpreter_show_functions::ShowFunctionsInterpreter;
136138
pub use interpreter_show_grants::ShowGrantsInterpreter;

query/src/sql/planner/binder/ddl/share.rs

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use common_exception::Result;
1818
use crate::sessions::TableContext;
1919
use crate::sql::binder::Binder;
2020
use crate::sql::plans::CreateSharePlan;
21+
use crate::sql::plans::DropSharePlan;
2122
use crate::sql::plans::Plan;
2223

2324
impl<'a> Binder {
@@ -41,4 +42,20 @@ impl<'a> Binder {
4142
};
4243
Ok(Plan::CreateShare(Box::new(plan)))
4344
}
45+
46+
pub(in crate::sql::planner::binder) async fn bind_drop_share(
47+
&mut self,
48+
stmt: &DropShareStmt<'a>,
49+
) -> Result<Plan> {
50+
let DropShareStmt { if_exists, share } = stmt;
51+
52+
let share = share.name.to_lowercase();
53+
54+
let plan = DropSharePlan {
55+
if_exists: *if_exists,
56+
tenant: self.ctx.get_tenant(),
57+
share,
58+
};
59+
Ok(Plan::DropShare(Box::new(plan)))
60+
}
4461
}

query/src/sql/planner/binder/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ impl<'a> Binder {
297297
Statement::CreateShare(stmt) => {
298298
self.bind_create_share(stmt).await?
299299
}
300-
300+
Statement::DropShare(stmt) => {
301+
self.bind_drop_share(stmt).await?
302+
}
301303
};
302304
Ok(plan)
303305
}

query/src/sql/planner/format/display_plan.rs

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl Plan {
9595
Plan::Kill(p) => Ok(format!("{:?}", p)),
9696

9797
Plan::CreateShare(p) => Ok(format!("{:?}", p)),
98+
Plan::DropShare(p) => Ok(format!("{:?}", p)),
9899
}
99100
}
100101
}

query/src/sql/planner/plans/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub enum Plan {
200200

201201
// Share
202202
CreateShare(Box<CreateSharePlan>),
203+
DropShare(Box<DropSharePlan>),
203204
}
204205

205206
#[derive(Clone)]
@@ -271,6 +272,7 @@ impl Display for Plan {
271272
Plan::SetVariable(_) => write!(f, "SetVariable"),
272273
Plan::Kill(_) => write!(f, "Kill"),
273274
Plan::CreateShare(_) => write!(f, "CreateShare"),
275+
Plan::DropShare(_) => write!(f, "DropShare"),
274276
}
275277
}
276278
}
@@ -334,6 +336,7 @@ impl Plan {
334336
Plan::SetVariable(plan) => plan.schema(),
335337
Plan::Kill(_) => Arc::new(DataSchema::empty()),
336338
Plan::CreateShare(plan) => plan.schema(),
339+
Plan::DropShare(plan) => plan.schema(),
337340
}
338341
}
339342
}

query/src/sql/planner/plans/share.rs

+28
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ use common_datavalues::chrono::Utc;
1818
use common_datavalues::DataSchema;
1919
use common_datavalues::DataSchemaRef;
2020
use common_meta_app::share::CreateShareReq;
21+
use common_meta_app::share::DropShareReq;
2122
use common_meta_app::share::ShareNameIdent;
2223

24+
// Create Share Plan
2325
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
2426
pub struct CreateSharePlan {
2527
pub if_not_exists: bool,
@@ -47,3 +49,29 @@ impl CreateSharePlan {
4749
Arc::new(DataSchema::empty())
4850
}
4951
}
52+
53+
// Drop Share Plan
54+
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
55+
pub struct DropSharePlan {
56+
pub if_exists: bool,
57+
pub tenant: String,
58+
pub share: String,
59+
}
60+
61+
impl From<DropSharePlan> for DropShareReq {
62+
fn from(p: DropSharePlan) -> Self {
63+
DropShareReq {
64+
if_exists: p.if_exists,
65+
share_name: ShareNameIdent {
66+
tenant: p.tenant,
67+
share_name: p.share,
68+
},
69+
}
70+
}
71+
}
72+
73+
impl DropSharePlan {
74+
pub fn schema(&self) -> DataSchemaRef {
75+
Arc::new(DataSchema::empty())
76+
}
77+
}

0 commit comments

Comments
 (0)