From e5221a0ab8a8427cb407b250d89f7d7e716b85aa Mon Sep 17 00:00:00 2001 From: TCeason Date: Sun, 26 Jan 2025 12:18:51 +0800 Subject: [PATCH] feat(query): support Support from to simplify select * from
--- src/query/ast/src/parser/query.rs | 30 ++++ src/query/ast/tests/it/parser.rs | 2 + src/query/ast/tests/it/testdata/stmt.txt | 156 +++++++++++++++++++ tests/sqllogictests/suites/query/select.test | 11 ++ 4 files changed, 199 insertions(+) diff --git a/src/query/ast/src/parser/query.rs b/src/query/ast/src/parser/query.rs index 8984a5ca3c9fe..58cee66a97b79 100644 --- a/src/query/ast/src/parser/query.rs +++ b/src/query/ast/src/parser/query.rs @@ -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 { context( @@ -103,6 +104,34 @@ pub fn set_operation_element(i: Input) -> IResult> } }, ); + 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) )? @@ -195,6 +224,7 @@ pub fn set_operation_element(i: Input) -> IResult> | #with | #set_operator | #select_stmt + | #from_stmt | #values | #order_by | #limit diff --git a/src/query/ast/tests/it/parser.rs b/src/query/ast/tests/it/parser.rs index 052648e507d76..d5127655652d8 100644 --- a/src/query/ast/tests/it/parser.rs +++ b/src/query/ast/tests/it/parser.rs @@ -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;"#, diff --git a/src/query/ast/tests/it/testdata/stmt.txt b/src/query/ast/tests/it/testdata/stmt.txt index 365e93019f253..4f8cfea435e4d 100644 --- a/src/query/ast/tests/it/testdata/stmt.txt +++ b/src/query/ast/tests/it/testdata/stmt.txt @@ -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 --------- diff --git a/tests/sqllogictests/suites/query/select.test b/tests/sqllogictests/suites/query/select.test index 85fc47a0bba28..57c4c2e06de60 100644 --- a/tests/sqllogictests/suites/query/select.test +++ b/tests/sqllogictests/suites/query/select.test @@ -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