gabotechs commented on code in PR #27573:
URL: https://github.com/apache/datafusion/pull/27573#discussion_r4679026783
##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -452,7 +452,29 @@ impl<'a> TypeCoercionRewriter<'a> {
Arc::clone(&union_schema),
)?))
}
- other_plan => Ok(Arc::new(other_plan)),
+ other_plan => {
+ let needs_rename = other_plan
+ .schema()
+ .fields()
+ .iter()
+ .zip(union_schema.fields())
+ .any(|(input, output)| input.name() !=
output.name());
+ if needs_rename {
+ let expr = other_plan
+ .schema()
+ .columns()
+ .into_iter()
+ .map(Expr::Column)
+ .collect();
+ Ok(Arc::new(project_with_column_index(
+ expr,
+ Arc::new(other_plan),
+ Arc::clone(&union_schema),
+ )?))
+ } else {
+ Ok(Arc::new(other_plan))
+ }
+ }
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`). For example, this fails in the logical optimizer with `No
field named a` even with #27718:
```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 keeping the first input's names in line with the union schema has to
happen in the logical plan, and #27718 alone is not enough. I take back the
suggestion to move this to physical planning.
--
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]