Phoenix500526 opened a new pull request, #22998:
URL: https://github.com/apache/datafusion/pull/22998

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and 
enhancements and this helps us generate change logs for our releases. You can 
link an issue to this PR using the GitHub syntax. For example `Closes #123` 
indicates that this PR will close issue #123.
   -->
   
   - Closes #22881
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in 
the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your 
changes and offer better suggestions for fixes.  
   -->
   
   A `USING` / `NATURAL` join exposes its join key as a single merged column 
whose value, per the SQL standard, is `COALESCE(left.key, right.key)`. 
DataFusion resolved an unqualified reference to that merged key to the left 
column unconditionally.
   
   For `RIGHT` and `FULL` joins the left key is NULL-padded on rows that exist 
only on the right, so the merged key came out NULL instead of the value that is 
actually present. The wrong NULL is silent and propagates into `GROUP BY`, 
`ORDER BY` (changing row order) and `SELECT *`, corrupting results; `WHERE` on 
the merged key additionally failed with an "ambiguous reference" error. `INNER` 
/ `LEFT` are unaffected, since their left key is never NULL-padded.
   
   ```sql
   create table a(k int, x int) as values (1, 10), (2, 20), (3, 30);
   create table b(k int, y int) as values (2, 200), (3, 300), (4, 400);
   
   select k from a right join b using (k) order by k nulls last;
   -- before: 2, 3, NULL      after: 2, 3, 4
   ```
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is 
sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   The unqualified merged key of a `RIGHT` / `FULL`` USING` / `NATURAL` join 
now resolves to `COALESCE(left, right)` everywhere it can be referenced:
   
   * column normalization (`SELECT`, `ORDER BY`, `GROUP BY`, `HAVING`, 
`QUALIFY`);
   * `WHERE` predicates — previously rejected as ambiguous; they now resolve 
against the join's real USING columns;
   * wildcard expansion (`SELECT *`).
   
   Mechanics:
   
   * a new `LogicalPlan::outer_using_key_pairs()` returns the `(left, right)` 
key pairs of `RIGHT` / `FULL` `USING` / `NATURAL` joins;
   * the merged key is materialized as `CASE WHEN left IS NOT NULL THEN left 
ELSE right END` — the exact form coalesce is simplified to, so it can be built 
in `datafusion-expr` without depending on the functions crate — aliased to the 
key name so the output column keeps its name.
   `INNER` / `LEFT` joins and qualified access (a.k / b.k) are left unchanged.
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are 
they covered by existing tests)?
   -->
   
   Yes. A new sqllogictest file `join_using_merged_key.slt` covers the merged 
key for `RIGHT` / `FULL` / `NATURAL` joins across `SELECT`, `SELECT *`, `WHERE` 
and `ORDER BY`, with `INNER` / `LEFT`, qualified `a.k` / `b.k` access, and the 
explicit `coalesce(a.k, b.k) ... ON` form as regression guards.
   
   The full sqllogictest suite and the `datafusion-common` /`datafusion-expr` / 
`datafusion-sql` unit tests pass with no regressions.
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be 
updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api 
change` label.
   -->
   
   Yes. For `RIGHT` / `FULL` `USING` / `NATURAL` joins, the merged join key now 
returns `COALESCE(left, right)` (the value from whichever side is present) 
instead of `NULL` for rows that exist only on the right, and referencing the 
merged key in `WHERE` no longer raises an ambiguous-reference error. Queries 
that do not use `RIGHT` / `FULL` `USING` / `NATURAL` joins are unaffected.
   
   No breaking public API changes (the change only adds new public items).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to