Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions crates/squawk_parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2952,7 +2952,7 @@ const JOIN_TYPE_FIRST: TokenSet =
// LEFT [ OUTER ] JOIN
// RIGHT [ OUTER ] JOIN
// FULL [ OUTER ] JOIN
fn join_type(p: &mut Parser<'_>) {
fn join_type(p: &mut Parser<'_>) -> Option<CompletedMarker> {
assert!(p.at_ts(JOIN_TYPE_FIRST));
let m = p.start();
let kind = match p.current() {
Expand Down Expand Up @@ -2987,10 +2987,10 @@ fn join_type(p: &mut Parser<'_>) {
_ => {
p.error("expected join type");
m.abandon(p);
return;
return None;
}
};
m.complete(p, kind);
Some(m.complete(p, kind))
}

const JOIN_FIRST: TokenSet = TokenSet::new(&[NATURAL_KW, CROSS_KW]).union(JOIN_TYPE_FIRST);
Expand Down Expand Up @@ -3379,10 +3379,10 @@ fn merge_using_clause(p: &mut Parser<'_>) {
let m = p.start();
p.expect(USING_KW);
opt_from_item(p);
p.expect(ON_KW);
// join_condition
if expr(p).is_none() {
p.error("expected an expression");
if p.at(ON_KW) {
on_clause(p);
} else {
p.error("expected on clause");
}
m.complete(p, USING_ON_CLAUSE);
}
Expand Down Expand Up @@ -3451,14 +3451,14 @@ fn opt_from_item(p: &mut Parser<'_>) -> bool {
fn join(p: &mut Parser<'_>) {
assert!(p.at_ts(JOIN_FIRST));
let m = p.start();
p.eat(NATURAL_KW);
join_type(p);
let is_natural = p.eat(NATURAL_KW);
let result = join_type(p);
let join_kind = result.map(|x| x.kind()).unwrap_or(JOIN_INNER);
if !opt_from_item(p) {
p.error("expected from_item");
}
// need to check that we're not actually at the on_conflict clause in an
// insert/update statement
if p.at(ON_KW) && !(p.nth_at(1, CONFLICT_KW) && matches!(p.nth(2), DO_KW | ON_KW | L_PAREN)) {
let can_have_on_clause = !is_natural && join_kind != JOIN_CROSS;
if p.at(ON_KW) && can_have_on_clause {
on_clause(p);
} else if p.at(USING_KW) {
join_using_clause(p);
Expand All @@ -3467,6 +3467,7 @@ fn join(p: &mut Parser<'_>) {
}

fn on_clause(p: &mut Parser<'_>) {
assert!(p.at(ON_KW));
let m = p.start();
p.bump(ON_KW);
if expr(p).is_none() {
Expand Down
14 changes: 14 additions & 0 deletions crates/squawk_parser/tests/data/ok/merge_pg17.sql
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,17 @@ WHEN MATCHED AND w.stock != s.stock THEN
UPDATE SET stock = s.stock
WHEN NOT MATCHED BY SOURCE THEN
DELETE;

-- cross_join_data_source
merge into t
using u cross join v
on t.id = u.id
when matched then
do nothing;

-- natural join
merge into t
using u natural join v
on t.id = u.id
when matched then
do nothing;
35 changes: 18 additions & 17 deletions crates/squawk_parser/tests/snapshots/tests__explain_ok.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1061,24 +1061,25 @@ SOURCE_FILE
NAME_REF
IDENT "u"
WHITESPACE "\n "
ON_KW "on"
WHITESPACE " "
BIN_EXPR
FIELD_EXPR
NAME_REF
IDENT "t"
DOT "."
NAME_REF
IDENT "id"
WHITESPACE " "
EQ "="
ON_CLAUSE
ON_KW "on"
WHITESPACE " "
FIELD_EXPR
NAME_REF
IDENT "u"
DOT "."
NAME_REF
IDENT "id"
BIN_EXPR
FIELD_EXPR
NAME_REF
IDENT "t"
DOT "."
NAME_REF
IDENT "id"
WHITESPACE " "
EQ "="
WHITESPACE " "
FIELD_EXPR
NAME_REF
IDENT "u"
DOT "."
NAME_REF
IDENT "id"
WHITESPACE "\n "
MERGE_WHEN_MATCHED
WHEN_KW "when"
Expand Down
Loading
Loading