gabotechs commented on code in PR #27612:
URL: https://github.com/apache/datafusion/pull/27612#discussion_r4678799971


##########
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:
   At the logical level, it really does not matter the column names, the 
logical planner matches columns by position not by name, so I get the 
impression that this is better solved at the physical layer, rather than the 
logical one. 
   
   Same comment I gave to @alexanderbianchi in 
https://github.com/apache/datafusion/pull/27573#discussion_r4678703107, I think 
it's worth doing this during logical->physical conversion, and indeed it should 
be a pretty simple fix there #27718
   
   



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