Skip to content

Add support for the Snowflake MINUS set operator #1652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2025
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
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 @@ -500,6 +500,7 @@ define_keywords!(
MILLISECOND,
MILLISECONDS,
MIN,
MINUS,
MINUTE,
MINUTES,
MINVALUE,
Expand Down Expand Up @@ -916,6 +917,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 @@ -979,6 +981,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 @@ -9855,7 +9855,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 @@ -9882,13 +9884,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
Loading