Skip to content

Commit 19d5c4f

Browse files
SeanTroyUWOserprex
authored andcommitted
ANY and ALL contains their operators (apache#963)
1 parent 315ffc7 commit 19d5c4f

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

src/ast/mod.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,18 @@ pub enum Expr {
419419
pattern: Box<Expr>,
420420
escape_char: Option<char>,
421421
},
422-
/// Any operation e.g. `1 ANY (1)` or `foo > ANY(bar)`, It will be wrapped in the right side of BinaryExpr
423-
AnyOp(Box<Expr>),
424-
/// ALL operation e.g. `1 ALL (1)` or `foo > ALL(bar)`, It will be wrapped in the right side of BinaryExpr
425-
AllOp(Box<Expr>),
422+
/// Any operation e.g. `foo > ANY(bar)`, comparison operator is one of [=, >, <, =>, =<, !=]
423+
AnyOp {
424+
left: Box<Expr>,
425+
compare_op: BinaryOperator,
426+
right: Box<Expr>,
427+
},
428+
/// ALL operation e.g. `foo > ALL(bar)`, comparison operator is one of [=, >, <, =>, =<, !=]
429+
AllOp {
430+
left: Box<Expr>,
431+
compare_op: BinaryOperator,
432+
right: Box<Expr>,
433+
},
426434
/// Unary operation e.g. `NOT foo`
427435
UnaryOp { op: UnaryOperator, expr: Box<Expr> },
428436
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
@@ -724,8 +732,16 @@ impl fmt::Display for Expr {
724732
pattern
725733
),
726734
},
727-
Expr::AnyOp(expr) => write!(f, "ANY({expr})"),
728-
Expr::AllOp(expr) => write!(f, "ALL({expr})"),
735+
Expr::AnyOp {
736+
left,
737+
compare_op,
738+
right,
739+
} => write!(f, "{left} {compare_op} ANY({right})"),
740+
Expr::AllOp {
741+
left,
742+
compare_op,
743+
right,
744+
} => write!(f, "{left} {compare_op} ALL({right})"),
729745
Expr::UnaryOp { op, expr } => {
730746
if op == &UnaryOperator::PGPostfixFactorial {
731747
write!(f, "{expr}{op}")

src/parser/mod.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,16 +1786,32 @@ impl<'a> Parser<'a> {
17861786
let right = self.parse_subexpr(precedence)?;
17871787
self.expect_token(&Token::RParen)?;
17881788

1789-
let right = match keyword {
1790-
Keyword::ALL => Box::new(Expr::AllOp(Box::new(right))),
1791-
Keyword::ANY => Box::new(Expr::AnyOp(Box::new(right))),
1792-
_ => unreachable!(),
1789+
if !matches!(
1790+
op,
1791+
BinaryOperator::Gt
1792+
| BinaryOperator::Lt
1793+
| BinaryOperator::GtEq
1794+
| BinaryOperator::LtEq
1795+
| BinaryOperator::Eq
1796+
| BinaryOperator::NotEq
1797+
) {
1798+
return parser_err!(format!(
1799+
"Expected one of [=, >, <, =>, =<, !=] as comparison operator, found: {op}"
1800+
));
17931801
};
17941802

1795-
Ok(Expr::BinaryOp {
1796-
left: Box::new(expr),
1797-
op,
1798-
right,
1803+
Ok(match keyword {
1804+
Keyword::ALL => Expr::AllOp {
1805+
left: Box::new(expr),
1806+
compare_op: op,
1807+
right: Box::new(right),
1808+
},
1809+
Keyword::ANY => Expr::AnyOp {
1810+
left: Box::new(expr),
1811+
compare_op: op,
1812+
right: Box::new(right),
1813+
},
1814+
_ => unreachable!(),
17991815
})
18001816
} else {
18011817
Ok(Expr::BinaryOp {

tests/sqlparser_common.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,10 +1558,10 @@ fn parse_bitwise_ops() {
15581558
fn parse_binary_any() {
15591559
let select = verified_only_select("SELECT a = ANY(b)");
15601560
assert_eq!(
1561-
SelectItem::UnnamedExpr(Expr::BinaryOp {
1561+
SelectItem::UnnamedExpr(Expr::AnyOp {
15621562
left: Box::new(Expr::Identifier(Ident::new("a"))),
1563-
op: BinaryOperator::Eq,
1564-
right: Box::new(Expr::AnyOp(Box::new(Expr::Identifier(Ident::new("b"))))),
1563+
compare_op: BinaryOperator::Eq,
1564+
right: Box::new(Expr::Identifier(Ident::new("b"))),
15651565
}),
15661566
select.projection[0]
15671567
);
@@ -1571,10 +1571,10 @@ fn parse_binary_any() {
15711571
fn parse_binary_all() {
15721572
let select = verified_only_select("SELECT a = ALL(b)");
15731573
assert_eq!(
1574-
SelectItem::UnnamedExpr(Expr::BinaryOp {
1574+
SelectItem::UnnamedExpr(Expr::AllOp {
15751575
left: Box::new(Expr::Identifier(Ident::new("a"))),
1576-
op: BinaryOperator::Eq,
1577-
right: Box::new(Expr::AllOp(Box::new(Expr::Identifier(Ident::new("b"))))),
1576+
compare_op: BinaryOperator::Eq,
1577+
right: Box::new(Expr::Identifier(Ident::new("b"))),
15781578
}),
15791579
select.projection[0]
15801580
);

0 commit comments

Comments
 (0)