Skip to content

fix!: keep each comma item's joins with it in Postgres, Databricks, DuckDB and SQL Server - #266

Merged
kvlonge merged 5 commits into
mainfrom
fix/comma-join-precedence
Nov 19, 2026
Merged

kvlonge merged 5 commits into
mainfrom
fix/comma-join-precedence

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Why

PostgreSQL, Databricks, DuckDB and SQL Server bind a comma looser than JOIN, so in a JOIN b USING (k), c the join reads only a and b. Our trees and semantics let c join in, which hid real errors and misordered SELECT *. Separately, SQLCLEAN049 missed UPDATE t SET x = 1 FROM a JOIN b ON a.k = b.k, which changes every row.

Changes

  • Breaking (trees): in a FROM list of several items, each item keeps its own joins as one joined table in the Postgres, Databricks, DuckDB and SQL Server parsers, so their tree JSON changes for joins after a comma. BigQuery, SQLite, ClickHouse and Snowflake trees are unchanged.
  • Semantics: a USING or NATURAL key, a right-outer side and a star's key placement reach only their own comma group; a key merged again in a nested join is listed once.
  • Lint: rules walk joins inside FROM items. SQLCLEAN038 now names the relation in 15–17 corpus statements per dialect (CROSS JOIN with tbl2 instead of an unnamed relation; only a file-wide exception), so a narrower exception can cover it. A one-row seed left of its own comma group is no longer flagged.
  • SQLCLEAN049: a join inside UPDATE FROM never limits the write; only WHERE, LIMIT or a join on the target does.

Verification

  • make check-ci on the head.
  • Comma grid vs main: +360 E201 (USING key missing on one side) in Postgres and Databricks; +17 E221 in Postgres, Databricks and DuckDB, all ..., exports c NATURAL JOIN customers d, all rejected by PostgreSQL 18 and DuckDB 2.0. Other dialects: star order only.
  • 110,003 corpus statements: 0 semantics changes in 8 dialects. Star lists match DuckDB/SQLite on 56/56 queries (0 at main).
  • SQLCLEAN049 cases match PostgreSQL 18 and DuckDB 2.0 row counts (3 of 3 unlimited, 1 of 3 with WHERE).

…uckDB and SQL Server

These dialects bind a comma looser than JOIN, so in `a JOIN b USING (k),
c` the join reads only a and b. DuckDB and SQL Server kept the first
item's joins in the select's joins while later items were collapsed, and
semantics lists FROM items before those joins, so c counted on b's left
side: a USING key c offered hid the missing-key E201, and a bare star
listed a, c, b. Postgres and Databricks flattened every item's joins into
the select's joins, with the same effect for every item.

In a FROM list of several items, each item now keeps its own joins as
one joined table, in all four parsers; a lone item keeps the select's
joins as before. SQL Server's query, UPDATE and DELETE share one reader.
BigQuery, SQLite, ClickHouse and Snowflake trees are unchanged. This is
breaking: the tree JSON of a FROM list with joins after a comma changes.

Semantics now records the sources each USING or NATURAL key covers, so a
join's keys, its right-outer side and a bare star's placement of a key
reach only the parts on its left in its own group. Reading an unqualified
name a merged key covers is unchanged.

DuckDB, Databricks and PostgreSQL cases cover a USING key only a later
comma item offers, and DuckDB and SQL Server cases the star order. A
DuckDB case keeps an unqualified USING key read beside a later item's
column of its name, which DuckDB binds.
The lint rules read only a select's own joins, so a join kept inside a
FROM item, in parentheses or after a comma, went unread: a cross join
there was reported against an unnamed relation, or never proved one
row, and its USING, NATURAL and ON conditions were missed by the CTE
column checks.

The cross-join, CTE-column and key checks now walk every join chain of a
select, each with the relation it starts from, and the relations of an
unaliased joined table count as the select's own sources.

Cross-join cases cover a cross join after a comma in DuckDB and
PostgreSQL, and a one-row seed on the left of its own comma group.
When a USING or NATURAL key merged again a key an inner join had merged,
the new key took the outer join's range of sources only, so a bare star
still placed the inner key's own sources' column: SELECT * FROM a JOIN
(b JOIN c USING (k)) USING (k) listed k twice, as did its RIGHT JOIN
form, a NATURAL JOIN (b NATURAL JOIN c), and a JOIN b JOIN c USING (k)
USING (k).

The merged key now covers the union of both ranges. DuckDB cases cover
the nested USING, RIGHT and NATURAL forms, and the same key merged in
two comma groups.
PostgreSQL and Databricks cases place a USING key first in its own comma
group, after the earlier item, and list a key merged twice by
a JOIN b JOIN c USING (k) USING (k) once. A PostgreSQL case keeps a
RIGHT JOIN after a comma from nulling the item before the comma, and the
PostgreSQL shape table records that a join stays with the item after
its comma.
SQLCLEAN049 counted a join in an UPDATE's FROM list as limiting the rows
written, so UPDATE t SET x = 1 FROM a JOIN b ON a.k = b.k was not
reported, though it changes every row of t. A join inside FROM, or
inside a DELETE's USING list, cannot read the target: PostgreSQL, DuckDB
and SQLite all refuse an ON that names it. So only WHERE, LIMIT and a
join on the target itself (UPDATE's table joins and the MySQL
multi-table DELETE's joins) limit which rows change.

PostgreSQL and DuckDB cases cover the one-item, comma and parenthesised
forms and DELETE USING, each changing every row on the engines, and the
same statements with a WHERE reading the target, which change one.
@kvlonge
kvlonge enabled auto-merge November 19, 2026 01:24
@kvlonge
kvlonge added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit 2786df7 Nov 19, 2026
9 checks passed
@kvlonge
kvlonge deleted the fix/comma-join-precedence branch November 19, 2026 02:41
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.

1 participant