Skip to content

Commit 9d750a1

Browse files
authored
feat(query): support Support from <table> to simplify select * from <table> (#17372)
1 parent 87a280a commit 9d750a1

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed

src/query/ast/src/parser/query.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::parser::statement::set_table_option;
3737
use crate::parser::statement::top_n;
3838
use crate::parser::token::*;
3939
use crate::parser::ErrorKind;
40+
use crate::Range;
4041

4142
pub fn query(i: Input) -> IResult<Query> {
4243
context(
@@ -103,6 +104,34 @@ pub fn set_operation_element(i: Input) -> IResult<WithSpan<SetOperationElement>>
103104
}
104105
},
105106
);
107+
let from_stmt = map_res(
108+
rule! {
109+
FROM ~ ^#comma_separated_list1(table_reference)
110+
},
111+
|(_from, from_block)| {
112+
if from_block.len() != 1 {
113+
return Err(nom::Err::Failure(ErrorKind::Other(
114+
"FROM query only support query one table",
115+
)));
116+
}
117+
Ok(SetOperationElement::SelectStmt {
118+
hints: None,
119+
distinct: false,
120+
top_n: None,
121+
select_list: vec![SelectTarget::StarColumns {
122+
qualified: vec![Indirection::Star(Some(Range { start: 0, end: 0 }))],
123+
column_filter: None,
124+
}],
125+
from: from_block,
126+
selection: None,
127+
group_by: None,
128+
having: None,
129+
window_list: None,
130+
qualify: None,
131+
})
132+
},
133+
);
134+
106135
let select_stmt = map_res(
107136
rule! {
108137
( FROM ~ ^#comma_separated_list1(table_reference) )?
@@ -195,6 +224,7 @@ pub fn set_operation_element(i: Input) -> IResult<WithSpan<SetOperationElement>>
195224
| #with
196225
| #set_operator
197226
| #select_stmt
227+
| #from_stmt
198228
| #values
199229
| #order_by
200230
| #limit

src/query/ast/tests/it/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ fn test_statement() {
191191
r#"select * from t4;"#,
192192
r#"select top 2 * from t4;"#,
193193
r#"select * from aa.bb;"#,
194+
r#"from aa.bb select *;"#,
195+
r#"from aa.bb"#,
194196
r#"select * from a, b, c;"#,
195197
r#"select * from a, b, c order by "db"."a"."c1";"#,
196198
r#"select * from a join b on a.a = b.a;"#,

src/query/ast/tests/it/testdata/stmt.txt

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5766,6 +5766,162 @@ Query(
57665766
)
57675767

57685768

5769+
---------- Input ----------
5770+
from aa.bb select *;
5771+
---------- Output ---------
5772+
SELECT * FROM aa.bb
5773+
---------- AST ------------
5774+
Query(
5775+
Query {
5776+
span: Some(
5777+
0..19,
5778+
),
5779+
with: None,
5780+
body: Select(
5781+
SelectStmt {
5782+
span: Some(
5783+
0..19,
5784+
),
5785+
hints: None,
5786+
distinct: false,
5787+
top_n: None,
5788+
select_list: [
5789+
StarColumns {
5790+
qualified: [
5791+
Star(
5792+
Some(
5793+
18..19,
5794+
),
5795+
),
5796+
],
5797+
column_filter: None,
5798+
},
5799+
],
5800+
from: [
5801+
Table {
5802+
span: Some(
5803+
5..10,
5804+
),
5805+
catalog: None,
5806+
database: Some(
5807+
Identifier {
5808+
span: Some(
5809+
5..7,
5810+
),
5811+
name: "aa",
5812+
quote: None,
5813+
ident_type: None,
5814+
},
5815+
),
5816+
table: Identifier {
5817+
span: Some(
5818+
8..10,
5819+
),
5820+
name: "bb",
5821+
quote: None,
5822+
ident_type: None,
5823+
},
5824+
alias: None,
5825+
temporal: None,
5826+
with_options: None,
5827+
pivot: None,
5828+
unpivot: None,
5829+
sample: None,
5830+
},
5831+
],
5832+
selection: None,
5833+
group_by: None,
5834+
having: None,
5835+
window_list: None,
5836+
qualify: None,
5837+
},
5838+
),
5839+
order_by: [],
5840+
limit: [],
5841+
offset: None,
5842+
ignore_result: false,
5843+
},
5844+
)
5845+
5846+
5847+
---------- Input ----------
5848+
from aa.bb
5849+
---------- Output ---------
5850+
SELECT * FROM aa.bb
5851+
---------- AST ------------
5852+
Query(
5853+
Query {
5854+
span: Some(
5855+
0..10,
5856+
),
5857+
with: None,
5858+
body: Select(
5859+
SelectStmt {
5860+
span: Some(
5861+
0..10,
5862+
),
5863+
hints: None,
5864+
distinct: false,
5865+
top_n: None,
5866+
select_list: [
5867+
StarColumns {
5868+
qualified: [
5869+
Star(
5870+
Some(
5871+
0..0,
5872+
),
5873+
),
5874+
],
5875+
column_filter: None,
5876+
},
5877+
],
5878+
from: [
5879+
Table {
5880+
span: Some(
5881+
5..10,
5882+
),
5883+
catalog: None,
5884+
database: Some(
5885+
Identifier {
5886+
span: Some(
5887+
5..7,
5888+
),
5889+
name: "aa",
5890+
quote: None,
5891+
ident_type: None,
5892+
},
5893+
),
5894+
table: Identifier {
5895+
span: Some(
5896+
8..10,
5897+
),
5898+
name: "bb",
5899+
quote: None,
5900+
ident_type: None,
5901+
},
5902+
alias: None,
5903+
temporal: None,
5904+
with_options: None,
5905+
pivot: None,
5906+
unpivot: None,
5907+
sample: None,
5908+
},
5909+
],
5910+
selection: None,
5911+
group_by: None,
5912+
having: None,
5913+
window_list: None,
5914+
qualify: None,
5915+
},
5916+
),
5917+
order_by: [],
5918+
limit: [],
5919+
offset: None,
5920+
ignore_result: false,
5921+
},
5922+
)
5923+
5924+
57695925
---------- Input ----------
57705926
select * from a, b, c;
57715927
---------- Output ---------

tests/sqllogictests/suites/query/select.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ create table db.t(a int)
1717
statement ok
1818
insert into db.t values(1),(2)
1919

20+
onlyif http
21+
statement error 1005
22+
from db.t, db.t2;
23+
24+
onlyif http
25+
query I
26+
from db.t
27+
----
28+
1
29+
2
30+
2031
onlyif http
2132
query I
2233
select db.t.a from db.t

0 commit comments

Comments
 (0)