This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 159ab179ff Minor: remove clones in `coerce_plan_expr_for_schema`
(#12051)
159ab179ff is described below
commit 159ab179ffc33e6aae9bdb9f7bbeab1d0dd41187
Author: Jonah Gao <[email protected]>
AuthorDate: Tue Aug 20 01:07:41 2024 +0800
Minor: remove clones in `coerce_plan_expr_for_schema` (#12051)
---
datafusion/expr/src/expr_rewriter/mod.rs | 12 +++++-------
datafusion/expr/src/logical_plan/builder.rs | 2 +-
datafusion/optimizer/src/analyzer/type_coercion.rs | 5 +++--
datafusion/optimizer/src/eliminate_nested_union.rs | 4 ++--
datafusion/optimizer/src/eliminate_one_union.rs | 2 +-
5 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/datafusion/expr/src/expr_rewriter/mod.rs
b/datafusion/expr/src/expr_rewriter/mod.rs
index 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 types
pub fn coerce_plan_expr_for_schema(
- plan: &LogicalPlan,
+ plan: LogicalPlan,
schema: &DFSchema,
) -> Result<LogicalPlan> {
match plan {
// special case Projection to avoid adding multiple projections
LogicalPlan::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.rs
index 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 term
let 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.rs
index 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.rs
index 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.rs
index 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());
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]