Skip to content

Commit c2bb15a

Browse files
fix: align non-projection UNION input names with output schema
(cherry picked from commit a11db8d) (cherry picked from commit 023355c)
1 parent 57eec87 commit c2bb15a

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎datafusion/core/tests/dataframe/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7193,7 +7193,7 @@ async fn test_copy_to_preserves_order() -> Result<()> {
71937193
DataSinkExec: sink=CsvSink(file_groups=[])
71947194
SortExec: expr=[column1@0 DESC], preserve_partitioning=[false]
71957195
DataSourceExec: partitions=1, partition_sizes=[1]
7196-
ProjectionExec: expr=[CAST(column1@0 AS UInt64) as count]
7196+
ProjectionExec: expr=[column1@0 as count]
71977197
DataSourceExec: partitions=1, partition_sizes=[1]
71987198
"
71997199
);

‎datafusion/optimizer/src/analyzer/type_coercion.rs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,29 @@ impl<'a> TypeCoercionRewriter<'a> {
315315
Arc::clone(&union_schema),
316316
)?))
317317
}
318-
other_plan => Ok(Arc::new(other_plan)),
318+
other_plan => {
319+
let needs_rename = other_plan
320+
.schema()
321+
.fields()
322+
.iter()
323+
.zip(union_schema.fields())
324+
.any(|(input, output)| input.name() != output.name());
325+
if needs_rename {
326+
let expr = other_plan
327+
.schema()
328+
.columns()
329+
.into_iter()
330+
.map(Expr::Column)
331+
.collect();
332+
Ok(Arc::new(project_with_column_index(
333+
expr,
334+
Arc::new(other_plan),
335+
Arc::clone(&union_schema),
336+
)?))
337+
} else {
338+
Ok(Arc::new(other_plan))
339+
}
340+
}
319341
}
320342
})
321343
.collect::<Result<Vec<_>>>()?;

‎datafusion/optimizer/src/optimize_unions.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ mod tests {
380380
TableScan: table
381381
Projection: id AS table_id, key, value
382382
TableScan: table
383-
TableScan: table
383+
Projection: table.id AS table_id, table.key, table.value
384+
TableScan: table
384385
")
385386
}
386387

‎datafusion/sqllogictest/test_files/limit.slt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,3 +1044,12 @@ physical_plan
10441044

10451045
statement ok
10461046
DROP TABLE t22489;
1047+
1048+
# Limited UNION branches must preserve same-named join columns through Substrait.
1049+
query II rowsort
1050+
(SELECT * FROM (VALUES (1)) AS x(a) CROSS JOIN (VALUES (2)) AS y(a) LIMIT 1)
1051+
UNION ALL
1052+
SELECT * FROM (VALUES (3)) AS x(a) CROSS JOIN (VALUES (4)) AS y(a);
1053+
----
1054+
1 2
1055+
3 4

0 commit comments

Comments
 (0)