Skip to content

Commit 48cc4c8

Browse files
authored
Optimize planning for projected nested union (#18713)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - part of #17261. ## Rationale for this change as explained in #17261 (comment) and discussion in the above mentioned issue <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? Projected Nested Union are now merged with their parent union. <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Tested it by Github CI. Works for the scenario discussed in the above mentioned issue. ``` > explain analyze (SELECT c0, 1 as c1, 2 as c2, 3 as c3, 4 as c4, 5 as c5, 6 as c6, 7 as c7, 8 as c8, 9 as c9 FROM t1 ORDER BY c0) UNION ALL (SELECT 0 as c0, c1, 2 as c2, 3 as c3, 4 as c4, 5 as c5, 6 as c6, 7 as c7, 8 as c8, 9 as c9 FROM t1 ORDER BY c1) UNION ALL (SELECT 0 as c0, 1 as c1, c2, 3 as c3, 4 as c4, 5 as c5, 6 as c6, 7 as c7, 8 as c8, 9 as c9 FROM t1 ORDER BY c2) UNION ALL (SELECT 0 as c0, 1 as c1, 2 as c2, c3, 4 as c4, 5 as c5, 6 as c6, 7 as c7, 8 as c8, 9 as c9 FROM t1 ORDER BY c3) UNION ALL (SELECT 0 as c0, 1 as c1, 2 as c2, 3 as c3, c4, 5 as c5, 6 as c6, 7 as c7, 8 as c8, 9 as c9 FROM t1 ORDER BY c4) UNION ALL (SELECT 0 as c0, 1 as c1, 2 as c2, 3 as c3, 4 as c4, c5, 6 as c6, 7 as c7, 8 as c8, 9 as c9 FROM t1 ORDER BY c5) UNION ALL (SELECT 0 as c0, 1 as c1, 2 as c2, 3 as c3, 4 as c4, 5 as c5, c6, 7 as c7, 8 as c8, 9 as c9 FROM t1 ORDER BY c6) UNION ALL (SELECT 0 as c0, 1 as c1, 2 as c2, 3 as c3, 4 as c4, 5 as c5, 6 as c6, c7, 8 as c8, 9 as c9 FROM t1 ORDER BY c7) UNION ALL (SELECT 0 as c0, 1 as c1, 2 as c2, 3 as c3, 4 as c4, 5 as c5, 6 as c6, 7 as c7, c8, 9 as c9 FROM t1 ORDER BY c8) UNION ALL (SELECT 0 as c0, 1 as c1, 2 as c2, 3 as c3, 4 as c4, 5 as c5, 6 as c6, 7 as c7, 8 as c8, c9 FROM t1 ORDER BY c9) ORDER BY c0, c1, c2, c3, c4, c5, c6, c7, c8, c9; +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | plan_type | plan | +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Plan with Metrics | SortPreservingMergeExec: [c0@0 ASC NULLS LAST, c1@1 ASC NULLS LAST, c2@2 ASC NULLS LAST, c3@3 ASC NULLS LAST, c4@4 ASC NULLS LAST, c5@5 ASC NULLS LAST, c6@6 ASC NULLS LAST, c7@7 ASC NULLS LAST, c8@8 ASC NULLS LAST, c9@9 ASC NULLS LAST], metrics=[output_rows=0, elapsed_compute=3.817µs, output_bytes=0.0 B, output_batches=0] | | | UnionExec, metrics=[output_rows=0, elapsed_compute=345.775µs, output_bytes=0.0 B, output_batches=0] | | | SortExec: expr=[c0@0 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(c0@0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c1@1 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(c1@0 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c2@2 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(c2@0 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c3@3 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(c3@0 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c4@4 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(c4@0 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c5@5 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(c5@0 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c6@6 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(c6@0 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c7@7 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(c7@0 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c8@8 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(c8@0 AS Decimal128(20, 0)) as c8, CAST(9 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | SortExec: expr=[c9@9 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0, spill_count=0, spilled_bytes=0.0 B, spilled_rows=0, batches_split=0] | | | ProjectionExec: expr=[CAST(0 AS Decimal128(20, 0)) as c0, CAST(1 AS Decimal128(20, 0)) as c1, CAST(2 AS Decimal128(20, 0)) as c2, CAST(3 AS Decimal128(20, 0)) as c3, CAST(4 AS Decimal128(20, 0)) as c4, CAST(5 AS Decimal128(20, 0)) as c5, CAST(6 AS Decimal128(20, 0)) as c6, CAST(7 AS Decimal128(20, 0)) as c7, CAST(8 AS Decimal128(20, 0)) as c8, CAST(c9@0 AS Decimal128(20, 0)) as c9], metrics=[output_rows=0, elapsed_compute=1ns, output_bytes=0.0 B, output_batches=0] | | | DataSourceExec: partitions=1, partition_sizes=[0], metrics=[] | | | | +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ ``` <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? Yes? logical plans for some queries should be different(and better) now. No syntax changes. <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent c83b9d7 commit 48cc4c8

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

datafusion/optimizer/src/optimize_unions.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{OptimizerConfig, OptimizerRule};
2121
use datafusion_common::tree_node::Transformed;
2222
use datafusion_common::Result;
2323
use datafusion_expr::expr_rewriter::coerce_plan_expr_for_schema;
24-
use datafusion_expr::{Distinct, LogicalPlan, Union};
24+
use datafusion_expr::{Distinct, LogicalPlan, Projection, Union};
2525
use itertools::Itertools;
2626
use std::sync::Arc;
2727

@@ -105,6 +105,38 @@ fn extract_plans_from_union(plan: Arc<LogicalPlan>) -> Vec<LogicalPlan> {
105105
.into_iter()
106106
.map(Arc::unwrap_or_clone)
107107
.collect::<Vec<_>>(),
108+
// While unnesting, unwrap a Projection whose input is a nested Union,
109+
// flatten the inner Union, and push the same Projection down onto
110+
// each of the nested Union’s children.
111+
//
112+
// Example:
113+
// Union { Projection { Union { plan1, plan2 } }, plan3 }
114+
// => Union { Projection { plan1 }, Projection { plan2 }, plan3 }
115+
LogicalPlan::Projection(Projection {
116+
expr,
117+
input,
118+
schema,
119+
..
120+
}) => match Arc::unwrap_or_clone(input) {
121+
LogicalPlan::Union(Union { inputs, .. }) => inputs
122+
.into_iter()
123+
.map(Arc::unwrap_or_clone)
124+
.map(|plan| {
125+
LogicalPlan::Projection(
126+
Projection::try_new_with_schema(
127+
expr.clone(),
128+
Arc::new(plan),
129+
Arc::clone(&schema),
130+
)
131+
.unwrap(),
132+
)
133+
})
134+
.collect::<Vec<_>>(),
135+
136+
plan => vec![LogicalPlan::Projection(
137+
Projection::try_new_with_schema(expr, Arc::new(plan), schema).unwrap(),
138+
)],
139+
},
108140
plan => vec![plan],
109141
}
110142
}
@@ -331,6 +363,27 @@ mod tests {
331363
")
332364
}
333365

366+
#[test]
367+
fn eliminate_nested_union_in_projection() -> Result<()> {
368+
let plan_builder = table_scan(Some("table"), &schema(), None)?;
369+
370+
let plan = plan_builder
371+
.clone()
372+
.union(plan_builder.clone().build()?)?
373+
.project(vec![col("id").alias("table_id"), col("key"), col("value")])?
374+
.union(plan_builder.build()?)?
375+
.build()?;
376+
377+
assert_optimized_plan_equal!(plan, @r"
378+
Union
379+
Projection: id AS table_id, key, value
380+
TableScan: table
381+
Projection: id AS table_id, key, value
382+
TableScan: table
383+
TableScan: table
384+
")
385+
}
386+
334387
#[test]
335388
fn eliminate_nested_union_with_type_cast_projection() -> Result<()> {
336389
let table_1 = table_scan(

0 commit comments

Comments
 (0)