kosiew opened a new issue, #27863:
URL: https://github.com/apache/datafusion/issues/27863

   ## Related PR
   #27192 
   
   ## Problem
   A join whose `ON` clause compares two columns from one input gets wrong 
results after a Substrait round-trip when the other input's qualifier also 
resolves the left input's bare qualifier. For example, `t` and `other.t` share 
the table name `t`.
   
   ```sql
   CREATE SCHEMA other;
   CREATE TABLE t AS SELECT 1 AS a, 2 AS b;
   CREATE TABLE other.t AS SELECT 1 AS b, 99 AS marker;
   
   SELECT r.marker FROM t l LEFT JOIN other.t r ON l.a = l.b;
   -- mirrored:
   SELECT l.marker FROM other.t l RIGHT JOIN t r ON r.a = r.b;
   ```
   
   | Path | Decoded join | Result |
   |---|---|---|
   | Direct SQL | — | `NULL` (`1 = 2` is false, so no match) |
   | unoptimized plan → `to_substrait_plan` → `from_substrait_plan` → `collect` 
| `Left Join: t.a = t.b` (mirrored: `Right Join: t.b = t.a`) | `99` |
   
   The decoded plan treats `t.b` as a right-input key. The planner binds that 
key to `other.t.b = 1`, so the row matches.
   
   #27192 fixes the non-overlapping case (`VALUES` inputs), but it doesn't 
change this one.
   
   ### Root cause
   - The Substrait field references decode correctly: ordinal → left field 
`t.b`. Input ownership is lost afterward, during name lookup.
   - `requalify_sides_if_needed` 
(`datafusion/expr/src/logical_plan/builder.rs:2032`) aliases the inputs to 
`left`/`right` only on identical qualifiers or qualified/unqualified 
collisions. It treats differing qualifiers as "no conflict" (`_ => {}` arm), 
even when one resolves to the other.
   - `DFSchema::index_of_column_by_name` 
(`datafusion/common/src/dfschema.rs:418`) matches qualifiers with 
`TableReference::resolved_eq`, so bare `t` matches the field qualified 
`other.t`.
   - As a result, `find_valid_equijoin_key_pair` (and, before #27192, 
shape-only classification) accepts `t.a = t.b` as a cross-input key. 
Classifying it as a residual filter wouldn't help either, because `t.b` stays 
ambiguous against the joined schema.
   
   ## Why it matters
   - The query silently returns wrong rows: unmatched rows on the preserved 
side get values from the wrong input. There is no error.
   - It's reachable from ordinary SQL with schema-qualified tables of the same 
name, a common layout (`public.orders` / `staging.orders`), and from any 
Substrait producer that emits such reads.
   
   ## Invariant / desired behavior
   After the consumer builds the join inputs, every column reference decoded 
from a `JoinRel` expression resolves to exactly the input that its ordinal came 
from. No qualifier on one input may `resolved_eq` a qualifier on the other 
input for a shared field name.
   
   ## Proposed direction
   Treat overlapping qualifiers as a conflict in `requalify_sides_if_needed`: 
if the two sides share a field name and either qualifier `resolved_eq`s the 
other, alias both sides to `left`/`right`, as the existing conflict arms 
already do. That fixes the input schemas before any expression is decoded, so 
keys and residuals both bind correctly, without changing the splitter or the 
wire format.
   
   Alternative if changing the shared helper turns out to be too broad: do the 
same check locally in the Substrait consumers (`join_rel.rs`, `cross_rel.rs`, 
`set_rel.rs`) before calling the helper. Decide between the two after auditing 
the callers listed under Scope.
   
   ## Scope
   ### In
   - Overlap detection in `requalify_sides_if_needed` (or a consumer-local 
equivalent).
   - Audit the helper's callers for changed output qualifiers:
     - Substrait `join_rel.rs:38`, `cross_rel.rs:27`, `set_rel.rs:290`
     - `LogicalPlanBuilder` INTERSECT/EXCEPT (`builder.rs:1648`)
     - the schema-based caller in `plan.rs:4946`
   - Executed Substrait round-trip regressions for both input orders.
   
   ### Out
   - Changing `TableReference::resolved_eq` or general `DFSchema` lookup 
semantics.
   - Changes to the Substrait producer or wire format.
   - Changing `find_valid_equijoin_key_pair` or the consumer's key/filter 
splitting (covered by #27192).
   
   ## Acceptance criteria
   - [ ] Both SQL examples above return a single `NULL` `marker` after the 
Substrait round-trip, matching direct SQL, for unoptimized and optimized input 
plans.
   - [ ] The decoded plan's same-input predicate appears as a join filter on 
input-distinct qualifiers (for example `left.a = left.b`), not as an equijoin 
key.
   - [ ] A genuine cross-input key between overlapping qualifiers still 
round-trips as an equijoin key with correct results (for example `ON l.a = r.b` 
→ `1 = 1`, one row with `marker = 99`).
   - [ ] Existing Substrait round-trip and plan-snapshot tests pass, or their 
snapshot changes are limited to the new `left`/`right` requalification for 
overlapping-qualifier inputs.
   
   ## Tests / verification
   - Round-trip regressions in 
`datafusion/substrait/tests/cases/roundtrip_logical_plan.rs`: LEFT and mirrored 
RIGHT cases above, plus the cross-input key case, asserting collected rows with 
`assert_batches_sorted_eq!`.
   - Unit test for `requalify_sides_if_needed`: `t` vs `other.t` with a shared 
field name requalifies; disjoint table names don't.
   - If the shared helper changes: `cargo xtask ci step test workspace`, and 
check the INTERSECT/EXCEPT SQLLogicTests for plan-text changes.
   


-- 
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