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:
This comment is the key part: the logical plan is already consistent here,
since the `Union` keeps its schema and matches inputs by position. The names
only break when `UnionExec` takes them from its first input. Fixing it in this
rule covers this one path, while other rules that drop or reorder union inputs,
or plans built directly, can still hit the same assertion (#27573 is the same
bug coming from the analyzer).
The single-input branch is different and still needed: there the `Union`
itself goes away, so parents need the projection to keep resolving the union's
names.
I tried aligning the names during physical planning in #27718, which fixes
both cases and leaves logical plans unchanged. WDYT?
--
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]