Skip to content

Commit 15dfbbd

Browse files
committed
Avoid cloning
1 parent 55c6b74 commit 15dfbbd

File tree

1 file changed

+38
-26
lines changed
  • datafusion/expr/src/logical_plan

1 file changed

+38
-26
lines changed

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,13 @@ impl LogicalPlan {
575575
/// Such as `Projection/Aggregate/Window`
576576
pub fn with_new_exprs(
577577
&self,
578-
expr: Vec<Expr>,
578+
mut expr: Vec<Expr>,
579579
inputs: &[LogicalPlan],
580580
) -> Result<LogicalPlan> {
581581
match self {
582582
LogicalPlan::Projection(Projection { schema, .. }) => {
583583
Ok(LogicalPlan::Projection(Projection::try_new_with_schema(
584-
expr.to_vec(),
584+
expr,
585585
Arc::new(inputs[0].clone()),
586586
schema.clone(),
587587
)?))
@@ -621,7 +621,7 @@ impl LogicalPlan {
621621
}
622622
LogicalPlan::Filter { .. } => {
623623
assert_eq!(1, expr.len());
624-
let predicate = expr[0].clone();
624+
let predicate = expr.pop().unwrap();
625625

626626
// filter predicates should not contain aliased expressions so we remove any aliases
627627
// before this logic was added we would have aliases within filters such as for
@@ -677,12 +677,12 @@ impl LogicalPlan {
677677
}))
678678
}
679679
Partitioning::Hash(_, n) => Ok(LogicalPlan::Repartition(Repartition {
680-
partitioning_scheme: Partitioning::Hash(expr.to_owned(), *n),
680+
partitioning_scheme: Partitioning::Hash(expr, *n),
681681
input: Arc::new(inputs[0].clone()),
682682
})),
683683
Partitioning::DistributeBy(_) => {
684684
Ok(LogicalPlan::Repartition(Repartition {
685-
partitioning_scheme: Partitioning::DistributeBy(expr.to_owned()),
685+
partitioning_scheme: Partitioning::DistributeBy(expr),
686686
input: Arc::new(inputs[0].clone()),
687687
}))
688688
}
@@ -691,21 +691,29 @@ impl LogicalPlan {
691691
window_expr,
692692
schema,
693693
..
694-
}) => Ok(LogicalPlan::Window(Window {
695-
input: Arc::new(inputs[0].clone()),
696-
window_expr: expr[0..window_expr.len()].to_vec(),
697-
schema: schema.clone(),
698-
})),
694+
}) => {
695+
assert_eq!(window_expr.len(), expr.len());
696+
Ok(LogicalPlan::Window(Window {
697+
input: Arc::new(inputs[0].clone()),
698+
window_expr: expr,
699+
schema: schema.clone(),
700+
}))
701+
}
699702
LogicalPlan::Aggregate(Aggregate {
700703
group_expr, schema, ..
701-
}) => Ok(LogicalPlan::Aggregate(Aggregate::try_new_with_schema(
702-
Arc::new(inputs[0].clone()),
703-
expr[0..group_expr.len()].to_vec(),
704-
expr[group_expr.len()..].to_vec(),
705-
schema.clone(),
706-
)?)),
704+
}) => {
705+
// group exprs are the first expressions
706+
let agg_expr = expr.split_off(group_expr.len());
707+
708+
Ok(LogicalPlan::Aggregate(Aggregate::try_new_with_schema(
709+
Arc::new(inputs[0].clone()),
710+
expr,
711+
agg_expr,
712+
schema.clone(),
713+
)?))
714+
}
707715
LogicalPlan::Sort(Sort { fetch, .. }) => Ok(LogicalPlan::Sort(Sort {
708-
expr: expr.to_vec(),
716+
expr,
709717
input: Arc::new(inputs[0].clone()),
710718
fetch: *fetch,
711719
})),
@@ -722,25 +730,29 @@ impl LogicalPlan {
722730
let equi_expr_count = on.len();
723731
assert!(expr.len() >= equi_expr_count);
724732

725-
// The preceding part of expr is equi-exprs,
733+
// Assume that the last expr, if any,
734+
// is the filter_expr (non equality predicate from ON clause)
735+
let filter_expr = if expr.len() > equi_expr_count {
736+
expr.pop()
737+
} else {
738+
None
739+
};
740+
741+
// The first part of expr is equi-exprs,
726742
// and the struct of each equi-expr is like `left-expr = right-expr`.
727-
let new_on:Vec<(Expr,Expr)> = expr.iter().take(equi_expr_count).map(|equi_expr| {
743+
assert_eq!(expr.len(), equi_expr_count);
744+
let new_on:Vec<(Expr,Expr)> = expr.into_iter().map(|equi_expr| {
728745
// SimplifyExpression rule may add alias to the equi_expr.
729746
let unalias_expr = equi_expr.clone().unalias();
730747
if let Expr::BinaryExpr(BinaryExpr { left, op: Operator::Eq, right }) = unalias_expr {
731748
Ok((*left, *right))
732749
} else {
733750
internal_err!(
734-
"The front part expressions should be an binary equiality expression, actual:{equi_expr}"
751+
"The front part expressions should be an binary equality expression, actual:{equi_expr}"
735752
)
736753
}
737754
}).collect::<Result<Vec<(Expr, Expr)>>>()?;
738755

739-
// Assume that the last expr, if any,
740-
// is the filter_expr (non equality predicate from ON clause)
741-
let filter_expr =
742-
(expr.len() > equi_expr_count).then(|| expr[expr.len() - 1].clone());
743-
744756
Ok(LogicalPlan::Join(Join {
745757
left: Arc::new(inputs[0].clone()),
746758
right: Arc::new(inputs[1].clone()),
@@ -849,7 +861,7 @@ impl LogicalPlan {
849861
LogicalPlan::TableScan(ts) => {
850862
assert!(inputs.is_empty(), "{self:?} should have no inputs");
851863
Ok(LogicalPlan::TableScan(TableScan {
852-
filters: expr.to_vec(),
864+
filters: expr,
853865
..ts.clone()
854866
}))
855867
}

0 commit comments

Comments
 (0)