Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion datafusion/optimizer/src/optimize_unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{OptimizerConfig, OptimizerRule};
use datafusion_common::tree_node::Transformed;
use datafusion_common::Result;
use datafusion_expr::expr_rewriter::coerce_plan_expr_for_schema;
use datafusion_expr::{Distinct, LogicalPlan, Union};
use datafusion_expr::{Distinct, LogicalPlan, Projection, Union};
use itertools::Itertools;
use std::sync::Arc;

Expand Down Expand Up @@ -105,6 +105,31 @@ fn extract_plans_from_union(plan: Arc<LogicalPlan>) -> Vec<LogicalPlan> {
.into_iter()
.map(Arc::unwrap_or_clone)
.collect::<Vec<_>>(),
LogicalPlan::Projection(Projection {
Copy link
Contributor

@alamb alamb Nov 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right in understanding this change that it transforms

  Projection 
    Union

to

Union 
  Projection

(aka it pushes the projection to each input of the union?)

Maybe some comments could help future readers

expr,
input,
schema,
..
}) => match Arc::unwrap_or_clone(input) {
LogicalPlan::Union(Union { inputs, .. }) => inputs
.into_iter()
.map(Arc::unwrap_or_clone)
.map(|plan| {
LogicalPlan::Projection(
Projection::try_new_with_schema(
expr.clone(),
Arc::new(plan),
Arc::clone(&schema),
)
.unwrap(),
)
})
.collect::<Vec<_>>(),

plan => vec![LogicalPlan::Projection(
Projection::try_new_with_schema(expr, Arc::new(plan), schema).unwrap(),
)],
},
plan => vec![plan],
}
}
Expand Down Expand Up @@ -331,6 +356,27 @@ mod tests {
")
}

#[test]
fn eliminate_nested_union_in_projection() -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the change in this PR the test fails like

────────────┬───────────────────────────────────────────────────────────────────
    1     1 │ Union
    2     2 │   Projection: id AS table_id, key, value
    3       │-    TableScan: table
    4       │-  Projection: id AS table_id, key, value
    5       │-    TableScan: table
          3 │+    Union
          4 │+      TableScan: table
          5 │+      TableScan: table
    6     6 │   TableScan: table
────────────┴───────────────────────────────────────────────────────────────────

In other words, the plan is left as

        Union
          Projection: id AS table_id, key, value
            Union
              TableScan: table
              TableScan: table
          TableScan: table

let plan_builder = table_scan(Some("table"), &schema(), None)?;

let plan = plan_builder
.clone()
.union(plan_builder.clone().build()?)?
.project(vec![col("id").alias("table_id"), col("key"), col("value")])?
.union(plan_builder.build()?)?
.build()?;

assert_optimized_plan_equal!(plan, @r"
Union
Projection: id AS table_id, key, value
TableScan: table
Projection: id AS table_id, key, value
TableScan: table
TableScan: table
")
}

#[test]
fn eliminate_nested_union_with_type_cast_projection() -> Result<()> {
let table_1 = table_scan(
Expand Down