Skip to content

Commit 47df15a

Browse files
committed
Remove Expr clones from SortExprs
1 parent 2e52580 commit 47df15a

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

datafusion/core/tests/user_defined/user_defined_plan.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ use datafusion::{
9797
use datafusion_common::config::ConfigOptions;
9898
use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode};
9999
use datafusion_common::ScalarValue;
100-
use datafusion_expr::tree_node::replace_sort_expression;
101100
use datafusion_expr::{FetchType, Projection, SortExpr};
102101
use datafusion_optimizer::optimizer::ApplyOrder;
103102
use datafusion_optimizer::AnalyzerRule;
@@ -440,7 +439,7 @@ impl UserDefinedLogicalNodeCore for TopKPlanNode {
440439
Ok(Self {
441440
k: self.k,
442441
input: inputs.swap_remove(0),
443-
expr: replace_sort_expression(self.expr.clone(), exprs.swap_remove(0)),
442+
expr: self.expr.with_expr(exprs.swap_remove(0)),
444443
})
445444
}
446445

datafusion/expr/src/expr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,14 @@ impl Sort {
629629
nulls_first: !self.nulls_first,
630630
}
631631
}
632+
633+
pub fn with_expr(&self, expr: Expr) -> Self {
634+
Self {
635+
expr,
636+
asc: self.asc,
637+
nulls_first: self.nulls_first,
638+
}
639+
}
632640
}
633641

634642
impl Display for Sort {

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ use indexmap::IndexSet;
5656

5757
// backwards compatibility
5858
use crate::display::PgJsonVisitor;
59-
use crate::tree_node::replace_sort_expressions;
6059
pub use datafusion_common::display::{PlanType, StringifiedPlan, ToStringifiedPlan};
6160
pub use datafusion_common::{JoinConstraint, JoinType};
6261

@@ -866,7 +865,11 @@ impl LogicalPlan {
866865
}) => {
867866
let input = self.only_input(inputs)?;
868867
Ok(LogicalPlan::Sort(Sort {
869-
expr: replace_sort_expressions(sort_expr.clone(), expr),
868+
expr: expr
869+
.into_iter()
870+
.zip(sort_expr.iter())
871+
.map(|(expr, sort)| sort.with_expr(expr))
872+
.collect(),
870873
input: Arc::new(input),
871874
fetch: *fetch,
872875
}))

datafusion/expr/src/tree_node.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -408,29 +408,9 @@ pub fn transform_sort_option_vec<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
408408
/// Transforms an vector of sort expressions by applying the provided closure `f`.
409409
pub fn transform_sort_vec<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
410410
sorts: Vec<Sort>,
411-
mut f: &mut F,
411+
f: &mut F,
412412
) -> Result<Transformed<Vec<Sort>>> {
413-
Ok(sorts
414-
.iter()
415-
.map(|sort| sort.expr.clone())
416-
.map_until_stop_and_collect(&mut f)?
417-
.update_data(|transformed_exprs| {
418-
replace_sort_expressions(sorts, transformed_exprs)
419-
}))
420-
}
421-
422-
pub fn replace_sort_expressions(sorts: Vec<Sort>, new_expr: Vec<Expr>) -> Vec<Sort> {
423-
assert_eq!(sorts.len(), new_expr.len());
424-
sorts
425-
.into_iter()
426-
.zip(new_expr)
427-
.map(|(sort, expr)| replace_sort_expression(sort, expr))
428-
.collect()
429-
}
430-
431-
pub fn replace_sort_expression(sort: Sort, new_expr: Expr) -> Sort {
432-
Sort {
433-
expr: new_expr,
434-
..sort
435-
}
413+
sorts.into_iter().map_until_stop_and_collect(|s| {
414+
Ok(f(s.expr)?.update_data(|e| Sort { expr: e, ..s }))
415+
})
436416
}

datafusion/optimizer/src/common_subexpr_eliminate.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ use datafusion_expr::expr::{Alias, ScalarFunction};
3434
use datafusion_expr::logical_plan::{
3535
Aggregate, Filter, LogicalPlan, Projection, Sort, Window,
3636
};
37-
use datafusion_expr::tree_node::replace_sort_expressions;
38-
use datafusion_expr::{col, BinaryExpr, Case, Expr, Operator};
37+
use datafusion_expr::{col, BinaryExpr, Case, Expr, Operator, SortExpr};
3938

4039
const CSE_PREFIX: &str = "__common_expr";
4140

@@ -91,19 +90,31 @@ impl CommonSubexprEliminate {
9190
.map(LogicalPlan::Projection)
9291
})
9392
}
93+
9494
fn try_optimize_sort(
9595
&self,
9696
sort: Sort,
9797
config: &dyn OptimizerConfig,
9898
) -> Result<Transformed<LogicalPlan>> {
9999
let Sort { expr, input, fetch } = sort;
100100
let input = Arc::unwrap_or_clone(input);
101-
let sort_expressions = expr.iter().map(|sort| sort.expr.clone()).collect();
101+
let (sort_expressions, sort_params): (Vec<_>, Vec<(_, _)>) = expr
102+
.into_iter()
103+
.map(|sort| (sort.expr, (sort.asc, sort.nulls_first)))
104+
.unzip();
102105
let new_sort = self
103106
.try_unary_plan(sort_expressions, input, config)?
104107
.update_data(|(new_expr, new_input)| {
105108
LogicalPlan::Sort(Sort {
106-
expr: replace_sort_expressions(expr, new_expr),
109+
expr: new_expr
110+
.into_iter()
111+
.zip(sort_params)
112+
.map(|(expr, (asc, nulls_first))| SortExpr {
113+
expr,
114+
asc,
115+
nulls_first,
116+
})
117+
.collect(),
107118
input: Arc::new(new_input),
108119
fetch,
109120
})

0 commit comments

Comments
 (0)