Skip to content

Commit

Permalink
Add support for the Snowflake MINUS set operator (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavcloud authored Jan 9, 2025
1 parent 4fdf5e1 commit 5a761dd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub enum SetOperator {
Union,
Except,
Intersect,
Minus,
}

impl fmt::Display for SetOperator {
Expand All @@ -216,6 +217,7 @@ impl fmt::Display for SetOperator {
SetOperator::Union => "UNION",
SetOperator::Except => "EXCEPT",
SetOperator::Intersect => "INTERSECT",
SetOperator::Minus => "MINUS",
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ define_keywords!(
MILLISECOND,
MILLISECONDS,
MIN,
MINUS,
MINUTE,
MINUTES,
MINVALUE,
Expand Down Expand Up @@ -921,6 +922,7 @@ pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
Keyword::UNION,
Keyword::EXCEPT,
Keyword::INTERSECT,
Keyword::MINUS,
// Reserved only as a table alias in the `FROM`/`JOIN` clauses:
Keyword::ON,
Keyword::JOIN,
Expand Down Expand Up @@ -984,6 +986,7 @@ pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
Keyword::UNION,
Keyword::EXCEPT,
Keyword::INTERSECT,
Keyword::MINUS,
Keyword::CLUSTER,
Keyword::DISTRIBUTE,
Keyword::RETURNING,
Expand Down
12 changes: 10 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9942,7 +9942,9 @@ impl<'a> Parser<'a> {
let op = self.parse_set_operator(&self.peek_token().token);
let next_precedence = match op {
// UNION and EXCEPT have the same binding power and evaluate left-to-right
Some(SetOperator::Union) | Some(SetOperator::Except) => 10,
Some(SetOperator::Union) | Some(SetOperator::Except) | Some(SetOperator::Minus) => {
10
}
// INTERSECT has higher precedence than UNION/EXCEPT
Some(SetOperator::Intersect) => 20,
// Unexpected token or EOF => stop parsing the query body
Expand All @@ -9969,13 +9971,19 @@ impl<'a> Parser<'a> {
Token::Word(w) if w.keyword == Keyword::UNION => Some(SetOperator::Union),
Token::Word(w) if w.keyword == Keyword::EXCEPT => Some(SetOperator::Except),
Token::Word(w) if w.keyword == Keyword::INTERSECT => Some(SetOperator::Intersect),
Token::Word(w) if w.keyword == Keyword::MINUS => Some(SetOperator::Minus),
_ => None,
}
}

pub fn parse_set_quantifier(&mut self, op: &Option<SetOperator>) -> SetQuantifier {
match op {
Some(SetOperator::Except | SetOperator::Intersect | SetOperator::Union) => {
Some(
SetOperator::Except
| SetOperator::Intersect
| SetOperator::Union
| SetOperator::Minus,
) => {
if self.parse_keywords(&[Keyword::DISTINCT, Keyword::BY, Keyword::NAME]) {
SetQuantifier::DistinctByName
} else if self.parse_keywords(&[Keyword::BY, Keyword::NAME]) {
Expand Down
5 changes: 4 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6860,14 +6860,17 @@ fn parse_derived_tables() {
}

#[test]
fn parse_union_except_intersect() {
fn parse_union_except_intersect_minus() {
// TODO: add assertions
verified_stmt("SELECT 1 UNION SELECT 2");
verified_stmt("SELECT 1 UNION ALL SELECT 2");
verified_stmt("SELECT 1 UNION DISTINCT SELECT 1");
verified_stmt("SELECT 1 EXCEPT SELECT 2");
verified_stmt("SELECT 1 EXCEPT ALL SELECT 2");
verified_stmt("SELECT 1 EXCEPT DISTINCT SELECT 1");
verified_stmt("SELECT 1 MINUS SELECT 2");
verified_stmt("SELECT 1 MINUS ALL SELECT 2");
verified_stmt("SELECT 1 MINUS DISTINCT SELECT 1");
verified_stmt("SELECT 1 INTERSECT SELECT 2");
verified_stmt("SELECT 1 INTERSECT ALL SELECT 2");
verified_stmt("SELECT 1 INTERSECT DISTINCT SELECT 1");
Expand Down

0 comments on commit 5a761dd

Please sign in to comment.