Skip to content

fix: graceful error for deeply nested expressions instead of stack overflow#22943

Open
nathanb9 wants to merge 1 commit into
apache:mainfrom
nathanb9:fix-22936-where-overflow
Open

fix: graceful error for deeply nested expressions instead of stack overflow#22943
nathanb9 wants to merge 1 commit into
apache:mainfrom
nathanb9:fix-22936-where-overflow

Conversation

@nathanb9

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

A flat WHERE col = 0 OR col = 1 OR ... chain builds a very deep BinaryExpr tree. The tree is built iteratively so building it is fine, but later recursive passes (derived Expr::clone, Drop, the optimizer) overflow the stack and abort the process. sqlparser's recursion_limit only guards nested expressions; flat equal-precedence chains are parsed iteratively and bypass it. We should fail gracefully with a planning error instead of crashing.

What changes are included in this PR?

  • Track expression-tree depth in the sql_expr_to_logical_expr stack machine, accumulating across nested sub-expressions via PlannerContext, and return a planning error when it exceeds recursion_limit * 10.

Are these changes tested?

Yes. New unit tests cover: chains within the limit succeed, over-deep chains return a graceful error, and depth accumulates across nesting. Existing deep-chain tests still pass.

Are there any user-facing changes?

Pathologically deep expressions now return a planning error instead of crashing. No API changes.

@github-actions github-actions Bot added the sql SQL Planner label Jun 13, 2026
@nathanb9 nathanb9 marked this pull request as ready for review June 14, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deep WHERE clause overflow stack in select_to_plan via derived expr::clone

1 participant