Repository navigation
Lower a SQL a AND b AND ... run as one boolean node - #13
Merged
Merged
Conversation
sqlglot nests a run of ANDs left-deep, one node per operator, and the adapter lowered that shape as is. Each level re-rendered and re-measured its whole subtree, so a WHERE clause was quadratic in its length: 300 conditions took 1.4s, and 1,000 took 8.7s before hitting the recursion limit and failing the request. One BOOLOP per operator also scored cognitive complexity per operator (299 for that 300-term clause) where Python's `a and b and ...` is one run worth 1. The run is now flattened with sqlglot's `flatten()` into a single BOOLOP with every operand, the shape Python's ast already gives. Cyclomatic complexity is unchanged, and 3,000 conditions take 0.4s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW
ethanzhoucool
marked this pull request as ready for review
November 3, 2026 17:26
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.
Summary
sqlglot parses
a AND b AND cleft-deep, asAnd(And(a, b), c), one node per operator.SqlLowerer.boolean()lowered that shape as it was, producing one BOOLOP per operator. Every one of those nodes re-rendered its whole subtree (sql_of) and re-measured its span (_offsets), so a long WHERE clause was quadratic, and its nesting depth grew with the number of conditions.Measured on
mainforSELECT a FROM t WHERE c0 = 0 AND c1 = 1 AND ...:mainRecursionError(a 500 from the API)RecursionErrorThe per-operator shape also disagreed with Python. Python's
ast.BoolOpis one node per run, and cognitive complexity charges +1 per run of like operators, sox = a and b and cscores 1 while the same condition in SQL scored 2 (and 299 for the clause above).Changes
adapters/sql.py:boolean()flattens the run with sqlglot'sExpression.flatten(), which is iterative and unwraps parentheses. The result is a single BOOLOP withoperand_countequal to the number of operands, and each operand is lowered as a child. A mixeda AND (b OR c) AND dstill nests itsORas its own BOOLOP.meta.operandskeeps up to 8 previews.Cyclomatic complexity is unchanged:
operand_count - 1on one node equals 1 per operator on the old chain. The walkthrough is unchanged: the bundled SQL sample and a multi-lineWHERE ... AND ... AND (... OR ...)produce identical lines at beginner and developer level, because BOOLOP never wins a line against WHERE or COMPARE.Testing
pyteston Python 3.13, 3.12 and 3.11: 149 passed, 3 skipped.ruffis clean, andmypyreports the same 26 errors as before.main, the first one withRecursionError:test_a_long_where_clause_is_one_boolean_run: a 1,000-term WHERE gives one BOOLOP with 1,000 operands and is not degraded.test_sql_and_python_score_boolean_runs_alike:a = 1 AND (b = 2 OR c = 3) AND d = 4and the same Python expression both score (cyclomatic 4, cognitive 2).Notes
This is independent of #2, which turns a
RecursionErrorinto a degraded answer instead of a 500. Here, long AND/OR runs no longer recurse at all.Generated by Claude Code