Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): stop sql_select_star on EXISTS and explicit-column sources, skip temp-table DML (#4775)

Raghav Chamadiya4990ba45e5ec
diff --git a/packages/core/src/repowise/core/analysis/health/sql_complexity.py b/packages/core/src/repowise/core/analysis/health/sql_complexity.py
index da55d166b..39e70e0d5 100644
--- a/packages/core/src/repowise/core/analysis/health/sql_complexity.py
+++ b/packages/core/src/repowise/core/analysis/health/sql_complexity.py
@@ -26,6 +26,7 @@ none of it may move the surfaced defect score.
from __future__ import annotations
import re
+from collections.abc import Callable
from typing import Any
from .complexity import FileComplexity, FunctionComplexity, PerfHit
@@ -187,6 +188,7 @@ def _statement_smells(text: str, dialect: str | None) -> list[PerfHit]:
return []
hits: list[PerfHit] = []
+ temp_tables: set[str] = set()
cursor = 0
def _anchor(token: str) -> int:
@@ -197,76 +199,120 @@ def _statement_smells(text: str, dialect: str | None) -> list[PerfHit]:
for stmt in statements:
if stmt is None:
continue
+ # A temp table created earlier in the file is scratch space: emptying
+ # it whole is the intent, not a missing WHERE.
+ if _is_temp_table(stmt, exp):
+ temp_tables.add((_create_name(stmt, exp) or "").lower())
+ # Source order matters: each hit advances the forward-only anchor cursor.
+ star = _select_star_hit(stmt, exp, _anchor)
+ dml = _whole_table_dml_hit(stmt, exp, _anchor, temp_tables)
+ hits += [hit for hit in (star, dml) if hit is not None]
+ hits.extend(_cartesian_join_hits(stmt, exp, _anchor))
+ return hits
- # -- sql_select_star: bare * projection inside a maintained relation.
- if isinstance(stmt, exp.Create) and (stmt.kind or "").upper() in _STAR_SCOPES:
- name = _create_name(stmt, exp)
- for sel in stmt.find_all(exp.Select):
- if any(isinstance(e, exp.Star) for e in sel.expressions):
- hits.append(
- PerfHit(
- kind="sql_select_star",
- line=_anchor(name) if name else 1,
- function=name,
- detail=(stmt.kind or "").lower(),
- )
- )
- break # one finding per relation, not per nested select
-
- # -- sql_update_delete_without_where: whole-table DML.
- # A LIMIT (``UPDATE ... LIMIT n`` / ``DELETE ... LIMIT n`` in MySQL and
- # friends) bounds the affected row count, so the statement provably does
- # NOT touch every row — treat it like a WHERE and stay silent.
- elif (
- isinstance(stmt, (exp.Update, exp.Delete))
- and not stmt.args.get("where")
- and not stmt.args.get("limit")
- ):
- table = _clean_ident(getattr(stmt.this, "name", "") or "")
- if table:
- hits.append(
- PerfHit(
- kind="sql_update_delete_without_where",
- line=_anchor(table),
- function=None,
- detail=f"{type(stmt).__name__.lower()} {table}",
- )
- )
- # -- sql_cartesian_join: comma-join with no predicate anywhere.
- for sel in stmt.find_all(exp.Select):
- if sel.args.get("where"):
- continue
- # ``SELECT ... INTO a, b`` — a multi-target INTO — is parsed with
- # the trailing target (``b``) as a comma-join FROM item, so a
- # table-creating statement looks like a cartesian join (issue
- # #1502). ``into`` is present exactly when the trailing names are
- # INTO targets, not joins; skip those.
- if sel.args.get("into"):
- continue
- for join in sel.args.get("joins") or ():
- if (
- not join.args.get("kind") # CROSS/OUTER etc. are explicit intent
- and not join.args.get("side")
- and not join.args.get("on")
- and not join.args.get("using")
- and not join.args.get("method") # NATURAL carries its predicate
- ):
- right = _clean_ident(getattr(join.this, "name", "") or "")
- if right is None:
- continue
- hits.append(
- PerfHit(
- kind="sql_cartesian_join",
- line=_anchor(right),
- function=None,
- detail=right,
- )
- )
- break # one finding per select
+def _select_star_hit(stmt: Any, exp: Any, anchor: Callable[[str], int]) -> PerfHit | None:
+ """sql_select_star: a bare ``*`` projection inside a maintained relation."""
+ if not (isinstance(stmt, exp.Create) and (stmt.kind or "").upper() in _STAR_SCOPES):
+ return None
+ ctes = {(c.alias or "").lower() for c in stmt.find_all(exp.CTE)}
+ if not any(_star_reads_base_table(sel, ctes, exp) for sel in stmt.find_all(exp.Select)):
+ return None # one finding per relation, not per nested select
+ name = _create_name(stmt, exp)
+ return PerfHit(
+ kind="sql_select_star",
+ line=anchor(name) if name else 1,
+ function=name,
+ detail=(stmt.kind or "").lower(),
+ )
+
+
+def _whole_table_dml_hit(
+ stmt: Any, exp: Any, anchor: Callable[[str], int], temp_tables: set[str]
+) -> PerfHit | None:
+ """sql_update_delete_without_where: whole-table DML.
+
+ A LIMIT (``UPDATE ... LIMIT n`` / ``DELETE ... LIMIT n`` in MySQL and
+ friends) bounds the affected row count, so the statement provably does
+ NOT touch every row: treat it like a WHERE and stay silent.
+ """
+ if not isinstance(stmt, (exp.Update, exp.Delete)):
+ return None
+ if stmt.args.get("where") or stmt.args.get("limit"):
+ return None
+ table = _clean_ident(getattr(stmt.this, "name", "") or "")
+ if not table or table.lower() in temp_tables:
+ return None
+ return PerfHit(
+ kind="sql_update_delete_without_where",
+ line=anchor(table),
+ function=None,
+ detail=f"{type(stmt).__name__.lower()} {table}",
+ )
+
+
+def _cartesian_join_hits(stmt: Any, exp: Any, anchor: Callable[[str], int]) -> list[PerfHit]:
+ """sql_cartesian_join: a comma-join with no predicate anywhere, one per select."""
+ hits: list[PerfHit] = []
+ for sel in stmt.find_all(exp.Select):
+ # ``SELECT ... INTO a, b`` (a multi-target INTO) is parsed with the
+ # trailing target (``b``) as a comma-join FROM item, so a
+ # table-creating statement looks like a cartesian join (issue #1502).
+ # ``into`` is present exactly when the trailing names are INTO
+ # targets, not joins; skip those.
+ if sel.args.get("where") or sel.args.get("into"):
+ continue
+ right = next((r for r in map(_bare_join_name, sel.args.get("joins") or ()) if r), None)
+ if right is not None:
+ hits.append(
+ PerfHit(kind="sql_cartesian_join", line=anchor(right), function=None, detail=right)
+ )
return hits
+def _bare_join_name(join: Any) -> str | None:
+ """The joined table's name when *join* carries no predicate or explicit kind."""
+ if any(
+ join.args.get(k)
+ # CROSS/OUTER etc. are explicit intent; NATURAL (``method``) carries its predicate.
+ for k in ("kind", "side", "on", "using", "method")
+ ):
+ return None
+ return _clean_ident(getattr(join.this, "name", "") or "")
+
+
+def _star_reads_base_table(sel: Any, ctes: set[str], exp: Any) -> bool:
+ """True when *sel* projects a bare ``*`` straight off a base table.
+
+ Silent for a star under ``EXISTS`` (the projection is ignored) and for a
+ star over only CTEs or subqueries: their column list is explicit, or their
+ own inner ``SELECT *`` is visited and judged on its own.
+ """
+ if not any(isinstance(e, exp.Star) for e in sel.expressions):
+ return False
+ node = sel.parent
+ while node is not None and not isinstance(node, exp.Select):
+ if isinstance(node, exp.Exists):
+ return False
+ node = node.parent
+ from_ = sel.args.get("from")
+ sources = [from_.this] if from_ else []
+ sources += [j.this for j in sel.args.get("joins") or ()]
+ return any(
+ isinstance(src, exp.Table)
+ and isinstance(src.this, exp.Identifier)
+ and (src.args.get("db") or src.name.lower() not in ctes)
+ for src in sources
+ )
+
+
+def _is_temp_table(stmt: Any, exp: Any) -> bool:
+ if not isinstance(stmt, exp.Create) or (stmt.kind or "").upper() != "TABLE":
+ return False
+ props = stmt.args.get("properties")
+ return bool(props) and any(isinstance(p, exp.TemporaryProperty) for p in props.expressions)
+
+
def _create_name(stmt: Any, exp: Any) -> str | None:
target = stmt.this
if isinstance(target, exp.Schema):
diff --git a/tests/unit/health/test_sql_markers.py b/tests/unit/health/test_sql_markers.py
index 6f2b6ca78..11a1052b7 100644
--- a/tests/unit/health/test_sql_markers.py
+++ b/tests/unit/health/test_sql_markers.py
@@ -149,6 +149,46 @@ class TestSelectStar:
sql = "CREATE VIEW v AS SELECT * FROM (SELECT * FROM t) sub"
assert _smell_kinds(sql) == ["sql_select_star"]
+ def test_fires_on_plain_table_in_view(self) -> None:
+ assert _smell_kinds("CREATE VIEW v AS SELECT * FROM users") == ["sql_select_star"]
+
+ def test_silent_inside_exists(self) -> None:
+ sql = (
+ "CREATE VIEW v AS SELECT id FROM users u "
+ "WHERE EXISTS (SELECT * FROM orders o WHERE o.user_id = u.id)"
+ )
+ assert _smell_kinds(sql) == []
+
+ def test_silent_inside_not_exists(self) -> None:
+ sql = (
+ "CREATE VIEW v AS SELECT id FROM users u "
+ "WHERE NOT EXISTS (SELECT * FROM orders o WHERE o.user_id = u.id)"
+ )
+ assert _smell_kinds(sql) == []
+
+ def test_silent_over_explicit_cte(self) -> None:
+ sql = "CREATE VIEW v AS WITH c AS (SELECT id, name FROM users) SELECT * FROM c"
+ assert _smell_kinds(sql) == []
+
+ def test_silent_over_explicit_subquery(self) -> None:
+ sql = "CREATE VIEW v AS SELECT * FROM (SELECT id, name FROM users) sub"
+ assert _smell_kinds(sql) == []
+
+ def test_fires_when_cte_itself_selects_star(self) -> None:
+ sql = "CREATE VIEW v AS WITH c AS (SELECT * FROM users) SELECT * FROM c"
+ assert _smell_kinds(sql) == ["sql_select_star"]
+
+ def test_fires_on_qualified_table_sharing_a_cte_name(self) -> None:
+ sql = "CREATE VIEW v AS WITH users AS (SELECT id FROM t) SELECT * FROM other.users"
+ assert _smell_kinds(sql) == ["sql_select_star"]
+
+ def test_fires_when_star_joins_base_table(self) -> None:
+ sql = (
+ "CREATE VIEW v AS WITH c AS (SELECT id FROM users) "
+ "SELECT * FROM c JOIN orders o ON o.user_id = c.id"
+ )
+ assert _smell_kinds(sql) == ["sql_select_star"]
+
class TestUpdateDeleteWithoutWhere:
def test_update_fires(self) -> None:
@@ -159,6 +199,14 @@ class TestUpdateDeleteWithoutWhere:
def test_delete_fires(self) -> None:
assert _smell_kinds("DELETE FROM sessions") == ["sql_update_delete_without_where"]
+ def test_silent_on_temp_table_from_same_file(self) -> None:
+ sql = "CREATE TEMP TABLE tmp (id INT); DELETE FROM tmp; UPDATE tmp SET id = 0"
+ assert _smell_kinds(sql) == []
+
+ def test_fires_on_regular_table_created_in_same_file(self) -> None:
+ sql = "CREATE TABLE logs (id INT); DELETE FROM logs"
+ assert _smell_kinds(sql) == ["sql_update_delete_without_where"]
+
def test_silent_with_where(self) -> None:
assert _smell_kinds("UPDATE users SET active = 0 WHERE id = 1") == []
assert _smell_kinds("DELETE FROM sessions WHERE expired = 1") == []