From a0e780a7185f37809bbe9f5b38a6dc02269ff601 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 07:18:47 +0000 Subject: [PATCH] Lower a SQL `a AND b AND ...` run as one boolean node 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 Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW --- explain_my_code/adapters/sql.py | 18 ++++++++++++------ tests/test_analysis.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/explain_my_code/adapters/sql.py b/explain_my_code/adapters/sql.py index 3d458e4..f5ea460 100644 --- a/explain_my_code/adapters/sql.py +++ b/explain_my_code/adapters/sql.py @@ -486,21 +486,27 @@ def comparison(self, expression: Any) -> Node: ) def boolean(self, expression: Any) -> Node: + """One BOOLOP for a whole `a AND b AND c` run, as Python's `ast.BoolOp` gives. + + sqlglot nests the run left-deep, one `And` per operator. Lowering that shape + made a node per operator, each re-rendering and re-measuring everything under + it, so a long WHERE clause was quadratic and hit the recursion limit. It also + scored cognitive complexity per operator rather than per run. + """ operator = "AND" if isinstance(expression, exp.And) else "OR" + operands = list(expression.flatten()) node = Node( kind=Kind.BOOLOP, span=self.span_for(expression), name=operator, meta={ "op": operator.lower(), - "operand_count": 2, - "operands": [ - self.sql_of(expression.this, 24), - self.sql_of(expression.expression, 24), - ], + "operand_count": len(operands), + "operands": [self.sql_of(operand, 24) for operand in operands[:8]], }, ) - node.extend(self.children(expression)) + for operand in operands: + node.extend(self.lower(operand)) return node def function(self, expression: Any) -> Node: diff --git a/tests/test_analysis.py b/tests/test_analysis.py index 87d8b2c..4d09d27 100644 --- a/tests/test_analysis.py +++ b/tests/test_analysis.py @@ -271,6 +271,24 @@ async def load(urls): assert {"comprehension", "async-await", "error-handling"} <= concept_ids(analysis) +def test_a_long_where_clause_is_one_boolean_run(): + """sqlglot nests `a AND b AND ...` one level per operator; lowered as-is, a + 1,000-term WHERE clause took ~9s and then hit the recursion limit.""" + conditions = " AND ".join(f"c{i} = {i}" for i in range(1_000)) + result = explain(f"SELECT a FROM t WHERE {conditions};", language=Language.SQL) + assert not result.analysis.degraded + runs = [n for n in result.analysis.root.walk() if n.kind.value == "boolop"] + assert [n.meta["operand_count"] for n in runs] == [1_000] + + +def test_sql_and_python_score_boolean_runs_alike(): + sql = analyze("SELECT 1 FROM t WHERE a = 1 AND (b = 2 OR c = 3) AND d = 4;", Language.SQL) + python = analyze("x = a == 1 and (b == 2 or c == 3) and d == 4\n") + assert (sql.metrics.cyclomatic, sql.metrics.cognitive) == ( + python.metrics.cyclomatic, python.metrics.cognitive, + ) == (4, 2) + + def test_sql_concepts_are_language_scoped(): python = analyze("def f():\n return 1\n") assert not {c for c in concept_ids(python) if c.startswith("sql-")}