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

   ## Which issue does this PR close?
   
   - Closes #26516.
   
   ## Rationale for this change
   
   A `UNION ALL` fails to plan with `Schema error: No field named ...` when the 
optimizer removes its first input and a later input remains. For example:
   
   ```sql
   SELECT 1 WHERE false UNION ALL (SELECT 2 LIMIT 1);
   ```
   
   `propagate_empty_relation` puts a projection over the remaining input with 
`Projection::new_from_schema`. That function makes column references from the 
union's field names, which come from the first input. When the remaining input 
has other names, the projection refers to columns that do not exist, and 
`optimize_projections` fails.
   
   ## What changes are included in this PR?
   
   - When one input remains and its schema is different from the union's 
schema, map its columns to the union's output fields **by position**, alias 
each one to the union's name, and build the projection with 
`Projection::try_new` (which also checks the result).
   - Three existing snapshots change their text from `Projection: a, b, c` to 
`Projection: test.a AS a, test.b AS b, test.c AS c`. In those tests the 
remaining input is the first input, so the old by-name references worked. The 
output schema is the same.
   
   ## What is the testing strategy for this PR?
   
   - New unit test `propagate_union_remaining_input_with_other_names`: the 
first input is empty and the second input has other column names. Without the 
fix, the rule produces `Projection: a, b, c` over an input that only has `x, y, 
z`.
   - New `union.slt` cases with the two queries from the issue.
   
   ## Are there any user-facing changes?
   
   No. The queries that failed now return results. There are 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