Skip to content

Commit 232c526

Browse files
committed
fix: build union schema with child has same column name but qualifier is different
1 parent f68214d commit 232c526

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

datafusion/core/tests/sql/union.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async fn test_union_upcast_types() -> Result<()> {
111111

112112
let expected_logical_plan = vec![
113113
"Limit: skip=0, fetch=5 [c1:Utf8, c9:Int64]",
114-
" Sort: c9 DESC NULLS FIRST [c1:Utf8, c9:Int64]",
114+
" Sort: aggregate_test_100.c9 DESC NULLS FIRST [c1:Utf8, c9:Int64]",
115115
" Union [c1:Utf8, c9:Int64]",
116116
" Projection: aggregate_test_100.c1, CAST(aggregate_test_100.c9 AS Int64) AS c9 [c1:Utf8, c9:Int64]",
117117
" TableScan: aggregate_test_100 [c1:Utf8, c2:UInt32, c3:Int8, c4:Int16, c5:Int32, c6:Int64, c7:UInt8, c8:UInt16, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8]",

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ pub fn union(left_plan: LogicalPlan, right_plan: LogicalPlan) -> Result<LogicalP
11011101
})?;
11021102

11031103
Ok(DFField::new(
1104-
None,
1104+
left_field.qualifier().map(|x| x.as_ref()),
11051105
left_field.name(),
11061106
data_type,
11071107
nullable,
@@ -1782,4 +1782,24 @@ mod tests {
17821782

17831783
table_scan(Some(table_name), &schema, None)
17841784
}
1785+
1786+
#[test]
1787+
fn test_union_after_join() -> Result<()> {
1788+
let values = vec![vec![lit(1)]];
1789+
1790+
let left = LogicalPlanBuilder::values(values.clone())?
1791+
.alias("left")?
1792+
.build()?;
1793+
let right = LogicalPlanBuilder::values(values)?
1794+
.alias("right")?
1795+
.build()?;
1796+
1797+
let join = LogicalPlanBuilder::from(left).cross_join(right)?.build()?;
1798+
1799+
let _ = LogicalPlanBuilder::from(join.clone())
1800+
.union(join)?
1801+
.build()?;
1802+
1803+
Ok(())
1804+
}
17851805
}

datafusion/optimizer/src/propagate_empty_relation.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ mod tests {
271271

272272
let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
273273

274-
let expected = "Projection: a, b, c\
275-
\n TableScan: test";
274+
let expected = "TableScan: test";
276275
assert_together_optimized_plan_eq(&plan, expected)
277276
}
278277

@@ -362,8 +361,7 @@ mod tests {
362361

363362
let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
364363

365-
let expected = "Projection: a, b, c\
366-
\n TableScan: test";
364+
let expected = "TableScan: test";
367365
assert_together_optimized_plan_eq(&plan, expected)
368366
}
369367

datafusion/sql/tests/integration_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,7 @@ fn union_with_multiply_cols() {
19161916
#[test]
19171917
fn sorted_union_with_different_types_and_group_by() {
19181918
let sql = "SELECT a FROM (select 1 a) x GROUP BY 1 UNION ALL (SELECT a FROM (select 1.1 a) x GROUP BY 1) ORDER BY 1";
1919-
let expected = "Sort: a ASC NULLS LAST\
1919+
let expected = "Sort: x.a ASC NULLS LAST\
19201920
\n Union\
19211921
\n Projection: CAST(x.a AS Float64) AS a\
19221922
\n Aggregate: groupBy=[[x.a]], aggr=[[]]\

0 commit comments

Comments
 (0)