Skip to content

Commit c228f82

Browse files
committed
perf(cube): Perform slightly less Expr copying in optimize_projections
1 parent fe9a1b2 commit c228f82

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

datafusion/optimizer/src/optimize_projections/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn optimize_projections(
147147
// functional dependency.
148148
group_by_reqs
149149
.append(&simplest_groupby_indices)
150-
.get_at_indices(&aggregate.group_expr)
150+
.take_at_indices(aggregate.group_expr)
151151
} else {
152152
aggregate.group_expr
153153
};
@@ -212,7 +212,7 @@ fn optimize_projections(
212212

213213
// Only use window expressions that are absolutely necessary according
214214
// to parent requirements:
215-
let new_window_expr = window_reqs.get_at_indices(&window.window_expr);
215+
let new_window_expr = window_reqs.take_at_indices(window.window_expr);
216216

217217
// Get all the required column indices at the input, either by the
218218
// parent or window expression requirements.
@@ -749,7 +749,7 @@ fn rewrite_projection_given_requirements(
749749
) -> Result<Transformed<LogicalPlan>> {
750750
let Projection { expr, input, .. } = proj;
751751

752-
let exprs_used = indices.get_at_indices(&expr);
752+
let exprs_used = indices.take_at_indices(expr);
753753

754754
let required_indices =
755755
RequiredIndices::new().with_exprs(input.schema(), exprs_used.iter());

datafusion/optimizer/src/optimize_projections/required_indices.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ impl RequiredIndices {
207207
self.indices.iter().map(|&idx| exprs[idx].clone()).collect()
208208
}
209209

210+
/// Same as `get_at_indices` except avoids cloning Exprs.
211+
pub fn take_at_indices(&self, mut exprs: Vec<Expr>) -> Vec<Expr> {
212+
let mut builder = Vec::with_capacity(self.indices.len());
213+
for &index in &self.indices {
214+
// Note that self.indices has no duplicates.
215+
let expr = std::mem::take(&mut exprs[index]);
216+
builder.push(expr);
217+
}
218+
builder
219+
}
220+
210221
/// Generates the required expressions (columns) that reside at `indices` of
211222
/// the given `input_schema`.
212223
pub fn get_required_exprs(&self, input_schema: &DFSchemaRef) -> Vec<Expr> {

0 commit comments

Comments
 (0)