Skip to content

Commit f2e2c3b

Browse files
committed
Avoid copies in TypeCoercion
1 parent a98adc7 commit f2e2c3b

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ impl LogicalPlan {
815815
}
816816
}
817817
}
818+
818819
/// Replaces placeholder param values (like `$1`, `$2`) in [`LogicalPlan`]
819820
/// with the specified `param_values`.
820821
///

datafusion/optimizer/src/analyzer/type_coercion.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,30 @@ fn analyze_internal(
106106
};
107107

108108
let preserver = NamePreserver::new(&plan);
109-
Ok(plan
110-
.map_expressions(|expr| {
111-
// ensure aggregate names don't change:
112-
// https://github.com/apache/datafusion/issues/3555
113-
let original_name = preserver.save(&expr)?;
114-
expr.rewrite(&mut expr_rewrite)?
115-
.map_data(|expr| original_name.restore(expr))
116-
})?
117-
// propagate the the transformation information from children
118-
.update_transformed(children_transformed))
109+
let transformed_plan = plan.map_expressions(|expr| {
110+
// ensure aggregate names don't change:
111+
// https://github.com/apache/datafusion/issues/3555
112+
let original_name = preserver.save(&expr)?;
113+
expr.rewrite(&mut expr_rewrite)?
114+
.map_data(|expr| original_name.restore(expr))
115+
})?;
116+
117+
// if any of the expressions were rewritten, we need to recreate the plan to
118+
// recalculate the schema. At the moment this requires a copy
119+
if transformed_plan.transformed || children_transformed {
120+
// TODO avoid this copy
121+
let plan = transformed_plan.data;
122+
let new_inputs = plan
123+
.inputs()
124+
.into_iter()
125+
.map(|input| input.clone())
126+
.collect::<Vec<_>>();
127+
128+
plan.with_new_exprs(plan.expressions(), new_inputs)
129+
.map(Transformed::yes)
130+
} else {
131+
Ok(transformed_plan)
132+
}
119133
}
120134

121135
pub(crate) struct TypeCoercionRewriter {

0 commit comments

Comments
 (0)