Repository navigation
fix!: keep each comma item's joins with it in Postgres, Databricks, DuckDB and SQL Server - #266
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
PostgreSQL, Databricks, DuckDB and SQL Server bind a comma looser than JOIN, so in
a JOIN b USING (k), cthe join reads onlyaandb. Our trees and semantics letcjoin in, which hid real errors and misorderedSELECT *. Separately, SQLCLEAN049 missedUPDATE t SET x = 1 FROM a JOIN b ON a.k = b.k, which changes every row.Changes
CROSS JOIN with tbl2instead ofan 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.Verification
make check-cion the head...., exports c NATURAL JOIN customers d, all rejected by PostgreSQL 18 and DuckDB 2.0. Other dialects: star order only.