Skip to content

Commit

Permalink
feat(query): support Support from <table> to simplify select * from <…
Browse files Browse the repository at this point in the history
…table> (#17372)
  • Loading branch information
TCeason authored Jan 26, 2025
1 parent 87a280a commit 9d750a1
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/query/ast/src/parser/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::parser::statement::set_table_option;
use crate::parser::statement::top_n;
use crate::parser::token::*;
use crate::parser::ErrorKind;
use crate::Range;

pub fn query(i: Input) -> IResult<Query> {
context(
Expand Down Expand Up @@ -103,6 +104,34 @@ pub fn set_operation_element(i: Input) -> IResult<WithSpan<SetOperationElement>>
}
},
);
let from_stmt = map_res(
rule! {
FROM ~ ^#comma_separated_list1(table_reference)
},
|(_from, from_block)| {
if from_block.len() != 1 {
return Err(nom::Err::Failure(ErrorKind::Other(
"FROM query only support query one table",
)));
}
Ok(SetOperationElement::SelectStmt {
hints: None,
distinct: false,
top_n: None,
select_list: vec![SelectTarget::StarColumns {
qualified: vec![Indirection::Star(Some(Range { start: 0, end: 0 }))],
column_filter: None,
}],
from: from_block,
selection: None,
group_by: None,
having: None,
window_list: None,
qualify: None,
})
},
);

let select_stmt = map_res(
rule! {
( FROM ~ ^#comma_separated_list1(table_reference) )?
Expand Down Expand Up @@ -195,6 +224,7 @@ pub fn set_operation_element(i: Input) -> IResult<WithSpan<SetOperationElement>>
| #with
| #set_operator
| #select_stmt
| #from_stmt
| #values
| #order_by
| #limit
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ fn test_statement() {
r#"select * from t4;"#,
r#"select top 2 * from t4;"#,
r#"select * from aa.bb;"#,
r#"from aa.bb select *;"#,
r#"from aa.bb"#,
r#"select * from a, b, c;"#,
r#"select * from a, b, c order by "db"."a"."c1";"#,
r#"select * from a join b on a.a = b.a;"#,
Expand Down
156 changes: 156 additions & 0 deletions src/query/ast/tests/it/testdata/stmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5766,6 +5766,162 @@ Query(
)


---------- Input ----------
from aa.bb select *;
---------- Output ---------
SELECT * FROM aa.bb
---------- AST ------------
Query(
Query {
span: Some(
0..19,
),
with: None,
body: Select(
SelectStmt {
span: Some(
0..19,
),
hints: None,
distinct: false,
top_n: None,
select_list: [
StarColumns {
qualified: [
Star(
Some(
18..19,
),
),
],
column_filter: None,
},
],
from: [
Table {
span: Some(
5..10,
),
catalog: None,
database: Some(
Identifier {
span: Some(
5..7,
),
name: "aa",
quote: None,
ident_type: None,
},
),
table: Identifier {
span: Some(
8..10,
),
name: "bb",
quote: None,
ident_type: None,
},
alias: None,
temporal: None,
with_options: None,
pivot: None,
unpivot: None,
sample: None,
},
],
selection: None,
group_by: None,
having: None,
window_list: None,
qualify: None,
},
),
order_by: [],
limit: [],
offset: None,
ignore_result: false,
},
)


---------- Input ----------
from aa.bb
---------- Output ---------
SELECT * FROM aa.bb
---------- AST ------------
Query(
Query {
span: Some(
0..10,
),
with: None,
body: Select(
SelectStmt {
span: Some(
0..10,
),
hints: None,
distinct: false,
top_n: None,
select_list: [
StarColumns {
qualified: [
Star(
Some(
0..0,
),
),
],
column_filter: None,
},
],
from: [
Table {
span: Some(
5..10,
),
catalog: None,
database: Some(
Identifier {
span: Some(
5..7,
),
name: "aa",
quote: None,
ident_type: None,
},
),
table: Identifier {
span: Some(
8..10,
),
name: "bb",
quote: None,
ident_type: None,
},
alias: None,
temporal: None,
with_options: None,
pivot: None,
unpivot: None,
sample: None,
},
],
selection: None,
group_by: None,
having: None,
window_list: None,
qualify: None,
},
),
order_by: [],
limit: [],
offset: None,
ignore_result: false,
},
)


---------- Input ----------
select * from a, b, c;
---------- Output ---------
Expand Down
11 changes: 11 additions & 0 deletions tests/sqllogictests/suites/query/select.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ create table db.t(a int)
statement ok
insert into db.t values(1),(2)

onlyif http
statement error 1005
from db.t, db.t2;

onlyif http
query I
from db.t
----
1
2

onlyif http
query I
select db.t.a from db.t
Expand Down

0 comments on commit 9d750a1

Please sign in to comment.