Skip to content
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
7 changes: 5 additions & 2 deletions src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,12 @@ impl Parser {

/// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed
pub fn parse_between(&mut self, expr: ASTNode, negated: bool) -> Result<ASTNode, ParserError> {
let low = self.parse_prefix()?;
// Stop parsing subexpressions for <low> and <high> on tokens with
// precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
let prec = self.get_precedence(&Token::make_keyword("BETWEEN"))?;
let low = self.parse_subexpr(prec)?;
self.expect_keyword("AND")?;
let high = self.parse_prefix()?;
let high = self.parse_subexpr(prec)?;
Ok(ASTNode::SQLBetween {
expr: Box::new(expr),
negated,
Expand Down
49 changes: 49 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,55 @@ fn parse_between() {
chk(true);
}

#[test]
fn parse_between_with_expr() {
use self::ASTNode::*;
use self::SQLOperator::*;
let sql = "SELECT * FROM t WHERE 1 BETWEEN 1 + 2 AND 3 + 4 IS NULL";
let select = verified_only_select(sql);
assert_eq!(
ASTNode::SQLIsNull(Box::new(ASTNode::SQLBetween {
expr: Box::new(ASTNode::SQLValue(Value::Long(1))),
low: Box::new(SQLBinaryExpr {
left: Box::new(ASTNode::SQLValue(Value::Long(1))),
op: Plus,
right: Box::new(ASTNode::SQLValue(Value::Long(2))),
}),
high: Box::new(SQLBinaryExpr {
left: Box::new(ASTNode::SQLValue(Value::Long(3))),
op: Plus,
right: Box::new(ASTNode::SQLValue(Value::Long(4))),
}),
negated: false,
})),
select.selection.unwrap()
);

let sql = "SELECT * FROM t WHERE 1 = 1 AND 1 + x BETWEEN 1 AND 2";
let select = verified_only_select(sql);
assert_eq!(
ASTNode::SQLBinaryExpr {
left: Box::new(ASTNode::SQLBinaryExpr {
left: Box::new(ASTNode::SQLValue(Value::Long(1))),
op: SQLOperator::Eq,
right: Box::new(ASTNode::SQLValue(Value::Long(1))),
}),
op: SQLOperator::And,
right: Box::new(ASTNode::SQLBetween {
expr: Box::new(ASTNode::SQLBinaryExpr {
left: Box::new(ASTNode::SQLValue(Value::Long(1))),
op: SQLOperator::Plus,
right: Box::new(ASTNode::SQLIdentifier("x".to_string())),
}),
low: Box::new(ASTNode::SQLValue(Value::Long(1))),
high: Box::new(ASTNode::SQLValue(Value::Long(2))),
negated: false,
}),
},
select.selection.unwrap(),
)
}

#[test]
fn parse_select_order_by() {
fn chk(sql: &str) {
Expand Down