Parser: fix exponential parse time on expression-named function arguments#2392
Merged
iffyio merged 1 commit intoJul 2, 2026
Merged
Conversation
iffyio
approved these changes
Jul 1, 2026
iffyio
left a comment
Contributor
There was a problem hiding this comment.
LGTM! Thanks @moshap-firebolt!
Contributor
|
@moshap-firebolt could you take a look at the conflict on the branch? |
…ents On dialects with expression-named function arguments (`supports_named_fn_args_with_expr_name`, e.g. PostgreSQL and MSSQL), `parse_function_args` speculatively parses the whole argument expression to detect the `name => value` form, then on failure rewinds and re-parses the same expression on the unnamed path. Because CAST and CASE fall back to function-call parsing when their reserved-word form fails, both passes recurse through the remaining chain, so on inputs like `CAST(CASE (CAST(CASE (...` work doubles per level. Parsing the leading expression once and then checking for a named-argument operator removes the redundant traversal; the resulting AST is identical on valid SQL. Measured on `PostgreSqlDialect` with `with_recursion_limit(256)`, release build. Input: `SELECT ` + `CAST(CASE (` repeated N times + `)` repeated 3N: | N | Before | After | |----|--------|--------| | 10 | 1.8 s | 781 us | | 15 | >30 s | 967 us | | 20 | >30 s | 1.6 ms | | 25 | >30 s | 2.3 ms | A wildcard can never be a named-argument name, so it is routed straight to the unnamed path, preserving prior behavior. This also makes the "reserved keyword as a bare function argument" error consistent across dialects (`SELECT MAX(interval)` now reports the same position regardless of named-argument support); `test_reserved_keywords_for_identifiers` is updated accordingly. Regression test in `tests/sqlparser_common.rs` runs a 30-level chain with a 5 s timeout (hits the timeout pre-fix, finishes in well under a millisecond post-fix); bench under `sqlparser_bench`.
3ed0dac to
5e5fb1c
Compare
Contributor
Author
@iffyio - done! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On dialects with expression-named function arguments
(
supports_named_fn_args_with_expr_name, e.g. PostgreSQL and MSSQL),parse_function_argsspeculatively parses the whole argument expression todetect the
name => valueform, then on failure rewinds and re-parses thesame expression on the unnamed path. Because CAST and CASE fall back to
function-call parsing when their reserved-word form fails, both passes recurse
through the remaining chain, so on inputs like
CAST(CASE (CAST(CASE (...workdoubles per level. Parsing the leading expression once and then checking for a
named-argument operator removes the redundant traversal; the resulting AST is
identical on valid SQL.
Measured on
PostgreSqlDialectwithwith_recursion_limit(256), releasebuild. Input:
SELECT+CAST(CASE (repeated N times +)repeated 3N:A wildcard can never be a named-argument name, so it is routed straight to the
unnamed path, preserving prior behavior. This also makes the "reserved keyword
as a bare function argument" error consistent across dialects (
SELECT MAX(interval)now reports the same position regardless of named-argumentsupport);
test_reserved_keywords_for_identifiersis updated accordingly.Regression test in
tests/sqlparser_common.rsruns a 30-level chain with a 5 stimeout (hits the timeout pre-fix, finishes in well under a millisecond
post-fix); bench under
sqlparser_bench.