Skip to content

Apache DataFusion SQL Query Engine

fix: union schema fix (#2688)

George Andronchikf37dd7cd231a
diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rs
index 509b47909b..8d58241eaa 100644
--- a/datafusion/expr/src/logical_plan/builder.rs
+++ b/datafusion/expr/src/logical_plan/builder.rs
@@ -866,7 +866,7 @@ pub fn project_with_column_index_alias(
.map(|(i, e)| match e {
ignore_alias @ Expr::Alias { .. } => ignore_alias,
ignore_col @ Expr::Column { .. } => ignore_col,
- x => x.alias(format!("column{}", i).as_str()),
+ x => x.alias(schema.field(i).name()),
})
.collect::<Vec<_>>();
Ok(LogicalPlan::Projection(Projection {
diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rs
index dff05babbd..3431578758 100644
--- a/datafusion/sql/src/planner.rs
+++ b/datafusion/sql/src/planner.rs
@@ -3987,9 +3987,9 @@ mod tests {
fn union_values_with_no_alias() {
let sql = "SELECT 1, 2 UNION ALL SELECT 3, 4";
let expected = "Union\
- \n Projection: Int64(1) AS column0, Int64(2) AS column1\
+ \n Projection: Int64(1) AS Int64(1), Int64(2) AS Int64(2)\
\n EmptyRelation\
- \n Projection: Int64(3) AS column0, Int64(4) AS column1\
+ \n Projection: Int64(3) AS Int64(1), Int64(4) AS Int64(2)\
\n EmptyRelation";
quick_test(sql, expected);
}