Support nested expressions in BETWEEN

`BETWEEN <thing> AND <thing>` allows <thing> to be any expr that doesn't
contain boolean operators. (Allowing boolean operators would wreak
havoc, because of the repurposing of AND as both a boolean operation
and part of the syntax of BETWEEN.)
This commit is contained in:
Nikhil Benesch 2019-05-24 14:04:42 -04:00
parent 4f944dd4aa
commit ba21ce9d37
No known key found for this signature in database
GPG key ID: F7386C5DEADABA7F
2 changed files with 54 additions and 2 deletions

View file

@ -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) {