diff --git a/fuzz/fuzz_seeds/postgres_3001_nested_qualified_keyword_calls.sql b/fuzz/fuzz_seeds/postgres_3001_nested_qualified_keyword_calls.sql new file mode 100644 index 0000000000..ce21509728 --- /dev/null +++ b/fuzz/fuzz_seeds/postgres_3001_nested_qualified_keyword_calls.sql @@ -0,0 +1 @@ +SELECT a.CEIL(a.CEIL(a.CEIL(x))) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index e4c10f57d6..08e6a92057 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -2215,18 +2215,17 @@ impl<'a> Parser<'a> { // recursive `parse_subexpr` would re-walk the rest of the chain at // every dot. _ => { - // For a plain `Word` field (not followed by `(`), skip the - // speculative `parse_prefix`. The only result the validator - // below would accept is `Identifier`, which `parse_identifier` - // in the None branch produces directly. This avoids 2^N work - // on chains like `.not-b.not-b...` where `parse_prefix` would + // For a `Word` field, skip the speculative `parse_prefix`: + // the None branch reads it directly as an identifier or, when + // `(` follows, as a function call. This avoids 2^N work on + // chains like `.not-b.not-b...`, where `parse_prefix` would // descend into `parse_not` and re-walk the remaining chain at - // every segment. - let word_field_no_lparen = - matches!(self.peek_token_ref().token, Token::Word(_)) - && self.peek_nth_token_ref(1).token != Token::LParen; + // every segment, and on nested calls like `a.ceil(a.ceil(...`, + // where a call that `parse_prefix` rejects or fails on would + // have its arguments parsed a second time by the None branch. + let word_field = matches!(self.peek_token_ref().token, Token::Word(_)); - let expr = if word_field_no_lparen { + let expr = if word_field { None } else { self.maybe_parse(|parser| { @@ -2266,7 +2265,9 @@ impl<'a> Parser<'a> { } None => { let ident = self.parse_identifier()?; - let field = if self.peek_token_ref().token == Token::LParen { + let field = if self.peek_token_ref().token == Token::LParen + && !self.peek_outer_join_operator() + { self.parse_function(ObjectName::from(vec![ident]))? } else { Expr::Identifier(ident) diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 912baadb84..a8956b6497 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -23637,6 +23637,42 @@ fn parse_only_table_name() { } } +/// Hang guard for nested schema-qualified calls whose name is a keyword with +/// its own syntax. The call was first read as that keyword's expression and +/// rejected, then read again as a function, at every level. +#[test] +fn parse_nested_qualified_keyword_calls_no_exponential_blowup() { + use std::sync::mpsc; + use std::thread; + use std::time::Duration; + + for level in [ + "a.CEIL({})", + "pg_catalog.floor({})", + "pg_catalog.substring({}, 1, 2)", + "pg_catalog.trim({})", + ] { + let sql = format!( + "SELECT {}", + (0..30).fold("x".to_string(), |inner, _| level.replace("{}", &inner)) + ); + + let (tx, rx) = mpsc::channel(); + thread::spawn(move || { + let result = Parser::new(&PostgreSqlDialect {}) + .with_recursion_limit(1000) + .try_with_sql(&sql) + .and_then(|mut parser| parser.parse_statements()); + let _ = tx.send(result.is_ok()); + }); + + let parsed = rx + .recv_timeout(Duration::from_secs(5)) + .expect("parser should handle this quickly, not loop exponentially"); + assert!(parsed, "{level} nested 30 deep should parse"); + } +} + #[test] fn parse_grant_revoke_all_privileges_columns() { match verified_stmt("GRANT ALL PRIVILEGES (id) ON t TO r") {