gabotechs commented on code in PR #27612:
URL: https://github.com/apache/datafusion/pull/27612#discussion_r4679027171
##########
datafusion/optimizer/src/propagate_empty_relation.rs:
##########
@@ -195,23 +195,25 @@ impl OptimizerRule for PropagateEmptyRelation {
// The union takes its field names from its first
// input, which may be gone. Map the remaining
// input's columns by position.
- let exprs = child
- .schema()
- .columns()
- .into_iter()
- .zip(plan.schema().iter())
- .map(|(col, (qualifier, field))| {
- Expr::Column(col)
- .alias_qualified(qualifier.cloned(),
field.name())
- })
- .collect::<Vec<_>>();
- Ok(Transformed::yes(LogicalPlan::Projection(
- Projection::try_new(exprs, Arc::new(child))?,
- )))
+ Ok(Transformed::yes(align_union_input(child,
plan.schema())?))
}
} else {
+ // Same name mismatch as the single-input case, but the
+ // union itself remains. Physical planning matches input
+ // fields to these names and asserts when they differ.
+ let inputs = new_inputs
+ .into_iter()
+ .map(|input| {
+ let child = Arc::unwrap_or_clone(input);
+ if union_input_names_match(&child, plan.schema()) {
+ Ok(Arc::new(child))
+ } else {
+ align_union_input(child,
plan.schema()).map(Arc::new)
+ }
+ })
+ .collect::<Result<Vec<_>>>()?;
Ok(Transformed::yes(LogicalPlan::Union(Union {
Review Comment:
Correction after digging further: the input names do matter at the logical
level. When `optimize_projections` prunes union columns, `Union` rebuilds its
schema from its first input, names included (`LogicalPlan::with_new_exprs` /
`recompute_schema`). This query fails in the logical optimizer with `No field
named a` with #27718, and passes with this PR:
```sql
SELECT a FROM (
SELECT a, a2 FROM (VALUES (1, 10)) t(a, a2) WHERE false
UNION ALL (SELECT b, b2 FROM (VALUES (2, 20)) s(b, b2) LIMIT 1)
UNION ALL (SELECT c, c2 FROM (VALUES (3, 30)) u(c, c2) LIMIT 1)
);
```
So aliasing the surviving inputs here is the right fix, and I take back the
suggestion to move this to physical planning. It might be worth adding this
query to `union.slt`.
--
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]