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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a.CEIL(a.CEIL(a.CEIL(x)))
23 changes: 12 additions & 11 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Loading