This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 53a638ef3 fix: build union schema with child has same column name but 
qualifier is different (#5452)
53a638ef3 is described below

commit 53a638ef3392e4217c6dfa5de5b90ac9b4153279
Author: yujie.zhang <[email protected]>
AuthorDate: Sun Mar 5 04:07:34 2023 +0800

    fix: build union schema with child has same column name but qualifier is 
different (#5452)
---
 datafusion/core/tests/sql/union.rs                 |  2 +-
 datafusion/expr/src/logical_plan/builder.rs        | 22 +++++++++++++++++++++-
 .../optimizer/src/propagate_empty_relation.rs      |  6 ++----
 datafusion/sql/tests/integration_test.rs           |  2 +-
 4 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/datafusion/core/tests/sql/union.rs 
b/datafusion/core/tests/sql/union.rs
index 5d16c1dc9..d56a0a39f 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.rs
index 775818e99..0461a1d85 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.rs
index 4e4280ee8..01e16058e 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.rs
index 44c0559ef..71f5bf05e 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=[[]]\

Reply via email to