Skip to content

Commit d4da80b

Browse files
emgeeealamb
andauthored
Stop copying LogicalPlan and Exprs in EliminateNestedUnion (#10319)
* First pass of #10296 * Update datafusion/optimizer/src/eliminate_nested_union.rs Co-authored-by: Andrew Lamb <[email protected]> * fix formatting * return original expr instead of clone() --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent 63e05fa commit d4da80b

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

datafusion/expr/src/expr_rewriter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn coerce_exprs_for_schema(
250250
_ => expr.cast_to(new_type, src_schema),
251251
}
252252
} else {
253-
Ok(expr.clone())
253+
Ok(expr)
254254
}
255255
})
256256
.collect::<Result<_>>()

datafusion/optimizer/src/eliminate_nested_union.rs

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
//! [`EliminateNestedUnion`]: flattens nested `Union` to a single `Union`
1919
use crate::optimizer::ApplyOrder;
2020
use crate::{OptimizerConfig, OptimizerRule};
21-
use datafusion_common::Result;
21+
use datafusion_common::tree_node::Transformed;
22+
use datafusion_common::{internal_err, Result};
2223
use datafusion_expr::expr_rewriter::coerce_plan_expr_for_schema;
2324
use datafusion_expr::{Distinct, LogicalPlan, Union};
2425
use std::sync::Arc;
@@ -37,49 +38,63 @@ impl EliminateNestedUnion {
3738
impl OptimizerRule for EliminateNestedUnion {
3839
fn try_optimize(
3940
&self,
40-
plan: &LogicalPlan,
41+
_plan: &LogicalPlan,
4142
_config: &dyn OptimizerConfig,
4243
) -> Result<Option<LogicalPlan>> {
44+
internal_err!("Should have called EliminateNestedUnion::rewrite")
45+
}
46+
47+
fn name(&self) -> &str {
48+
"eliminate_nested_union"
49+
}
50+
51+
fn apply_order(&self) -> Option<ApplyOrder> {
52+
Some(ApplyOrder::BottomUp)
53+
}
54+
55+
fn supports_rewrite(&self) -> bool {
56+
true
57+
}
58+
59+
fn rewrite(
60+
&self,
61+
plan: LogicalPlan,
62+
_config: &dyn OptimizerConfig,
63+
) -> Result<Transformed<LogicalPlan>> {
4364
match plan {
4465
LogicalPlan::Union(Union { inputs, schema }) => {
4566
let inputs = inputs
4667
.iter()
4768
.flat_map(extract_plans_from_union)
4869
.collect::<Vec<_>>();
4970

50-
Ok(Some(LogicalPlan::Union(Union {
71+
Ok(Transformed::yes(LogicalPlan::Union(Union {
5172
inputs,
52-
schema: schema.clone(),
73+
schema,
5374
})))
5475
}
55-
LogicalPlan::Distinct(Distinct::All(plan)) => match plan.as_ref() {
56-
LogicalPlan::Union(Union { inputs, schema }) => {
57-
let inputs = inputs
58-
.iter()
59-
.map(extract_plan_from_distinct)
60-
.flat_map(extract_plans_from_union)
61-
.collect::<Vec<_>>();
62-
63-
Ok(Some(LogicalPlan::Distinct(Distinct::All(Arc::new(
64-
LogicalPlan::Union(Union {
65-
inputs,
66-
schema: schema.clone(),
67-
}),
68-
)))))
76+
LogicalPlan::Distinct(Distinct::All(ref nested_plan)) => {
77+
match nested_plan.as_ref() {
78+
LogicalPlan::Union(Union { inputs, schema }) => {
79+
let inputs = inputs
80+
.iter()
81+
.map(extract_plan_from_distinct)
82+
.flat_map(extract_plans_from_union)
83+
.collect::<Vec<_>>();
84+
85+
Ok(Transformed::yes(LogicalPlan::Distinct(Distinct::All(
86+
Arc::new(LogicalPlan::Union(Union {
87+
inputs,
88+
schema: schema.clone(),
89+
})),
90+
))))
91+
}
92+
_ => Ok(Transformed::no(plan)),
6993
}
70-
_ => Ok(None),
71-
},
72-
_ => Ok(None),
94+
}
95+
_ => Ok(Transformed::no(plan)),
7396
}
7497
}
75-
76-
fn name(&self) -> &str {
77-
"eliminate_nested_union"
78-
}
79-
80-
fn apply_order(&self) -> Option<ApplyOrder> {
81-
Some(ApplyOrder::BottomUp)
82-
}
8398
}
8499

85100
fn extract_plans_from_union(plan: &Arc<LogicalPlan>) -> Vec<Arc<LogicalPlan>> {

0 commit comments

Comments
 (0)