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 @@ -995,12 +995,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 @@ -1819,13 +1823,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
};
// 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 @@ -20060,3 +20064,23 @@ fn parse_insert_by_name() {
_ => unreachable!(),
}
}

#[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",
);
}
Loading