Apache DataFusion SQL Query Engine
fix: union schema (#2334)
George Andronchik7304719bb483
diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rsindex 895e5bc1e7..0f544ca89e 100644--- a/datafusion/core/src/execution/context.rs+++ b/datafusion/core/src/execution/context.rs@@ -2090,6 +2090,44 @@ mod tests {Ok(())}+ #[tokio::test]+ async fn union_test() -> Result<()> {+ let ctx = SessionContext::with_config(+ SessionConfig::new().with_information_schema(true),+ );++ let result = ctx+ .sql("SELECT 1 A UNION ALL SELECT 2")+ .await+ .unwrap()+ .collect()+ .await+ .unwrap();++ let expected = vec!["+---+", "| a |", "+---+", "| 1 |", "| 2 |", "+---+"];+ assert_batches_eq!(expected, &result);++ let result = ctx+ .sql("SELECT 1 UNION SELECT 2")+ .await+ .unwrap()+ .collect()+ .await+ .unwrap();++ let expected = vec![+ "+----------+",+ "| Int64(1) |",+ "+----------+",+ "| 1 |",+ "| 2 |",+ "+----------+",+ ];+ assert_batches_eq!(expected, &result);++ Ok(())+ }+struct MyPhysicalPlanner {}#[async_trait]diff --git a/datafusion/core/src/logical_plan/builder.rs b/datafusion/core/src/logical_plan/builder.rsindex 4f00bec60a..1fbb1f5f9d 100644--- a/datafusion/core/src/logical_plan/builder.rs+++ b/datafusion/core/src/logical_plan/builder.rs@@ -1055,19 +1055,31 @@ pub fn union_with_alias(right_plan: LogicalPlan,alias: Option<String>,) -> Result<LogicalPlan> {- let inputs = vec![left_plan, right_plan]+ let union_schema = left_plan.schema().clone();+ let inputs_iter = vec![left_plan, right_plan].into_iter().flat_map(|p| match p {LogicalPlan::Union(Union { inputs, .. }) => inputs,x => vec![x],- })+ });++ inputs_iter+ .clone()+ .skip(1)+ .try_for_each(|input_plan| -> Result<()> {+ union_schema.check_arrow_schema_type_compatible(+ &((**input_plan.schema()).clone().into()),+ )+ })?;++ let inputs = inputs_iter.map(|p| match p {LogicalPlan::Projection(Projection {- expr,- input,- schema,- alias,- }) => project_with_column_index_alias(expr, input, schema, alias).unwrap(),+ expr, input, alias, ..+ }) => {+ project_with_column_index_alias(expr, input, union_schema.clone(), alias)+ .unwrap()+ }x => x,}).collect::<Vec<_>>();@@ -1081,15 +1093,6 @@ pub fn union_with_alias(None => union_schema.strip_qualifiers(),});- inputs- .iter()- .skip(1)- .try_for_each(|input_plan| -> Result<()> {- union_schema.check_arrow_schema_type_compatible(- &((**input_plan.schema()).clone().into()),- )- })?;-Ok(LogicalPlan::Union(Union {inputs,schema: union_schema,