Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Fixed
- Lint GV003 now reports a join whose `ON` condition does not relate the two tables (`ON 1=1`, `ON TRUE`, `ON t.id = t.id`) as a cartesian product (error) instead of passing it as a key join. A real key, with or without extra conditions, and unqualified columns are unchanged. (#156)
- `impact`, `lineage` and `drift` refuse `--models` pointing to a folder with no `.sql` files with `status: error` and a `next_step` instead of reporting zero models or empty impact. Thanks to @shaw778! (#122, #147)

## 0.4.0 - 2026-11-11
Expand Down
40 changes: 40 additions & 0 deletions src/gulliver/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ def _aliases_in(expr: exp.Expression) -> set[str]:
return {col.table.lower() for col in expr.find_all(exp.Column) if col.table}


def _on_link(on: exp.Expression) -> str:
"""Does an ON condition relate tables to each other?

"constant": it names no column at all (ON 1=1, ON TRUE).
"unlinked": it has equalities, but each one stays inside a single table (ON t.id = t.id).
"ok": some equality compares columns of two tables, or Gulliver cannot tell (an unqualified
column could come from either side, so it is never guessed).
"""
if next(on.find_all(exp.Column), None) is None:
return "constant"
equalities = list(on.find_all(exp.EQ))
if not equalities:
return "ok" # no equality at all: GV008 handles it
for eq in equalities:
tables: set[str] = set()
for col in eq.find_all(exp.Column):
if not col.table:
return "ok"
tables.add(col.table.lower())
if len(tables) >= 2:
return "ok"
return "unlinked"


# reading `0 < (SELECT COUNT(*) ...)` as `(SELECT COUNT(*) ...) > 0`
_MIRRORED = {exp.GT: exp.LT, exp.LT: exp.GT, exp.GTE: exp.LTE, exp.LTE: exp.GTE}

Expand Down Expand Up @@ -278,6 +302,22 @@ def add(rule: str, severity: str, message: str, fix: str, node: exp.Expression |
continue
if join.args.get("on") is not None:
on = join.args["on"]
link = _on_link(on)
if link == "constant":
add("GV003", "error",
f"Join with {alias or 'a table'} has a condition that names no column "
f"({on.sql(dialect=dialect)}): a cartesian product.",
"Add ON <key> = <key>. If you really want every combination, write CROSS JOIN.",
join)
continue
if link == "unlinked":
add("GV003", "error",
f"Join with {alias or 'a table'} has a condition that does not link the two tables "
f"({on.sql(dialect=dialect)}): a cartesian product.",
"Compare a column of each table, e.g. ON a.id = b.id. If you really want every "
"combination, write CROSS JOIN.",
join)
continue
has_eq = any(True for _ in on.find_all(exp.EQ))
top_or = isinstance(on, exp.Or)
if not has_eq or top_or:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,37 @@ def test_non_equi_join():
assert rules("select a.id from a join b on a.ts between b.start_ts and b.end_ts limit 5")["GV008"] == "warn"


@pytest.mark.parametrize("on", ["1=1", "true", "'a' = 'a'", "t.id = t.id", "u.x = u.y", "t.id = t.id and 1=1"])
def test_join_condition_that_does_not_link_the_tables_is_a_cartesian_error(on):
r = rules(f"select t.a from t join u on {on} limit 5")
assert r["GV003"] == "error"
assert "GV008" not in r


@pytest.mark.parametrize("on", [
"t.id = u.id",
"t.id = u.id and 1=1",
"t.id = u.id and u.kind = 'x'",
"1=1 and t.id = u.id",
"id = user_id",
])
def test_join_condition_with_a_real_key_or_unknown_columns_is_not_flagged(on):
r = rules(f"select t.a from t join u on {on} limit 5")
assert "GV003" not in r


def test_join_using_and_cross_join_behave_as_before():
assert "GV003" not in rules("select a from t join u using (id) limit 5")
assert rules("select a from t cross join u limit 5")["GV003"] == "warn"


def test_unlinked_condition_message_names_the_condition():
stmt = classify("select t.a from t join u on t.id = t.id limit 5", "duckdb")
finding, = [f for f in lint(stmt.select, "duckdb") if f.rule == "GV003"]
assert "does not link the two tables" in finding.message
assert "t.id = t.id" in finding.message


def test_missing_partition_filter_uses_metadata():
info = {"events": {"partitioned_by": "event_date"}}
assert rules("select count(*) from events", table_info=info)["GV004"] == "error"
Expand Down
Loading