Skip to content
Open
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
10 changes: 8 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,12 +1002,16 @@ pub enum Expr {
/// `true` when the `NOT` modifier is present.
negated: bool,
},
/// `<expr> [ NOT ] BETWEEN <low> AND <high>`
/// `<expr> [ NOT ] BETWEEN [ ASYMMETRIC | SYMMETRIC ] <low> AND <high>`
///
/// See [SYMMETRIC](https://www.postgresql.org/docs/current/functions-comparison.html)
Between {
/// Expression being compared.
expr: Box<Expr>,
/// `true` when the `NOT` modifier is present.
negated: bool,
/// `true` when the `SYMMETRIC` modifier is present.
symmetric: bool,
/// Lower bound.
low: Box<Expr>,
/// Upper bound.
Expand Down Expand Up @@ -1826,13 +1830,15 @@ impl fmt::Display for Expr {
Expr::Between {
expr,
negated,
symmetric,
low,
high,
} => write!(
f,
"{} {}BETWEEN {} AND {}",
"{} {}BETWEEN {}{} AND {}",
expr,
if *negated { "NOT " } else { "" },
if *symmetric { "SYMMETRIC " } else { "" },
low,
high
),
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,7 @@ impl Spanned for Expr {
Expr::Between {
expr,
negated: _,
symmetric: _,
low,
high,
} => expr.span().union(&low.span()).union(&high.span()),
Expand Down
9 changes: 8 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4457,8 +4457,14 @@ impl<'a> Parser<'a> {
Ok(in_op)
}

/// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed.
/// Parses `[ASYMMETRIC | SYMMETRIC] <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed.
pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
let symmetric = if self.parse_keyword(Keyword::SYMMETRIC) {
true
} else {
let _ = self.parse_keyword(Keyword::ASYMMETRIC);
false
};
Comment on lines +4462 to +4467

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should gate the modifier behind a dialect method. Consumed unconditionally, it breaks a BETWEEN symmetric AND 5 in every dialect where symmetric is an ordinary identifier.

Suggested change
let symmetric = if self.parse_keyword(Keyword::SYMMETRIC) {
true
} else {
let _ = self.parse_keyword(Keyword::ASYMMETRIC);
false
};
let symmetric = self.dialect.supports_between_symmetric()
&& self.parse_one_of_keywords(&[Keyword::SYMMETRIC, Keyword::ASYMMETRIC])
== Some(Keyword::SYMMETRIC);

// Stop parsing subexpressions for <low> and <high> on tokens with
// precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
let low = self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?;
Expand All @@ -4467,6 +4473,7 @@ impl<'a> Parser<'a> {
Ok(Expr::Between {
expr: Box::new(expr),
negated,
symmetric,
low: Box::new(low),
high: Box::new(high),
})
Expand Down
24 changes: 24 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,7 @@ fn parse_not_precedence() {
expr: Box::new(Expr::value(number("1"))),
low: Box::new(Expr::value(number("1"))),
high: Box::new(Expr::value(number("2"))),
symmetric: false,
negated: true,
}),
},
Expand Down Expand Up @@ -2735,6 +2736,7 @@ fn parse_between() {
expr: Box::new(Expr::Identifier(Ident::new("age"))),
low: Box::new(Expr::value(number("25"))),
high: Box::new(Expr::value(number("32"))),
symmetric: false,
negated,
},
select.selection.unwrap()
Expand Down Expand Up @@ -2762,6 +2764,7 @@ fn parse_between_with_expr() {
op: Plus,
right: Box::new(Expr::value(number("4"))),
}),
symmetric: false,
negated: false,
})),
select.selection.unwrap()
Expand All @@ -2785,6 +2788,7 @@ fn parse_between_with_expr() {
}),
low: Box::new(Expr::value(number("1"))),
high: Box::new(Expr::value(number("2"))),
symmetric: false,
negated: false,
}),
},
Expand Down Expand Up @@ -20067,6 +20071,26 @@ fn parse_insert_by_name() {
}
}

#[test]
fn parse_between_symmetric() {
// See https://www.postgresql.org/docs/current/functions-comparison.html
match verified_expr("1 BETWEEN SYMMETRIC 2 AND 3") {
Expr::Between {
symmetric, negated, ..
} => {
assert!(symmetric);
assert!(!negated);
}
_ => unreachable!(),
}

// ASYMMETRIC is the default behavior and is not preserved as SQL text.
one_statement_parses_to(
"SELECT * FROM t WHERE 1 NOT BETWEEN ASYMMETRIC 2 AND 3",
"SELECT * FROM t WHERE 1 NOT BETWEEN 2 AND 3",
);
Comment on lines +20076 to +20091

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional red tests.

Suggested change
// See https://www.postgresql.org/docs/current/functions-comparison.html
match verified_expr("1 BETWEEN SYMMETRIC 2 AND 3") {
Expr::Between {
symmetric, negated, ..
} => {
assert!(symmetric);
assert!(!negated);
}
_ => unreachable!(),
}
// ASYMMETRIC is the default behavior and is not preserved as SQL text.
one_statement_parses_to(
"SELECT * FROM t WHERE 1 NOT BETWEEN ASYMMETRIC 2 AND 3",
"SELECT * FROM t WHERE 1 NOT BETWEEN 2 AND 3",
);
// See https://www.postgresql.org/docs/current/functions-comparison.html
let supported = all_dialects_where(|d| d.supports_between_symmetric());
match supported.verified_expr("1 BETWEEN SYMMETRIC 2 AND 3") {
Expr::Between {
symmetric, negated, ..
} => {
assert!(symmetric);
assert!(!negated);
}
_ => unreachable!(),
}
// ASYMMETRIC is the default behavior and is not preserved as SQL text.
supported.one_statement_parses_to(
"SELECT * FROM t WHERE 1 NOT BETWEEN ASYMMETRIC 2 AND 3",
"SELECT * FROM t WHERE 1 NOT BETWEEN 2 AND 3",
);
let unsupported = all_dialects_where(|d| !d.supports_between_symmetric());
unsupported.verified_expr("a BETWEEN symmetric AND 5");
unsupported.verified_expr("a BETWEEN asymmetric AND 5");

}

#[test]
fn parse_bitwise_not_renders_apart_from_operand() {
all_dialects().verified_stmt("SELECT ~ -1");
Expand Down
Loading