You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by tu...@apache.org on 2022/12/16 11:26:46 UTC

[arrow-datafusion] branch master updated: Make LogicalPlanBuilder consuming (#4622) (#4632)

This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/master by this push:
     new 2e9eea4f6 Make LogicalPlanBuilder consuming (#4622) (#4632)
2e9eea4f6 is described below

commit 2e9eea4f66bd64b11b99668561f2d277b4fe99da
Author: Raphael Taylor-Davies <17...@users.noreply.github.com>
AuthorDate: Fri Dec 16 11:26:40 2022 +0000

    Make LogicalPlanBuilder consuming (#4622) (#4632)
---
 datafusion/core/src/dataframe.rs                   |   2 +-
 datafusion/expr/src/logical_plan/builder.rs        | 150 ++++++++++-----------
 datafusion/expr/src/utils.rs                       |   2 +-
 .../optimizer/src/common_subexpr_eliminate.rs      |   2 +-
 .../optimizer/src/decorrelate_where_exists.rs      |   2 +-
 datafusion/optimizer/src/decorrelate_where_in.rs   |   2 +-
 datafusion/optimizer/src/eliminate_cross_join.rs   |  64 ++++-----
 datafusion/optimizer/src/eliminate_limit.rs        |   2 +-
 datafusion/optimizer/src/eliminate_outer_join.rs   |  10 +-
 datafusion/optimizer/src/filter_null_join_keys.rs  |   4 +-
 .../optimizer/src/propagate_empty_relation.rs      |   4 +-
 datafusion/optimizer/src/push_down_filter.rs       |  37 ++---
 datafusion/optimizer/src/push_down_limit.rs        |  30 ++---
 datafusion/optimizer/src/push_down_projection.rs   |   6 +-
 .../optimizer/src/scalar_subquery_to_join.rs       |   4 +-
 datafusion/proto/src/logical_plan.rs               |   6 +-
 datafusion/sql/src/planner.rs                      |  33 ++---
 17 files changed, 181 insertions(+), 179 deletions(-)

diff --git a/datafusion/core/src/dataframe.rs b/datafusion/core/src/dataframe.rs
index 5e615be60..153c59c56 100644
--- a/datafusion/core/src/dataframe.rs
+++ b/datafusion/core/src/dataframe.rs
@@ -351,7 +351,7 @@ impl DataFrame {
     ) -> Result<DataFrame> {
         let plan = LogicalPlanBuilder::from(self.plan)
             .join(
-                &right.plan,
+                right.plan,
                 join_type,
                 (left_cols.to_vec(), right_cols.to_vec()),
                 filter,
diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rs
index 0b69d0157..2c16a3ca7 100644
--- a/datafusion/expr/src/logical_plan/builder.rs
+++ b/datafusion/expr/src/logical_plan/builder.rs
@@ -86,7 +86,7 @@ pub const UNNAMED_TABLE: &str = "?table?";
 /// # Ok(())
 /// # }
 /// ```
-#[derive(Debug)]
+#[derive(Debug, Clone)]
 pub struct LogicalPlanBuilder {
     plan: LogicalPlan,
 }
@@ -267,27 +267,27 @@ impl LogicalPlanBuilder {
     }
     /// Apply a projection without alias.
     pub fn project(
-        &self,
+        self,
         expr: impl IntoIterator<Item = impl Into<Expr>>,
     ) -> Result<Self> {
-        Ok(Self::from(project(self.plan.clone(), expr)?))
+        Ok(Self::from(project(self.plan, expr)?))
     }
 
     /// Apply a filter
-    pub fn filter(&self, expr: impl Into<Expr>) -> Result<Self> {
+    pub fn filter(self, expr: impl Into<Expr>) -> Result<Self> {
         let expr = normalize_col(expr.into(), &self.plan)?;
         Ok(Self::from(LogicalPlan::Filter(Filter::try_new(
             expr,
-            Arc::new(self.plan.clone()),
+            Arc::new(self.plan),
         )?)))
     }
 
     /// Make a builder for a prepare logical plan from the builder's plan
-    pub fn prepare(&self, name: String, data_types: Vec<DataType>) -> Result<Self> {
+    pub fn prepare(self, name: String, data_types: Vec<DataType>) -> Result<Self> {
         Ok(Self::from(LogicalPlan::Prepare(Prepare {
             name,
             data_types,
-            input: Arc::new(self.plan.clone()),
+            input: Arc::new(self.plan),
         })))
     }
 
@@ -297,17 +297,17 @@ impl LogicalPlanBuilder {
     ///
     /// `fetch` - Maximum number of rows to fetch, after skipping `skip` rows,
     ///          if specified.
-    pub fn limit(&self, skip: usize, fetch: Option<usize>) -> Result<Self> {
+    pub fn limit(self, skip: usize, fetch: Option<usize>) -> Result<Self> {
         Ok(Self::from(LogicalPlan::Limit(Limit {
             skip,
             fetch,
-            input: Arc::new(self.plan.clone()),
+            input: Arc::new(self.plan),
         })))
     }
 
     /// Apply an alias
-    pub fn alias(&self, alias: impl Into<String>) -> Result<Self> {
-        Ok(Self::from(subquery_alias(&self.plan, alias)?))
+    pub fn alias(self, alias: impl Into<String>) -> Result<Self> {
+        Ok(Self::from(subquery_alias(self.plan, alias)?))
     }
 
     /// Add missing sort columns to all downstream projection
@@ -353,7 +353,7 @@ impl LogicalPlanBuilder {
 
     /// Apply a sort
     pub fn sort(
-        &self,
+        self,
         exprs: impl IntoIterator<Item = impl Into<Expr>> + Clone,
     ) -> Result<Self> {
         let exprs = rewrite_sort_cols_by_aggs(exprs, &self.plan)?;
@@ -380,17 +380,11 @@ impl LogicalPlanBuilder {
         if missing_cols.is_empty() {
             return Ok(Self::from(LogicalPlan::Sort(Sort {
                 expr: normalize_cols(exprs, &self.plan)?,
-                input: Arc::new(self.plan.clone()),
+                input: Arc::new(self.plan),
                 fetch: None,
             })));
         }
 
-        let plan = Self::add_missing_columns(self.plan.clone(), &missing_cols)?;
-        let sort_plan = LogicalPlan::Sort(Sort {
-            expr: normalize_cols(exprs, &plan)?,
-            input: Arc::new(plan.clone()),
-            fetch: None,
-        });
         // remove pushed down sort columns
         let new_expr = schema
             .fields()
@@ -398,6 +392,13 @@ impl LogicalPlanBuilder {
             .map(|f| Expr::Column(f.qualified_column()))
             .collect();
 
+        let plan = Self::add_missing_columns(self.plan, &missing_cols)?;
+        let sort_plan = LogicalPlan::Sort(Sort {
+            expr: normalize_cols(exprs, &plan)?,
+            input: Arc::new(plan),
+            fetch: None,
+        });
+
         Ok(Self::from(LogicalPlan::Projection(Projection::try_new(
             new_expr,
             Arc::new(sort_plan),
@@ -405,17 +406,16 @@ impl LogicalPlanBuilder {
     }
 
     /// Apply a union, preserving duplicate rows
-    pub fn union(&self, plan: LogicalPlan) -> Result<Self> {
-        Ok(Self::from(union(self.plan.clone(), plan)?))
+    pub fn union(self, plan: LogicalPlan) -> Result<Self> {
+        Ok(Self::from(union(self.plan, plan)?))
     }
 
     /// Apply a union, removing duplicate rows
-    pub fn union_distinct(&self, plan: LogicalPlan) -> Result<Self> {
+    pub fn union_distinct(self, plan: LogicalPlan) -> Result<Self> {
         // unwrap top-level Distincts, to avoid duplication
-        let left_plan = self.plan.clone();
-        let left_plan: LogicalPlan = match left_plan {
+        let left_plan: LogicalPlan = match self.plan {
             LogicalPlan::Distinct(Distinct { input }) => (*input).clone(),
-            _ => left_plan,
+            _ => self.plan,
         };
         let right_plan: LogicalPlan = match plan {
             LogicalPlan::Distinct(Distinct { input }) => (*input).clone(),
@@ -428,9 +428,9 @@ impl LogicalPlanBuilder {
     }
 
     /// Apply deduplication: Only distinct (different) values are returned)
-    pub fn distinct(&self) -> Result<Self> {
+    pub fn distinct(self) -> Result<Self> {
         Ok(Self::from(LogicalPlan::Distinct(Distinct {
-            input: Arc::new(self.plan.clone()),
+            input: Arc::new(self.plan),
         })))
     }
 
@@ -440,8 +440,8 @@ impl LogicalPlanBuilder {
     /// down to any of join inputs.
     /// In case of outer join, filter applied to only matched rows.
     pub fn join(
-        &self,
-        right: &LogicalPlan,
+        self,
+        right: LogicalPlan,
         join_type: JoinType,
         join_keys: (Vec<impl Into<Column>>, Vec<impl Into<Column>>),
         filter: Option<Expr>,
@@ -463,8 +463,8 @@ impl LogicalPlanBuilder {
     /// Apply a join with on constraint and specified null equality
     /// If null_equals_null is true then null == null, else null != null
     pub fn join_detailed(
-        &self,
-        right: &LogicalPlan,
+        self,
+        right: LogicalPlan,
         join_type: JoinType,
         join_keys: (Vec<impl Into<Column>>, Vec<impl Into<Column>>),
         filter: Option<Expr>,
@@ -501,7 +501,7 @@ impl LogicalPlanBuilder {
                                 (Ok(_), _, _, Ok(_)) => (Ok(l), Ok(r)),
                                 _ => (
                                     Self::normalize(&self.plan, l),
-                                    Self::normalize(right, r),
+                                    Self::normalize(&right, r),
                                 ),
                             }
                         }
@@ -512,11 +512,11 @@ impl LogicalPlanBuilder {
                                 right.schema().field_with_qualified_name(lr, &l.name);
 
                             match (l_is_left, l_is_right) {
-                                (Ok(_), _) => (Ok(l), Self::normalize(right, r)),
+                                (Ok(_), _) => (Ok(l), Self::normalize(&right, r)),
                                 (_, Ok(_)) => (Self::normalize(&self.plan, r), Ok(l)),
                                 _ => (
                                     Self::normalize(&self.plan, l),
-                                    Self::normalize(right, r),
+                                    Self::normalize(&right, r),
                                 ),
                             }
                         }
@@ -527,11 +527,11 @@ impl LogicalPlanBuilder {
                                 right.schema().field_with_qualified_name(rr, &r.name);
 
                             match (r_is_left, r_is_right) {
-                                (Ok(_), _) => (Ok(r), Self::normalize(right, l)),
+                                (Ok(_), _) => (Ok(r), Self::normalize(&right, l)),
                                 (_, Ok(_)) => (Self::normalize(&self.plan, l), Ok(r)),
                                 _ => (
                                     Self::normalize(&self.plan, l),
-                                    Self::normalize(right, r),
+                                    Self::normalize(&right, r),
                                 ),
                             }
                         }
@@ -540,12 +540,12 @@ impl LogicalPlanBuilder {
                             let left_key = Self::normalize(&self.plan, l.clone())
                                 .or_else(|_| {
                                     swap = true;
-                                    Self::normalize(right, l)
+                                    Self::normalize(&right, l)
                                 });
                             if swap {
                                 (Self::normalize(&self.plan, r), left_key)
                             } else {
-                                (left_key, Self::normalize(right, r))
+                                (left_key, Self::normalize(&right, r))
                             }
                         }
                     }
@@ -560,8 +560,8 @@ impl LogicalPlanBuilder {
             build_join_schema(self.plan.schema(), right.schema(), &join_type)?;
 
         Ok(Self::from(LogicalPlan::Join(Join {
-            left: Arc::new(self.plan.clone()),
-            right: Arc::new(right.clone()),
+            left: Arc::new(self.plan),
+            right: Arc::new(right),
             on,
             filter,
             join_type,
@@ -573,8 +573,8 @@ impl LogicalPlanBuilder {
 
     /// Apply a join with using constraint, which duplicates all join columns in output schema.
     pub fn join_using(
-        &self,
-        right: &LogicalPlan,
+        self,
+        right: LogicalPlan,
         join_type: JoinType,
         using_keys: Vec<impl Into<Column> + Clone>,
     ) -> Result<Self> {
@@ -585,7 +585,7 @@ impl LogicalPlanBuilder {
             .collect::<Result<_>>()?;
         let right_keys: Vec<Column> = using_keys
             .into_iter()
-            .map(|c| Self::normalize(right, c))
+            .map(|c| Self::normalize(&right, c))
             .collect::<Result<_>>()?;
 
         let on: Vec<(_, _)> = left_keys.into_iter().zip(right_keys.into_iter()).collect();
@@ -617,14 +617,14 @@ impl LogicalPlanBuilder {
             }
         }
         if join_on.is_empty() {
-            let join = Self::from(self.plan.clone()).cross_join(&right.clone())?;
+            let join = Self::from(self.plan).cross_join(right)?;
             join.filter(filters.ok_or_else(|| {
                 DataFusionError::Internal("filters should not be None here".to_string())
             })?)
         } else {
             Ok(Self::from(LogicalPlan::Join(Join {
-                left: Arc::new(self.plan.clone()),
-                right: Arc::new(right.clone()),
+                left: Arc::new(self.plan),
+                right: Arc::new(right),
                 on: join_on,
                 filter: filters,
                 join_type,
@@ -636,26 +636,26 @@ impl LogicalPlanBuilder {
     }
 
     /// Apply a cross join
-    pub fn cross_join(&self, right: &LogicalPlan) -> Result<Self> {
+    pub fn cross_join(self, right: LogicalPlan) -> Result<Self> {
         let schema = self.plan.schema().join(right.schema())?;
         Ok(Self::from(LogicalPlan::CrossJoin(CrossJoin {
-            left: Arc::new(self.plan.clone()),
-            right: Arc::new(right.clone()),
+            left: Arc::new(self.plan),
+            right: Arc::new(right),
             schema: DFSchemaRef::new(schema),
         })))
     }
 
     /// Repartition
-    pub fn repartition(&self, partitioning_scheme: Partitioning) -> Result<Self> {
+    pub fn repartition(self, partitioning_scheme: Partitioning) -> Result<Self> {
         Ok(Self::from(LogicalPlan::Repartition(Repartition {
-            input: Arc::new(self.plan.clone()),
+            input: Arc::new(self.plan),
             partitioning_scheme,
         })))
     }
 
     /// Apply a window functions to extend the schema
     pub fn window(
-        &self,
+        self,
         window_expr: impl IntoIterator<Item = impl Into<Expr>>,
     ) -> Result<Self> {
         let window_expr = normalize_cols(window_expr, &self.plan)?;
@@ -663,13 +663,12 @@ impl LogicalPlanBuilder {
         validate_unique_names("Windows", all_expr.clone())?;
         let mut window_fields: Vec<DFField> = exprlist_to_fields(all_expr, &self.plan)?;
         window_fields.extend_from_slice(self.plan.schema().fields());
+        let metadata = self.plan.schema().metadata().clone();
+
         Ok(Self::from(LogicalPlan::Window(Window {
-            input: Arc::new(self.plan.clone()),
+            input: Arc::new(self.plan),
             window_expr,
-            schema: Arc::new(DFSchema::new_with_metadata(
-                window_fields,
-                self.plan.schema().metadata().clone(),
-            )?),
+            schema: Arc::new(DFSchema::new_with_metadata(window_fields, metadata)?),
         })))
     }
 
@@ -677,14 +676,14 @@ impl LogicalPlanBuilder {
     /// and calculating `aggr_expr` aggregates for each distinct
     /// value of the `group_expr`;
     pub fn aggregate(
-        &self,
+        self,
         group_expr: impl IntoIterator<Item = impl Into<Expr>>,
         aggr_expr: impl IntoIterator<Item = impl Into<Expr>>,
     ) -> Result<Self> {
         let group_expr = normalize_cols(group_expr, &self.plan)?;
         let aggr_expr = normalize_cols(aggr_expr, &self.plan)?;
         Ok(Self::from(LogicalPlan::Aggregate(Aggregate::try_new(
-            Arc::new(self.plan.clone()),
+            Arc::new(self.plan),
             group_expr,
             aggr_expr,
         )?)))
@@ -696,14 +695,14 @@ impl LogicalPlanBuilder {
     /// information about metrics during run.
     ///
     /// if `verbose` is true, prints out additional details.
-    pub fn explain(&self, verbose: bool, analyze: bool) -> Result<Self> {
+    pub fn explain(self, verbose: bool, analyze: bool) -> Result<Self> {
         let schema = LogicalPlan::explain_schema();
         let schema = schema.to_dfschema_ref()?;
 
         if analyze {
             Ok(Self::from(LogicalPlan::Analyze(Analyze {
                 verbose,
-                input: Arc::new(self.plan.clone()),
+                input: Arc::new(self.plan),
                 schema,
             })))
         } else {
@@ -712,7 +711,7 @@ impl LogicalPlanBuilder {
 
             Ok(Self::from(LogicalPlan::Explain(Explain {
                 verbose,
-                plan: Arc::new(self.plan.clone()),
+                plan: Arc::new(self.plan),
                 stringified_plans,
                 schema,
             })))
@@ -778,19 +777,19 @@ impl LogicalPlanBuilder {
             .unzip();
         if is_all {
             LogicalPlanBuilder::from(left_plan)
-                .join_detailed(&right_plan, join_type, join_keys, None, true)?
+                .join_detailed(right_plan, join_type, join_keys, None, true)?
                 .build()
         } else {
             LogicalPlanBuilder::from(left_plan)
                 .distinct()?
-                .join_detailed(&right_plan, join_type, join_keys, None, true)?
+                .join_detailed(right_plan, join_type, join_keys, None, true)?
                 .build()
         }
     }
 
     /// Build the plan
-    pub fn build(&self) -> Result<LogicalPlan> {
-        Ok(self.plan.clone())
+    pub fn build(self) -> Result<LogicalPlan> {
+        Ok(self.plan)
     }
 }
 
@@ -978,12 +977,11 @@ pub fn project(
 
 /// Create a SubqueryAlias to wrap a LogicalPlan.
 pub fn subquery_alias(
-    plan: &LogicalPlan,
+    plan: LogicalPlan,
     alias: impl Into<String>,
 ) -> Result<LogicalPlan> {
     Ok(LogicalPlan::SubqueryAlias(SubqueryAlias::try_new(
-        plan.clone(),
-        alias,
+        plan, alias,
     )?))
 }
 
@@ -1169,7 +1167,7 @@ mod tests {
         let t2 = table_scan(Some("t2"), &employee_schema(), None)?.build()?;
 
         let plan = table_scan(Some("t1"), &employee_schema(), None)?
-            .join_using(&t2, JoinType::Inner, vec!["id"])?
+            .join_using(t2, JoinType::Inner, vec!["id"])?
             .project(vec![Expr::Wildcard])?
             .build()?;
 
@@ -1190,8 +1188,9 @@ mod tests {
             table_scan(Some("employee_csv"), &employee_schema(), Some(vec![3, 4]))?;
 
         let plan = plan
-            .union(plan.build()?)?
-            .union(plan.build()?)?
+            .clone()
+            .union(plan.clone().build()?)?
+            .union(plan.clone().build()?)?
             .union(plan.build()?)?
             .build()?;
 
@@ -1213,8 +1212,9 @@ mod tests {
             table_scan(Some("employee_csv"), &employee_schema(), Some(vec![3, 4]))?;
 
         let plan = plan
-            .union_distinct(plan.build()?)?
-            .union_distinct(plan.build()?)?
+            .clone()
+            .union_distinct(plan.clone().build()?)?
+            .union_distinct(plan.clone().build()?)?
             .union_distinct(plan.build()?)?
             .build()?;
 
@@ -1239,7 +1239,7 @@ mod tests {
         let plan2 = table_scan(None, &employee_schema(), Some(vec![3, 4]))?;
 
         let expected = "Error during planning: Union queries must have the same number of columns, (left is 1, right is 2)";
-        let err_msg1 = plan1.union(plan2.build()?).unwrap_err();
+        let err_msg1 = plan1.clone().union(plan2.clone().build()?).unwrap_err();
         let err_msg2 = plan1.union_distinct(plan2.build()?).unwrap_err();
 
         assert_eq!(err_msg1.to_string(), expected);
diff --git a/datafusion/expr/src/utils.rs b/datafusion/expr/src/utils.rs
index 491693d93..0e8c599c4 100644
--- a/datafusion/expr/src/utils.rs
+++ b/datafusion/expr/src/utils.rs
@@ -499,7 +499,7 @@ pub fn from_plan(
         }
         LogicalPlan::CrossJoin(_) => {
             let left = inputs[0].clone();
-            let right = &inputs[1];
+            let right = inputs[1].clone();
             LogicalPlanBuilder::from(left).cross_join(right)?.build()
         }
         LogicalPlan::Subquery(_) => {
diff --git a/datafusion/optimizer/src/common_subexpr_eliminate.rs b/datafusion/optimizer/src/common_subexpr_eliminate.rs
index 6885a0bfb..8236f20f3 100644
--- a/datafusion/optimizer/src/common_subexpr_eliminate.rs
+++ b/datafusion/optimizer/src/common_subexpr_eliminate.rs
@@ -788,7 +788,7 @@ mod test {
         let table_scan_1 = test_table_scan_with_name("test1").unwrap();
         let table_scan_2 = test_table_scan_with_name("test2").unwrap();
         let join = LogicalPlanBuilder::from(table_scan_1)
-            .join(&table_scan_2, JoinType::Inner, (vec!["a"], vec!["a"]), None)
+            .join(table_scan_2, JoinType::Inner, (vec!["a"], vec!["a"]), None)
             .unwrap()
             .build()
             .unwrap();
diff --git a/datafusion/optimizer/src/decorrelate_where_exists.rs b/datafusion/optimizer/src/decorrelate_where_exists.rs
index 54e346a08..f0fa6bb9d 100644
--- a/datafusion/optimizer/src/decorrelate_where_exists.rs
+++ b/datafusion/optimizer/src/decorrelate_where_exists.rs
@@ -196,7 +196,7 @@ fn optimize_exists(
         false => JoinType::LeftSemi,
     };
     let mut new_plan = LogicalPlanBuilder::from(outer_input.clone()).join(
-        &subqry_plan,
+        subqry_plan,
         join_type,
         join_keys,
         join_filters,
diff --git a/datafusion/optimizer/src/decorrelate_where_in.rs b/datafusion/optimizer/src/decorrelate_where_in.rs
index dac5ff03b..de1725dc5 100644
--- a/datafusion/optimizer/src/decorrelate_where_in.rs
+++ b/datafusion/optimizer/src/decorrelate_where_in.rs
@@ -202,7 +202,7 @@ fn optimize_where_in(
         false => JoinType::LeftSemi,
     };
     let mut new_plan = LogicalPlanBuilder::from(outer_input.clone()).join(
-        &subqry_plan,
+        subqry_plan,
         join_type,
         join_keys,
         join_filters,
diff --git a/datafusion/optimizer/src/eliminate_cross_join.rs b/datafusion/optimizer/src/eliminate_cross_join.rs
index a00362d70..3e1227d02 100644
--- a/datafusion/optimizer/src/eliminate_cross_join.rs
+++ b/datafusion/optimizer/src/eliminate_cross_join.rs
@@ -411,7 +411,7 @@ mod tests {
 
         // could eliminate to inner join since filter has Join predicates
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 col("t1.a").eq(col("t2.a")),
                 And,
@@ -439,7 +439,7 @@ mod tests {
         // could not eliminate to inner join since filter OR expression and there is no common
         // Join predicates in left and right of OR expr.
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 col("t1.a").eq(col("t2.a")),
                 Or,
@@ -466,7 +466,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(20u32))),
                 And,
@@ -493,7 +493,7 @@ mod tests {
 
         // could eliminate to inner join since Or predicates have common Join predicates
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
                 Or,
@@ -523,7 +523,7 @@ mod tests {
 
         // could not eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
                 Or,
@@ -553,7 +553,7 @@ mod tests {
 
         // could not eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
                 Or,
@@ -595,8 +595,8 @@ mod tests {
 
         // could eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
-            .cross_join(&t3)?
+            .cross_join(t2)?
+            .cross_join(t3)?
             .filter(binary_expr(
                 binary_expr(col("t3.a").eq(col("t1.a")), And, col("t3.c").lt(lit(15u32))),
                 And,
@@ -628,7 +628,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan1 = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
                 Or,
@@ -641,7 +641,7 @@ mod tests {
             .build()?;
 
         let plan2 = LogicalPlanBuilder::from(t3)
-            .cross_join(&t4)?
+            .cross_join(t4)?
             .filter(binary_expr(
                 binary_expr(
                     binary_expr(
@@ -666,7 +666,7 @@ mod tests {
             .build()?;
 
         let plan = LogicalPlanBuilder::from(plan1)
-            .cross_join(&plan2)?
+            .cross_join(plan2)?
             .filter(binary_expr(
                 binary_expr(col("t3.a").eq(col("t1.a")), And, col("t4.c").lt(lit(15u32))),
                 Or,
@@ -705,7 +705,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan1 = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
                 Or,
@@ -719,7 +719,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan2 = LogicalPlanBuilder::from(t3)
-            .cross_join(&t4)?
+            .cross_join(t4)?
             .filter(binary_expr(
                 binary_expr(
                     binary_expr(
@@ -745,7 +745,7 @@ mod tests {
 
         // could not eliminate to inner join
         let plan = LogicalPlanBuilder::from(plan1)
-            .cross_join(&plan2)?
+            .cross_join(plan2)?
             .filter(binary_expr(
                 binary_expr(col("t3.a").eq(col("t1.a")), And, col("t4.c").lt(lit(15u32))),
                 Or,
@@ -780,7 +780,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan1 = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
                 Or,
@@ -794,7 +794,7 @@ mod tests {
 
         // could not eliminate to inner join
         let plan2 = LogicalPlanBuilder::from(t3)
-            .cross_join(&t4)?
+            .cross_join(t4)?
             .filter(binary_expr(
                 binary_expr(
                     binary_expr(
@@ -816,7 +816,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan = LogicalPlanBuilder::from(plan1)
-            .cross_join(&plan2)?
+            .cross_join(plan2)?
             .filter(binary_expr(
                 binary_expr(col("t3.a").eq(col("t1.a")), And, col("t4.c").lt(lit(15u32))),
                 Or,
@@ -855,7 +855,7 @@ mod tests {
 
         // could not eliminate to inner join
         let plan1 = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), Or, col("t2.c").lt(lit(15u32))),
                 Or,
@@ -869,7 +869,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan2 = LogicalPlanBuilder::from(t3)
-            .cross_join(&t4)?
+            .cross_join(t4)?
             .filter(binary_expr(
                 binary_expr(
                     binary_expr(
@@ -895,7 +895,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan = LogicalPlanBuilder::from(plan1)
-            .cross_join(&plan2)?
+            .cross_join(plan2)?
             .filter(binary_expr(
                 binary_expr(col("t3.a").eq(col("t1.a")), And, col("t4.c").lt(lit(15u32))),
                 Or,
@@ -934,7 +934,7 @@ mod tests {
 
         // could eliminate to inner join
         let plan1 = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(col("t1.a").eq(col("t2.a")), Or, col("t2.c").lt(lit(15u32))),
                 And,
@@ -947,11 +947,11 @@ mod tests {
             .build()?;
 
         // could eliminate to inner join
-        let plan2 = LogicalPlanBuilder::from(t3).cross_join(&t4)?.build()?;
+        let plan2 = LogicalPlanBuilder::from(t3).cross_join(t4)?.build()?;
 
         // could eliminate to inner join
         let plan = LogicalPlanBuilder::from(plan1)
-            .cross_join(&plan2)?
+            .cross_join(plan2)?
             .filter(binary_expr(
                 binary_expr(
                     binary_expr(
@@ -1016,14 +1016,14 @@ mod tests {
         let t4 = test_table_scan_with_name("t4")?;
 
         // could eliminate to inner join
-        let plan1 = LogicalPlanBuilder::from(t1).cross_join(&t2)?.build()?;
+        let plan1 = LogicalPlanBuilder::from(t1).cross_join(t2)?.build()?;
 
         // could eliminate to inner join
-        let plan2 = LogicalPlanBuilder::from(t3).cross_join(&t4)?.build()?;
+        let plan2 = LogicalPlanBuilder::from(t3).cross_join(t4)?.build()?;
 
         // could eliminate to inner join
         let plan = LogicalPlanBuilder::from(plan1)
-            .cross_join(&plan2)?
+            .cross_join(plan2)?
             .filter(binary_expr(
                 binary_expr(
                     binary_expr(
@@ -1102,7 +1102,7 @@ mod tests {
 
         // could eliminate to inner join since filter has Join predicates
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 (col("t1.a") + lit(100u32)).eq(col("t2.a") * lit(2u32)),
                 And,
@@ -1133,7 +1133,7 @@ mod tests {
         // could not eliminate to inner join since filter OR expression and there is no common
         // Join predicates in left and right of OR expr.
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 (col("t1.a") + lit(100u32)).eq(col("t2.a") * lit(2u32)),
                 Or,
@@ -1161,7 +1161,7 @@ mod tests {
         // could eliminate to inner join
         let common_join_key = (col("t1.a") + lit(100u32)).eq(col("t2.a") * lit(2u32));
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(common_join_key.clone(), And, col("t2.c").lt(lit(20u32))),
                 And,
@@ -1192,7 +1192,7 @@ mod tests {
         // could eliminate to inner join since Or predicates have common Join predicates
         let common_join_key = (col("t1.a") + lit(100u32)).eq(col("t2.a") * lit(2u32));
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
+            .cross_join(t2)?
             .filter(binary_expr(
                 binary_expr(common_join_key.clone(), And, col("t2.c").lt(lit(15u32))),
                 Or,
@@ -1223,8 +1223,8 @@ mod tests {
 
         // could eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
-            .cross_join(&t2)?
-            .cross_join(&t3)?
+            .cross_join(t2)?
+            .cross_join(t3)?
             .filter(binary_expr(
                 binary_expr(
                     (col("t3.a") + lit(100u32)).eq(col("t1.a") * lit(2u32)),
diff --git a/datafusion/optimizer/src/eliminate_limit.rs b/datafusion/optimizer/src/eliminate_limit.rs
index 45844e120..91771a940 100644
--- a/datafusion/optimizer/src/eliminate_limit.rs
+++ b/datafusion/optimizer/src/eliminate_limit.rs
@@ -226,7 +226,7 @@ mod tests {
         let plan = LogicalPlanBuilder::from(table_scan)
             .limit(2, Some(1))?
             .join_using(
-                &table_scan_inner,
+                table_scan_inner,
                 JoinType::Inner,
                 vec![Column::from_name("a".to_string())],
             )?
diff --git a/datafusion/optimizer/src/eliminate_outer_join.rs b/datafusion/optimizer/src/eliminate_outer_join.rs
index ffe270cdb..c615d1fb0 100644
--- a/datafusion/optimizer/src/eliminate_outer_join.rs
+++ b/datafusion/optimizer/src/eliminate_outer_join.rs
@@ -336,7 +336,7 @@ mod tests {
         // could not eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
             .join(
-                &t2,
+                t2,
                 JoinType::Left,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 None,
@@ -359,7 +359,7 @@ mod tests {
         // eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
             .join(
-                &t2,
+                t2,
                 JoinType::Left,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 None,
@@ -382,7 +382,7 @@ mod tests {
         // eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
             .join(
-                &t2,
+                t2,
                 JoinType::Right,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 None,
@@ -409,7 +409,7 @@ mod tests {
         // eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
             .join(
-                &t2,
+                t2,
                 JoinType::Full,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 None,
@@ -436,7 +436,7 @@ mod tests {
         // eliminate to inner join
         let plan = LogicalPlanBuilder::from(t1)
             .join(
-                &t2,
+                t2,
                 JoinType::Full,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 None,
diff --git a/datafusion/optimizer/src/filter_null_join_keys.rs b/datafusion/optimizer/src/filter_null_join_keys.rs
index 8563c13a8..ee6c2cc5d 100644
--- a/datafusion/optimizer/src/filter_null_join_keys.rs
+++ b/datafusion/optimizer/src/filter_null_join_keys.rs
@@ -202,7 +202,7 @@ mod tests {
         let t3 = table_scan(Some("t3"), &schema, None)?.build()?;
         let plan = LogicalPlanBuilder::from(t3)
             .join(
-                &plan,
+                plan,
                 JoinType::Inner,
                 (
                     vec![
@@ -236,7 +236,7 @@ mod tests {
     ) -> Result<LogicalPlan> {
         LogicalPlanBuilder::from(left_table)
             .join(
-                &right_table,
+                right_table,
                 JoinType::Inner,
                 (
                     vec![Column::from_qualified_name(left_key)],
diff --git a/datafusion/optimizer/src/propagate_empty_relation.rs b/datafusion/optimizer/src/propagate_empty_relation.rs
index 9b5869366..b44a918fe 100644
--- a/datafusion/optimizer/src/propagate_empty_relation.rs
+++ b/datafusion/optimizer/src/propagate_empty_relation.rs
@@ -265,7 +265,7 @@ mod tests {
 
         let plan = LogicalPlanBuilder::from(left)
             .join_using(
-                &right,
+                right,
                 JoinType::Inner,
                 vec![Column::from_name("a".to_string())],
             )?
@@ -399,7 +399,7 @@ mod tests {
         let right = LogicalPlanBuilder::empty(false).build()?;
 
         let plan = LogicalPlanBuilder::from(left)
-            .cross_join(&right)?
+            .cross_join(right)?
             .filter(col("a").lt_eq(lit(1i64)))?
             .build()?;
 
diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rs
index 2bcb60a6b..7e90a4a79 100644
--- a/datafusion/optimizer/src/push_down_filter.rs
+++ b/datafusion/optimizer/src/push_down_filter.rs
@@ -1134,6 +1134,7 @@ mod tests {
             .alias("test2")?;
 
         let plan = table
+            .clone()
             .union(table.build()?)?
             .filter(col("b").eq(lit(1i64)))?
             .build()?;
@@ -1166,7 +1167,7 @@ mod tests {
             .build()?;
         let filter = and(col("test.a").eq(lit(1)), col("test1.d").gt(lit(2)));
         let plan = LogicalPlanBuilder::from(left)
-            .cross_join(&right)?
+            .cross_join(right)?
             .project(vec![col("test.a"), col("test1.d")])?
             .filter(filter)?
             .build()?;
@@ -1195,7 +1196,7 @@ mod tests {
             .build()?;
         let filter = and(col("test.a").eq(lit(1)), col("test1.a").gt(lit(2)));
         let plan = LogicalPlanBuilder::from(left)
-            .cross_join(&right)?
+            .cross_join(right)?
             .project(vec![col("test.a"), col("test1.a")])?
             .filter(filter)?
             .build()?;
@@ -1309,7 +1310,7 @@ mod tests {
             .build()?;
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Inner,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 None,
@@ -1349,7 +1350,7 @@ mod tests {
             .build()?;
         let plan = LogicalPlanBuilder::from(left)
             .join_using(
-                &right,
+                right,
                 JoinType::Inner,
                 vec![Column::from_name("a".to_string())],
             )?
@@ -1390,7 +1391,7 @@ mod tests {
             .build()?;
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Inner,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 None,
@@ -1429,7 +1430,7 @@ mod tests {
 
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Inner,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 None,
@@ -1470,7 +1471,7 @@ mod tests {
             .build()?;
         let plan = LogicalPlanBuilder::from(left)
             .join_using(
-                &right,
+                right,
                 JoinType::Left,
                 vec![Column::from_name("a".to_string())],
             )?
@@ -1508,7 +1509,7 @@ mod tests {
             .build()?;
         let plan = LogicalPlanBuilder::from(left)
             .join_using(
-                &right,
+                right,
                 JoinType::Right,
                 vec![Column::from_name("a".to_string())],
             )?
@@ -1547,7 +1548,7 @@ mod tests {
             .build()?;
         let plan = LogicalPlanBuilder::from(left)
             .join_using(
-                &right,
+                right,
                 JoinType::Left,
                 vec![Column::from_name("a".to_string())],
             )?
@@ -1586,7 +1587,7 @@ mod tests {
             .build()?;
         let plan = LogicalPlanBuilder::from(left)
             .join_using(
-                &right,
+                right,
                 JoinType::Right,
                 vec![Column::from_name("a".to_string())],
             )?
@@ -1630,7 +1631,7 @@ mod tests {
             .and(col("test2.c").gt(lit(4u32)));
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Inner,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 Some(filter),
@@ -1674,7 +1675,7 @@ mod tests {
             .and(col("test2.c").gt(lit(4u32)));
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Inner,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 Some(filter),
@@ -1716,7 +1717,7 @@ mod tests {
         let filter = col("test.a").gt(lit(1u32));
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Inner,
                 (vec![Column::from_name("a")], vec![Column::from_name("b")]),
                 Some(filter),
@@ -1761,7 +1762,7 @@ mod tests {
             .and(col("test2.c").gt(lit(4u32)));
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Left,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 Some(filter),
@@ -1805,7 +1806,7 @@ mod tests {
             .and(col("test2.c").gt(lit(4u32)));
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Right,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 Some(filter),
@@ -1849,7 +1850,7 @@ mod tests {
             .and(col("test2.c").gt(lit(4u32)));
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Full,
                 (vec![Column::from_name("a")], vec![Column::from_name("a")]),
                 Some(filter),
@@ -2102,7 +2103,7 @@ mod tests {
         let filter = col("c").gt(lit(1u32));
         let plan = LogicalPlanBuilder::from(left)
             .join(
-                &right,
+                right,
                 JoinType::Inner,
                 (vec![Column::from_name("c")], vec![Column::from_name("d")]),
                 Some(filter),
@@ -2281,7 +2282,7 @@ mod tests {
             and(col("b").eq(col("e")), col("c").lt(lit(10u32))),
         );
         let plan = LogicalPlanBuilder::from(left)
-            .cross_join(&right)?
+            .cross_join(right)?
             .filter(filter)?
             .build()?;
 
diff --git a/datafusion/optimizer/src/push_down_limit.rs b/datafusion/optimizer/src/push_down_limit.rs
index 22e837a6c..3da7e07db 100644
--- a/datafusion/optimizer/src/push_down_limit.rs
+++ b/datafusion/optimizer/src/push_down_limit.rs
@@ -536,7 +536,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
             .join(
-                &LogicalPlanBuilder::from(table_scan_2).build()?,
+                LogicalPlanBuilder::from(table_scan_2).build()?,
                 JoinType::Inner,
                 (vec!["a"], vec!["a"]),
                 None,
@@ -560,7 +560,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
             .join(
-                &LogicalPlanBuilder::from(table_scan_2).build()?,
+                LogicalPlanBuilder::from(table_scan_2).build()?,
                 JoinType::Inner,
                 (vec!["a"], vec!["a"]),
                 None,
@@ -643,7 +643,7 @@ mod test {
         let right_keys: Vec<&str> = Vec::new();
         let plan = LogicalPlanBuilder::from(table_scan_1.clone())
             .join(
-                &LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
+                LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
                 JoinType::Left,
                 (left_keys.clone(), right_keys.clone()),
                 None,
@@ -662,7 +662,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1.clone())
             .join(
-                &LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
+                LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
                 JoinType::Right,
                 (left_keys.clone(), right_keys.clone()),
                 None,
@@ -681,7 +681,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1.clone())
             .join(
-                &LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
+                LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
                 JoinType::Full,
                 (left_keys.clone(), right_keys.clone()),
                 None,
@@ -700,7 +700,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1.clone())
             .join(
-                &LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
+                LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
                 JoinType::LeftSemi,
                 (left_keys.clone(), right_keys.clone()),
                 None,
@@ -718,7 +718,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1.clone())
             .join(
-                &LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
+                LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
                 JoinType::LeftAnti,
                 (left_keys.clone(), right_keys.clone()),
                 None,
@@ -736,7 +736,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1.clone())
             .join(
-                &LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
+                LogicalPlanBuilder::from(table_scan_2.clone()).build()?,
                 JoinType::RightSemi,
                 (left_keys.clone(), right_keys.clone()),
                 None,
@@ -754,7 +754,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
             .join(
-                &LogicalPlanBuilder::from(table_scan_2).build()?,
+                LogicalPlanBuilder::from(table_scan_2).build()?,
                 JoinType::RightAnti,
                 (left_keys, right_keys),
                 None,
@@ -778,7 +778,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
             .join(
-                &LogicalPlanBuilder::from(table_scan_2).build()?,
+                LogicalPlanBuilder::from(table_scan_2).build()?,
                 JoinType::Left,
                 (vec!["a"], vec!["a"]),
                 None,
@@ -803,7 +803,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
             .join(
-                &LogicalPlanBuilder::from(table_scan_2).build()?,
+                LogicalPlanBuilder::from(table_scan_2).build()?,
                 JoinType::Left,
                 (vec!["a"], vec!["a"]),
                 None,
@@ -828,7 +828,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
             .join(
-                &LogicalPlanBuilder::from(table_scan_2).build()?,
+                LogicalPlanBuilder::from(table_scan_2).build()?,
                 JoinType::Right,
                 (vec!["a"], vec!["a"]),
                 None,
@@ -853,7 +853,7 @@ mod test {
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
             .join(
-                &LogicalPlanBuilder::from(table_scan_2).build()?,
+                LogicalPlanBuilder::from(table_scan_2).build()?,
                 JoinType::Right,
                 (vec!["a"], vec!["a"]),
                 None,
@@ -877,7 +877,7 @@ mod test {
         let table_scan_2 = test_table_scan_with_name("test2")?;
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
-            .cross_join(&LogicalPlanBuilder::from(table_scan_2).build()?)?
+            .cross_join(LogicalPlanBuilder::from(table_scan_2).build()?)?
             .limit(0, Some(1000))?
             .build()?;
 
@@ -897,7 +897,7 @@ mod test {
         let table_scan_2 = test_table_scan_with_name("test2")?;
 
         let plan = LogicalPlanBuilder::from(table_scan_1)
-            .cross_join(&LogicalPlanBuilder::from(table_scan_2).build()?)?
+            .cross_join(LogicalPlanBuilder::from(table_scan_2).build()?)?
             .limit(1000, Some(1000))?
             .build()?;
 
diff --git a/datafusion/optimizer/src/push_down_projection.rs b/datafusion/optimizer/src/push_down_projection.rs
index 642aa188e..67a6c0d5c 100644
--- a/datafusion/optimizer/src/push_down_projection.rs
+++ b/datafusion/optimizer/src/push_down_projection.rs
@@ -666,7 +666,7 @@ mod tests {
         let table2_scan = scan_empty(Some("test2"), &schema, None)?.build()?;
 
         let plan = LogicalPlanBuilder::from(table_scan)
-            .join(&table2_scan, JoinType::Left, (vec!["a"], vec!["c1"]), None)?
+            .join(table2_scan, JoinType::Left, (vec!["a"], vec!["c1"]), None)?
             .project(vec![col("a"), col("b"), col("c1")])?
             .build()?;
 
@@ -707,7 +707,7 @@ mod tests {
         let table2_scan = scan_empty(Some("test2"), &schema, None)?.build()?;
 
         let plan = LogicalPlanBuilder::from(table_scan)
-            .join(&table2_scan, JoinType::Left, (vec!["a"], vec!["c1"]), None)?
+            .join(table2_scan, JoinType::Left, (vec!["a"], vec!["c1"]), None)?
             // projecting joined column `a` should push the right side column `c1` projection as
             // well into test2 table even though `c1` is not referenced in projection.
             .project(vec![col("a"), col("b")])?
@@ -750,7 +750,7 @@ mod tests {
         let table2_scan = scan_empty(Some("test2"), &schema, None)?.build()?;
 
         let plan = LogicalPlanBuilder::from(table_scan)
-            .join_using(&table2_scan, JoinType::Left, vec!["a"])?
+            .join_using(table2_scan, JoinType::Left, vec!["a"])?
             .project(vec![col("a"), col("b")])?
             .build()?;
 
diff --git a/datafusion/optimizer/src/scalar_subquery_to_join.rs b/datafusion/optimizer/src/scalar_subquery_to_join.rs
index 3ee46c8d6..12092621b 100644
--- a/datafusion/optimizer/src/scalar_subquery_to_join.rs
+++ b/datafusion/optimizer/src/scalar_subquery_to_join.rs
@@ -314,10 +314,10 @@ fn optimize_scalar(
     let new_plan = LogicalPlanBuilder::from(filter_input.clone());
     let mut new_plan = if join_keys.0.is_empty() {
         // if not correlated, group down to 1 row and cross join on that (preserving row count)
-        new_plan.cross_join(&subqry_plan)?
+        new_plan.cross_join(subqry_plan)?
     } else {
         // inner join if correlated, grouping by the join keys so we don't change row count
-        new_plan.join(&subqry_plan, JoinType::Inner, join_keys, None)?
+        new_plan.join(subqry_plan, JoinType::Inner, join_keys, None)?
     };
 
     // restore where in condition
diff --git a/datafusion/proto/src/logical_plan.rs b/datafusion/proto/src/logical_plan.rs
index d9423f8de..09ae9e41e 100644
--- a/datafusion/proto/src/logical_plan.rs
+++ b/datafusion/proto/src/logical_plan.rs
@@ -728,13 +728,13 @@ impl AsLogicalPlan for LogicalPlanNode {
                 )?);
                 let builder = match join_constraint.into() {
                     JoinConstraint::On => builder.join(
-                        &into_logical_plan!(join.right, ctx, extension_codec)?,
+                        into_logical_plan!(join.right, ctx, extension_codec)?,
                         join_type.into(),
                         (left_keys, right_keys),
                         filter,
                     )?,
                     JoinConstraint::Using => builder.join_using(
-                        &into_logical_plan!(join.right, ctx, extension_codec)?,
+                        into_logical_plan!(join.right, ctx, extension_codec)?,
                         join_type.into(),
                         left_keys,
                     )?,
@@ -768,7 +768,7 @@ impl AsLogicalPlan for LogicalPlanNode {
                 let left = into_logical_plan!(crossjoin.left, ctx, extension_codec)?;
                 let right = into_logical_plan!(crossjoin.right, ctx, extension_codec)?;
 
-                LogicalPlanBuilder::from(left).cross_join(&right)?.build()
+                LogicalPlanBuilder::from(left).cross_join(right)?.build()
             }
             LogicalPlanType::Extension(LogicalExtensionNode { node, inputs }) => {
                 let input_plans: Vec<LogicalPlan> = inputs
diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rs
index 58c7229bc..761e13fda 100644
--- a/datafusion/sql/src/planner.rs
+++ b/datafusion/sql/src/planner.rs
@@ -711,15 +711,16 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
                 self.plan_table_with_joins(from, planner_context)
             }
             _ => {
-                let plans = from
+                let mut plans = from
                     .into_iter()
-                    .map(|t| self.plan_table_with_joins(t, planner_context))
-                    .collect::<Result<Vec<_>>>()?;
-                let mut left = plans[0].clone();
-                for right in plans.iter().skip(1) {
-                    left = LogicalPlanBuilder::from(left).cross_join(right)?.build()?;
+                    .map(|t| self.plan_table_with_joins(t, planner_context));
+
+                let mut left = LogicalPlanBuilder::from(plans.next().unwrap()?);
+
+                for right in plans {
+                    left = left.cross_join(right?)?;
                 }
-                Ok(left)
+                Ok(left.build()?)
             }
         }
     }
@@ -779,7 +780,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
             JoinOperator::FullOuter(constraint) => {
                 self.parse_join(left, right, constraint, JoinType::Full, planner_context)
             }
-            JoinOperator::CrossJoin => self.parse_cross_join(left, &right),
+            JoinOperator::CrossJoin => self.parse_cross_join(left, right),
             other => Err(DataFusionError::NotImplemented(format!(
                 "Unsupported JOIN operator {:?}",
                 other
@@ -790,7 +791,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
     fn parse_cross_join(
         &self,
         left: LogicalPlan,
-        right: &LogicalPlan,
+        right: LogicalPlan,
     ) -> Result<LogicalPlan> {
         LogicalPlanBuilder::from(left).cross_join(right)?.build()
     }
@@ -845,11 +846,11 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
                 if left_keys.is_empty() {
                     // TODO should not use cross join when the join_filter exists
                     // https://github.com/apache/arrow-datafusion/issues/4363
-                    let join = LogicalPlanBuilder::from(left).cross_join(&right)?;
-                    join_filter
-                        .map(|filter| join.filter(filter))
-                        .unwrap_or(Ok(join))?
-                        .build()
+                    let mut join = LogicalPlanBuilder::from(left).cross_join(right)?;
+                    if let Some(filter) = join_filter {
+                        join = join.filter(filter)?;
+                    }
+                    join.build()
                 } else {
                     // Wrap projection for left input if left join keys contain normal expression.
                     let (left_child, left_join_keys, left_projected) =
@@ -860,7 +861,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
                         wrap_projection_for_join_if_necessary(&right_keys, right)?;
 
                     let join_plan_builder = LogicalPlanBuilder::from(left_child).join(
-                        &right_child,
+                        right_child,
                         join_type,
                         (left_join_keys, right_join_keys),
                         join_filter,
@@ -885,7 +886,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
                     .map(|x| Column::from_name(normalize_ident(x)))
                     .collect();
                 LogicalPlanBuilder::from(left)
-                    .join_using(&right, join_type, keys)?
+                    .join_using(right, join_type, keys)?
                     .build()
             }
             JoinConstraint::Natural => {