Skip to content

Lower a SQL a AND b AND ... run as one boolean node - #13

Merged
ethanzhoucool merged 2 commits into
mainfrom
claude/flatten-sql-boolean-chains
Nov 3, 2026
Merged

ethanzhoucool merged 2 commits into
mainfrom
claude/flatten-sql-boolean-chains

Conversation

@ethanzhoucool

Copy link
Copy Markdown
Owner

Summary

sqlglot parses a AND b AND c left-deep, as And(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 main for SELECT a FROM t WHERE c0 = 0 AND c1 = 1 AND ...:

conditions size main this PR
300 4.3 KB 1.42 s, cognitive 299 0.04 s, cognitive 1
1,000 15 KB 8.7 s, then RecursionError (a 500 from the API) 0.14 s
3,000 49 KB 32 s, then RecursionError 0.38 s

The per-operator shape also disagreed with Python. Python's ast.BoolOp is one node per run, and cognitive complexity charges +1 per run of like operators, so x = a and b and c scores 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's Expression.flatten(), which is iterative and unwraps parentheses. The result is a single BOOLOP with operand_count equal to the number of operands, and each operand is lowered as a child. A mixed a AND (b OR c) AND d still nests its OR as its own BOOLOP. meta.operands keeps up to 8 previews.

Cyclomatic complexity is unchanged: operand_count - 1 on one node equals 1 per operator on the old chain. The walkthrough is unchanged: the bundled SQL sample and a multi-line WHERE ... AND ... AND (... OR ...) produce identical lines at beginner and developer level, because BOOLOP never wins a line against WHERE or COMPARE.

Testing

  • pytest on Python 3.13, 3.12 and 3.11: 149 passed, 3 skipped. ruff is clean, and mypy reports the same 26 errors as before.
  • New tests. Both fail on main, the first one with RecursionError:
    • 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 = 4 and the same Python expression both score (cyclomatic 4, cognitive 2).

Notes

This is independent of #2, which turns a RecursionError into a degraded answer instead of a 500. Here, long AND/OR runs no longer recurse at all.


Generated by Claude Code

claude and others added 2 commits November 2, 2026 07:18
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
ethanzhoucool marked this pull request as ready for review November 3, 2026 17:26
@ethanzhoucool
ethanzhoucool merged commit 4c6ee2a into main Nov 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants