Skip to content

Commit 77a6bf0

Browse files
authored
Merge pull request #98 from supabase/feat/combining-queries
feat: add support for combining queries
2 parents d7df5d5 + f1b2356 commit 77a6bf0

File tree

7 files changed

+776
-198
lines changed

7 files changed

+776
-198
lines changed

Diff for: crates/codegen/src/get_node_properties.rs

+17
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ fn custom_handlers(node: &Node) -> TokenStream {
181181
tokens.push(TokenProperty::from(Token::GroupP));
182182
tokens.push(TokenProperty::from(Token::By));
183183
}
184+
match n.op() {
185+
protobuf::SetOperation::Undefined => {},
186+
protobuf::SetOperation::SetopNone => {},
187+
protobuf::SetOperation::SetopUnion => tokens.push(TokenProperty::from(Token::Union)),
188+
protobuf::SetOperation::SetopIntersect => tokens.push(TokenProperty::from(Token::Intersect)),
189+
protobuf::SetOperation::SetopExcept => tokens.push(TokenProperty::from(Token::Except)),
190+
_ => panic!("Unknown SelectStmt op {:#?}", n.op()),
191+
}
192+
if n.all {
193+
tokens.push(TokenProperty::from(Token::All));
194+
}
184195
},
185196
"BoolExpr" => quote! {
186197
match n.boolop() {
@@ -391,6 +402,12 @@ fn custom_handlers(node: &Node) -> TokenStream {
391402
tokens.push(TokenProperty::from(Token::View));
392403
if n.query.is_some() {
393404
tokens.push(TokenProperty::from(Token::As));
405+
// check if SelectStmt with WithClause with recursive set to true
406+
if let Some(NodeEnum::SelectStmt(select_stmt)) = n.query.as_ref().and_then(|query| query.node.as_ref()) {
407+
if select_stmt.with_clause.is_some() && select_stmt.with_clause.as_ref().unwrap().recursive {
408+
tokens.push(TokenProperty::from(Token::Recursive));
409+
}
410+
}
394411
}
395412
if n.replace {
396413
tokens.push(TokenProperty::from(Token::Or));

Diff for: crates/parser/src/parse/statement.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ fn collect_statement_token_range(parser: &mut Parser, kind: SyntaxKind) -> Range
7474
let mut ignore_next_non_whitespace = false;
7575
while !parser.at(SyntaxKind::Ascii59) && !parser.eof() {
7676
match parser.nth(0, false).kind {
77+
SyntaxKind::All => {
78+
// ALL is never a statement start, but needs to be skipped when combining queries
79+
// (e.g. UNION ALL)
80+
parser.advance();
81+
}
7782
SyntaxKind::BeginP => {
7883
// BEGIN, consume until END
7984
is_sub_trx += 1;
@@ -92,7 +97,7 @@ fn collect_statement_token_range(parser: &mut Parser, kind: SyntaxKind) -> Range
9297
is_sub_stmt -= 1;
9398
parser.advance();
9499
}
95-
SyntaxKind::As => {
100+
SyntaxKind::As | SyntaxKind::Union | SyntaxKind::Intersect | SyntaxKind::Except => {
96101
// ignore the next non-whitespace token
97102
ignore_next_non_whitespace = true;
98103
parser.advance();

Diff for: crates/parser/tests/data/statements/valid/0044.sql

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ CREATE VIEW comedies AS SELECT * FROM films WHERE kind = 'Comedy';
22
CREATE VIEW universal_comedies AS SELECT * FROM comedies WHERE classification = 'U' WITH LOCAL CHECK OPTION;
33
CREATE VIEW pg_comedies AS SELECT * FROM comedies WHERE classification = 'PG' WITH CASCADED CHECK OPTION;
44
CREATE VIEW comedies AS SELECT f.*, country_code_to_name(f.country_code) AS country, (SELECT avg(r.rating) FROM user_ratings r WHERE r.film_id = f.id) AS avg_rating FROM films f WHERE f.kind = 'Comedy';
5-
CREATE RECURSIVE VIEW public.nums_1_100 (n) AS VALUES (1) UNION ALL SELECT n+1 FROM nums_1_100 WHERE n < 100;

Diff for: crates/parser/tests/data/statements/valid/0055.sql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
select 1 union all select 2;
2+
select 1 union select 2;

0 commit comments

Comments
 (0)