Skip to content

Parser: fix exponential parse time on (x) -> lambda chains - #2699

Open
moshap-firebolt wants to merge 2 commits into
apache:mainfrom
moshap-firebolt:lambda-exponential
Open

moshap-firebolt wants to merge 2 commits into
apache:mainfrom
moshap-firebolt:lambda-exponential

Conversation

@moshap-firebolt

Copy link
Copy Markdown
Contributor

try_parse_lambda speculatively parses (<params>) -> <body>. When the body fails, the fallback parses (x) as a nested expression and -> as a binary operator, whose right operand starts the same speculative parse again. A chain of lambdas whose last body is incomplete therefore doubles the work per level:

SELECT (x) -> (x) -> (x) -> ... 1 +

This affects every dialect whose supports_lambda_functions() is true: ClickHouse, Snowflake, DuckDB, Spark and Databricks. On each of them, release build, main 3330dc9e: 20 levels (110 bytes) take 1.4 s, 24 levels (130 bytes) take 23 s. With this change 24 levels take under 0.5 ms and 40 levels about 1 ms.

The fix caches where the lambda arm failed, keyed by position and ParserState as in #2619. Valid SQL never reaches the cache; only a chain that already failed retries it.

Tests: a timeout regression at 24 levels, which fails without the change, and the lambda and -> fallback forms, including a chain whose inner lambda is answered from the cache. Both run on every dialect with supports_lambda_functions().

This PR was prepared with AI assistance (Claude).

Dialects: ClickHouse, Snowflake, DuckDB, Spark, Databricks

🤖 Generated with Claude Code

moshap-firebolt and others added 2 commits October 21, 2026 21:06
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.53%. Comparing base (d9eec6e) to head (451fc78).

Files with missing lines Patch % Lines
src/parser/mod.rs 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2699   +/-   ##
=======================================
  Coverage   81.53%   81.53%           
=======================================
  Files          42       42           
  Lines       34218    34228   +10     
  Branches    34218    34228   +10     
=======================================
+ Hits        27898    27908   +10     
  Misses       2770     2770           
  Partials     3550     3550           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants