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
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,11 @@ pub trait Dialect: Debug + Any {
0
}

/// Precedence used to stop parsing the low bound expression of a BETWEEN predicate.
fn between_low_bound_precedence(&self) -> u8 {
self.prec_value(Precedence::Between)
}

/// Returns true if this dialect requires the `TABLE` keyword after `DESCRIBE`
///
/// Defaults to false.
Expand Down
7 changes: 6 additions & 1 deletion src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use alloc::boxed::Box;

use crate::ast::BinaryOperator;
use crate::ast::{Expr, Statement};
use crate::dialect::Dialect;
use crate::dialect::{Dialect, Precedence};
use crate::keywords::Keyword;
use crate::parser::{Parser, ParserError};

Expand Down Expand Up @@ -146,4 +146,9 @@ impl Dialect for SQLiteDialect {
fn supports_parens_around_table_factor(&self) -> bool {
true
}

/// Comparison and postfix operators bind within the BETWEEN low bound in SQLite (<https://www.sqlite.org/lang_expr.html>).
fn between_low_bound_precedence(&self) -> u8 {
self.prec_value(Precedence::And)
}
}
4 changes: 1 addition & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4548,9 +4548,7 @@ impl<'a> Parser<'a> {

/// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed.
pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
// 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))?;
let low = self.parse_subexpr(self.dialect.between_low_bound_precedence())?;
self.expect_keyword_is(Keyword::AND)?;
let high = self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?;
Ok(Expr::Between {
Expand Down
33 changes: 33 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,39 @@ fn parse_parenthesized_table_factor() {
);
}

#[test]
fn parse_between_low_bound_precedence() {
match sqlite().verified_expr("a BETWEEN b > 1 AND c") {
Expr::Between { low, .. } => assert!(
matches!(
*low,
Expr::BinaryOp {
op: BinaryOperator::Gt,
..
}
),
"{low:?}"
),
other => panic!("expected BETWEEN, got {other:?}"),
}
sqlite().verified_expr("a BETWEEN b LIKE 'x' AND c");
sqlite().verified_expr("a BETWEEN b IS NOT NULL AND c");
sqlite().expr_parses_to("a BETWEEN b NOTNULL AND c", "a BETWEEN b IS NOT NULL AND c");
sqlite().verified_expr("a BETWEEN b BETWEEN 1 AND 2 AND c");

// Operators after the high bound apply to the whole BETWEEN.
sqlite().verified_expr("5 BETWEEN 1 AND NULL IS NULL");
sqlite().verified_expr("5 BETWEEN 1 AND 10 = 1");

assert_eq!(
TestedDialects::new(vec![Box::new(GenericDialect {})])
.parse_sql_statements("SELECT a BETWEEN b > 1 AND c FROM t"),
Err(ParserError::ParserError(
"Expected: AND, found: >".to_string()
))
);
}

fn sqlite() -> TestedDialects {
TestedDialects::new(vec![Box::new(SQLiteDialect {})])
}
Expand Down
Loading