Skip to content

Commit 633222d

Browse files
fix: map remaining union input by position in propagate_empty_relation
When propagate_empty_relation removes all but one input of a Union, it put a Projection over the remaining input with Projection::new_from_schema. That made column references from the union's field names, which come from the first input. If a later input remained and had other names, the projection referred to columns that did not exist, and optimize_projections failed with "No field named ...". Map the remaining input's columns to the union's fields by position and build the projection with Projection::try_new. Closes apache#26516. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6c1727f commit 633222d

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

‎datafusion/optimizer/src/propagate_empty_relation.rs‎

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,21 @@ impl OptimizerRule for PropagateEmptyRelation {
210210
if child.schema().eq(plan.schema()) {
211211
Ok(Transformed::yes(child))
212212
} else {
213+
// The union takes its field names from its first
214+
// input, which may be gone. Map the remaining
215+
// input's columns by position.
216+
let exprs = child
217+
.schema()
218+
.columns()
219+
.into_iter()
220+
.zip(plan.schema().iter())
221+
.map(|(col, (qualifier, field))| {
222+
Expr::Column(col)
223+
.alias_qualified(qualifier.cloned(), field.name())
224+
})
225+
.collect::<Vec<_>>();
213226
Ok(Transformed::yes(LogicalPlan::Projection(
214-
Projection::new_from_schema(
215-
Arc::new(child),
216-
Arc::clone(plan.schema()),
217-
),
227+
Projection::try_new(exprs, Arc::new(child))?,
218228
)))
219229
}
220230
} else {
@@ -439,7 +449,8 @@ mod tests {
439449

440450
let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
441451

442-
let expected = "Projection: a, b, c\n TableScan: test";
452+
let expected =
453+
"Projection: test.a AS a, test.b AS b, test.c AS c\n TableScan: test";
443454
assert_together_optimized_plan(plan, expected, true)
444455
}
445456

@@ -529,7 +540,32 @@ mod tests {
529540

530541
let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
531542

532-
let expected = "Projection: a, b, c\n TableScan: test";
543+
let expected =
544+
"Projection: test.a AS a, test.b AS b, test.c AS c\n TableScan: test";
545+
assert_together_optimized_plan(plan, expected, true)
546+
}
547+
548+
#[test]
549+
fn propagate_union_remaining_input_with_other_names() -> Result<()> {
550+
// Union
551+
// Filter: false <- removed
552+
// Projection: x, y, z <- remains, other names
553+
let left = LogicalPlanBuilder::from(test_table_scan()?)
554+
.filter(lit(false))?
555+
.build()?;
556+
let right = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
557+
.project(vec![
558+
col("a").alias("x"),
559+
col("b").alias("y"),
560+
col("c").alias("z"),
561+
])?
562+
.build()?;
563+
564+
let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
565+
566+
let expected = "Projection: x AS a, y AS b, z AS c\
567+
\n Projection: test2.a AS x, test2.b AS y, test2.c AS z\
568+
\n TableScan: test2";
533569
assert_together_optimized_plan(plan, expected, true)
534570
}
535571

@@ -813,7 +849,7 @@ mod tests {
813849
.union(three)?
814850
.build()?;
815851

816-
let expected = "Projection: a, b, c\
852+
let expected = "Projection: test.a AS a, test.b AS b, test.c AS c\
817853
\n TableScan: test";
818854

819855
assert_together_optimized_plan(plan, expected, true)

‎datafusion/sqllogictest/test_files/union.slt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,21 @@ query TITT
11441144
----
11451145
ns3 4 t3 m
11461146

1147+
# The first input is empty, so only the second input remains.
1148+
# The output must keep the names of the first input.
1149+
# https://github.com/apache/datafusion/issues/26516
1150+
query I
1151+
SELECT 1 WHERE false UNION ALL (SELECT 2 LIMIT 1);
1152+
----
1153+
2
1154+
1155+
query I
1156+
SELECT a FROM (VALUES (1)) t(a) WHERE false
1157+
UNION ALL
1158+
(SELECT b FROM (VALUES (2)) s(b) LIMIT 1);
1159+
----
1160+
2
1161+
11471162
# Config reset
11481163
statement ok
11491164
RESET datafusion.execution.batch_size;

0 commit comments

Comments
 (0)