Apache DataFusion SQL Query Engine
fix: build union schema with child has same column name but qualifier is different (#5452)
yujie.zhang53a638ef3392
diff --git a/datafusion/core/tests/sql/union.rs b/datafusion/core/tests/sql/union.rsindex 5d16c1dc93..d56a0a39ff 100644--- a/datafusion/core/tests/sql/union.rs+++ b/datafusion/core/tests/sql/union.rs@@ -111,7 +111,7 @@ async fn test_union_upcast_types() -> Result<()> {let expected_logical_plan = vec!["Limit: skip=0, fetch=5 [c1:Utf8, c9:Int64]",- " Sort: c9 DESC NULLS FIRST [c1:Utf8, c9:Int64]",+ " Sort: aggregate_test_100.c9 DESC NULLS FIRST [c1:Utf8, c9:Int64]"," Union [c1:Utf8, c9:Int64]"," Projection: aggregate_test_100.c1, CAST(aggregate_test_100.c9 AS Int64) AS c9 [c1:Utf8, c9:Int64]"," 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]",diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rsindex 775818e990..0461a1d858 100644--- a/datafusion/expr/src/logical_plan/builder.rs+++ b/datafusion/expr/src/logical_plan/builder.rs@@ -1098,7 +1098,7 @@ pub fn union(left_plan: LogicalPlan, right_plan: LogicalPlan) -> Result<LogicalP})?;Ok(DFField::new(- None,+ left_field.qualifier().map(|x| x.as_ref()),left_field.name(),data_type,nullable,@@ -1779,4 +1779,24 @@ mod tests {table_scan(Some(table_name), &schema, None)}++ #[test]+ fn test_union_after_join() -> Result<()> {+ let values = vec![vec![lit(1)]];++ let left = LogicalPlanBuilder::values(values.clone())?+ .alias("left")?+ .build()?;+ let right = LogicalPlanBuilder::values(values)?+ .alias("right")?+ .build()?;++ let join = LogicalPlanBuilder::from(left).cross_join(right)?.build()?;++ let _ = LogicalPlanBuilder::from(join.clone())+ .union(join)?+ .build()?;++ Ok(())+ }}diff --git a/datafusion/optimizer/src/propagate_empty_relation.rs b/datafusion/optimizer/src/propagate_empty_relation.rsindex 4e4280ee80..01e16058ec 100644--- a/datafusion/optimizer/src/propagate_empty_relation.rs+++ b/datafusion/optimizer/src/propagate_empty_relation.rs@@ -271,8 +271,7 @@ mod tests {let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;- let expected = "Projection: a, b, c\- \n TableScan: test";+ let expected = "TableScan: test";assert_together_optimized_plan_eq(&plan, expected)}@@ -362,8 +361,7 @@ mod tests {let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;- let expected = "Projection: a, b, c\- \n TableScan: test";+ let expected = "TableScan: test";assert_together_optimized_plan_eq(&plan, expected)}diff --git a/datafusion/sql/tests/integration_test.rs b/datafusion/sql/tests/integration_test.rsindex 44c0559ef3..71f5bf05e9 100644--- a/datafusion/sql/tests/integration_test.rs+++ b/datafusion/sql/tests/integration_test.rs@@ -1916,7 +1916,7 @@ fn union_with_multiply_cols() {#[test]fn sorted_union_with_different_types_and_group_by() {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";- let expected = "Sort: a ASC NULLS LAST\+ let expected = "Sort: x.a ASC NULLS LAST\\n Union\\n Projection: CAST(x.a AS Float64) AS a\\n Aggregate: groupBy=[[x.a]], aggr=[[]]\