From 36a5b017896abcfe2c5d47dfdc7b2ef55e8a0dd4 Mon Sep 17 00:00:00 2001 From: yussypu Date: Sat, 28 Nov 2026 09:12:40 +0500 Subject: [PATCH] Parser: Stop an ANY/ALL subquery operand at its closing parenthesis Closes #2802 --- .../postgresql_2802_any_subquery_operator.sql | 1 + src/parser/mod.rs | 9 ++++--- tests/sqlparser_postgres.rs | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 fuzz/fuzz_seeds/postgresql_2802_any_subquery_operator.sql diff --git a/fuzz/fuzz_seeds/postgresql_2802_any_subquery_operator.sql b/fuzz/fuzz_seeds/postgresql_2802_any_subquery_operator.sql new file mode 100644 index 0000000000..48fcac18be --- /dev/null +++ b/fuzz/fuzz_seeds/postgresql_2802_any_subquery_operator.sql @@ -0,0 +1 @@ +SELECT a = ANY(SELECT 1) - 1; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 82bdbf956e..c769a8d941 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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)?; diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 1cd2009e89..9795b626b3 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -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:?}"), + } +}