Apache DataFusion SQL Query Engine
Minor: remove clones in `coerce_plan_expr_for_schema` (#12051)
Jonah Gao159ab179ffc3
diff --git a/datafusion/expr/src/expr_rewriter/mod.rs b/datafusion/expr/src/expr_rewriter/mod.rsindex 32e621350e..c26970cb05 100644--- a/datafusion/expr/src/expr_rewriter/mod.rs+++ b/datafusion/expr/src/expr_rewriter/mod.rs@@ -207,27 +207,25 @@ pub fn strip_outer_reference(expr: Expr) -> Expr {/// Returns plan with expressions coerced to types compatible with/// schema typespub fn coerce_plan_expr_for_schema(- plan: &LogicalPlan,+ plan: LogicalPlan,schema: &DFSchema,) -> Result<LogicalPlan> {match plan {// special case Projection to avoid adding multiple projectionsLogicalPlan::Projection(Projection { expr, input, .. }) => {- let new_exprs =- coerce_exprs_for_schema(expr.clone(), input.schema(), schema)?;- let projection = Projection::try_new(new_exprs, Arc::clone(input))?;+ let new_exprs = coerce_exprs_for_schema(expr, input.schema(), schema)?;+ let projection = Projection::try_new(new_exprs, input)?;Ok(LogicalPlan::Projection(projection))}_ => {let exprs: Vec<Expr> = plan.schema().iter().map(Expr::from).collect();-let new_exprs = coerce_exprs_for_schema(exprs, plan.schema(), schema)?;let add_project = new_exprs.iter().any(|expr| expr.try_as_col().is_none());if add_project {- let projection = Projection::try_new(new_exprs, Arc::new(plan.clone()))?;+ let projection = Projection::try_new(new_exprs, Arc::new(plan))?;Ok(LogicalPlan::Projection(projection))} else {- Ok(plan.clone())+ Ok(plan)}}}diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rsindex aa28c1c192..a96caa03d6 100644--- a/datafusion/expr/src/logical_plan/builder.rs+++ b/datafusion/expr/src/logical_plan/builder.rs@@ -147,7 +147,7 @@ impl LogicalPlanBuilder {}// Ensure that the recursive term has the same field types as the static termlet coerced_recursive_term =- coerce_plan_expr_for_schema(&recursive_term, self.plan.schema())?;+ coerce_plan_expr_for_schema(recursive_term, self.plan.schema())?;Ok(Self::from(LogicalPlan::RecursiveQuery(RecursiveQuery {name,static_term: Arc::new(self.plan.clone()),diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs b/datafusion/optimizer/src/analyzer/type_coercion.rsindex 40efbba6de..7251a95d77 100644--- a/datafusion/optimizer/src/analyzer/type_coercion.rs+++ b/datafusion/optimizer/src/analyzer/type_coercion.rs@@ -181,9 +181,10 @@ impl<'a> TypeCoercionRewriter<'a> {let union_schema = Arc::new(coerce_union_schema(&union_plan.inputs)?);let new_inputs = union_plan.inputs- .iter()+ .into_iter().map(|p| {- let plan = coerce_plan_expr_for_schema(p, &union_schema)?;+ let plan =+ coerce_plan_expr_for_schema(Arc::unwrap_or_clone(p), &union_schema)?;match plan {LogicalPlan::Projection(Projection { expr, input, .. }) => {Ok(Arc::new(project_with_column_index(diff --git a/datafusion/optimizer/src/eliminate_nested_union.rs b/datafusion/optimizer/src/eliminate_nested_union.rsindex 5f41e4f137..5d7895bba4 100644--- a/datafusion/optimizer/src/eliminate_nested_union.rs+++ b/datafusion/optimizer/src/eliminate_nested_union.rs@@ -60,7 +60,7 @@ impl OptimizerRule for EliminateNestedUnion {let inputs = inputs.into_iter().flat_map(extract_plans_from_union)- .map(|plan| coerce_plan_expr_for_schema(&plan, &schema))+ .map(|plan| coerce_plan_expr_for_schema(plan, &schema)).collect::<Result<Vec<_>>>()?;Ok(Transformed::yes(LogicalPlan::Union(Union {@@ -75,7 +75,7 @@ impl OptimizerRule for EliminateNestedUnion {.into_iter().map(extract_plan_from_distinct).flat_map(extract_plans_from_union)- .map(|plan| coerce_plan_expr_for_schema(&plan, &schema))+ .map(|plan| coerce_plan_expr_for_schema(plan, &schema)).collect::<Result<Vec<_>>>()?;Ok(Transformed::yes(LogicalPlan::Distinct(Distinct::All(diff --git a/datafusion/optimizer/src/eliminate_one_union.rs b/datafusion/optimizer/src/eliminate_one_union.rsindex 5e37b8cf7c..43024107c4 100644--- a/datafusion/optimizer/src/eliminate_one_union.rs+++ b/datafusion/optimizer/src/eliminate_one_union.rs@@ -107,7 +107,7 @@ mod tests {#[test]fn eliminate_one_union() -> Result<()> {let table_plan = coerce_plan_expr_for_schema(- &table_scan(Some("table"), &schema(), None)?.build()?,+ table_scan(Some("table"), &schema(), None)?.build()?,&schema().to_dfschema()?,)?;let schema = Arc::clone(table_plan.schema());