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
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/postgresql_2802_any_subquery_operator.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a = ANY(SELECT 1) - 1;
9 changes: 5 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4138,10 +4138,11 @@ impl<'a> Parser<'a> {
{
self.expect_token(&Token::LParen)?;
let right = if self.peek_sub_query() {
// We have a subquery ahead (SELECT\WITH ...) need to rewind and
// use the parenthesis for parsing the subquery as an expression.
self.prev_token(); // LParen
self.parse_subexpr(precedence)?
// The right side is the parenthesized subquery only; any
// operator after its `)` applies to the whole comparison.
let query = self.parse_query()?;
self.expect_token(&Token::RParen)?;
Expr::Subquery(query)
} else {
// Non-subquery expression
let right = self.parse_subexpr(precedence)?;
Expand Down
27 changes: 27 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14597,3 +14597,30 @@ fn parse_alter_column_set_storage() {
);
}
}

#[test]
fn parse_any_all_subquery_followed_by_operator() {
let select = pg().verified_only_select("SELECT a = ANY(SELECT 1) - 1");
match expr_from_projection(only(&select.projection)) {
Expr::BinaryOp { left, op, .. } => {
assert_eq!(*op, BinaryOperator::Minus);
assert!(matches!(left.as_ref(), Expr::AnyOp { .. }));
}
e => panic!("expected a binary operation, got {e:?}"),
}

let select = pg().verified_only_select("SELECT a = ALL(SELECT 1) + 1");
match expr_from_projection(only(&select.projection)) {
Expr::BinaryOp { left, op, .. } => {
assert_eq!(*op, BinaryOperator::Plus);
assert!(matches!(left.as_ref(), Expr::AllOp { .. }));
}
e => panic!("expected a binary operation, got {e:?}"),
}

let select = pg().verified_only_select("SELECT 1 = ANY(SELECT 1)::TEXT");
match expr_from_projection(only(&select.projection)) {
Expr::Cast { expr, .. } => assert!(matches!(expr.as_ref(), Expr::AnyOp { .. })),
e => panic!("expected a cast, got {e:?}"),
}
}
Loading