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

   ## Which issue does this PR close?
   
   - Closes #27611.
   
   This is an alternative to #27573 and #27612, which fix two cases of the same 
bug in different places.
   
   ## Rationale for this change
   
   Some valid `UNION` queries fail during physical planning with:
   
   ```
   Internal error: Assertion failed: col.name() == matching_name: Input field 
name b does not match with the projection expression a
   ```
   
   Plans built directly with `LogicalPlanBuilder` can also run but return the 
wrong column names.
   
   Both cases come from the same mismatch. `UnionExec` takes its output column 
names from its first physical input, while the logical `Union` has its own 
schema, and nothing keeps the two in sync. They disagree in at least two 
situations:
   
   - **Duplicate names** (#27573): the union makes duplicate names unique (`a, 
a` becomes `a, a_1`), but its first input still produces `a, a`. This shows up 
after a Substrait round trip or with plans built in code.
   - **Removed first input** (#27611 / #27612): `propagate_empty_relation` 
drops an empty first input. The union keeps the names that came from that 
input, while the input now in first place has different ones.
   
   ```
           logical                     physical (before)            physical 
(after)
   
      Union {a, a_1}               UnionExec {a, a}            UnionExec {a, 
a_1}
        ├─ Limit {x.a, y.a}          ├─ ... {a, a}               ├─ 
ProjectionExec (a AS a, a AS a_1)
        └─ ...                       └─ ...                      │    └─ ... 
{a, a}
                                                                 └─ ...
                                 names come from input 0,
                                 so a parent looking up a_1 fails
   ```
   
   #27573 and #27612 each fix this by adding a renaming projection to the 
logical plan, in the analyzer's union coercion and in 
`propagate_empty_relation`. Each fixes the place where it was found, but any 
other code that builds or rewrites a `Union` can recreate the mismatch: other 
optimizer rules, custom rules, or plans built directly. Both PRs also change 
logical plans, and EXPLAIN output with them, for queries that already worked.
   
   Here the names are aligned where the mismatch actually matters: where a 
logical `Union` becomes a `UnionExec`. Unions match columns by position, so 
renaming the first input's columns to the union's names only changes labels. 
This covers every way a `Union` can be built. Logical plans don't change at 
all, and since the projection is only added when the names differ, no existing 
expected plan changed. A column-count mismatch now returns an internal error 
instead of being silently cut short.
   
   The same scenarios as the two other PRs are covered: their `.slt` queries 
(`limit.slt` also runs through the Substrait round trip in CI), plus two 
`LogicalPlanBuilder` tests that reproduce both cases without SQL or Substrait.
   
   ## Are there any user-facing changes?
   
   Queries that failed to plan now return rows, and plans built directly get 
the union's column names. No public API 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