You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2023/06/01 10:43:32 UTC

[arrow-datafusion] branch main updated: Minimize clone in or simplifier (#6509)

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/arrow-datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 2086e1db00 Minimize clone in or simplifier (#6509)
2086e1db00 is described below

commit 2086e1db009853f26080a251b3d50f88a34204da
Author: Armin Primadi <ap...@gmail.com>
AuthorDate: Thu Jun 1 17:43:26 2023 +0700

    Minimize clone in or simplifier (#6509)
---
 .../simplify_expressions/or_in_list_simplifier.rs  | 24 ++++++++++++----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/datafusion/optimizer/src/simplify_expressions/or_in_list_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/or_in_list_simplifier.rs
index 10f3aa0278..cebaaccc41 100644
--- a/datafusion/optimizer/src/simplify_expressions/or_in_list_simplifier.rs
+++ b/datafusion/optimizer/src/simplify_expressions/or_in_list_simplifier.rs
@@ -17,6 +17,8 @@
 
 //! This module implements a rule that simplifies OR expressions into IN list expressions
 
+use std::borrow::Cow;
+
 use datafusion_common::tree_node::TreeNodeRewriter;
 use datafusion_common::Result;
 use datafusion_expr::expr::InList;
@@ -48,6 +50,8 @@ impl TreeNodeRewriter for OrInListSimplifier {
                         && !lhs.negated
                         && !rhs.negated
                     {
+                        let lhs = lhs.into_owned();
+                        let rhs = rhs.into_owned();
                         let mut list = vec![];
                         list.extend(lhs.list);
                         list.extend(rhs.list);
@@ -67,23 +71,21 @@ impl TreeNodeRewriter for OrInListSimplifier {
 }
 
 /// Try to convert an expression to an in-list expression
-fn as_inlist(expr: &Expr) -> Option<InList> {
+fn as_inlist(expr: &Expr) -> Option<Cow<InList>> {
     match expr {
-        Expr::InList(inlist) => Some(inlist.clone()),
+        Expr::InList(inlist) => Some(Cow::Borrowed(inlist)),
         Expr::BinaryExpr(BinaryExpr { left, op, right }) if *op == Operator::Eq => {
-            let unboxed_left = *left.clone();
-            let unboxed_right = *right.clone();
-            match (&unboxed_left, &unboxed_right) {
-                (Expr::Column(_), Expr::Literal(_)) => Some(InList {
+            match (left.as_ref(), right.as_ref()) {
+                (Expr::Column(_), Expr::Literal(_)) => Some(Cow::Owned(InList {
                     expr: left.clone(),
-                    list: vec![unboxed_right],
+                    list: vec![*right.clone()],
                     negated: false,
-                }),
-                (Expr::Literal(_), Expr::Column(_)) => Some(InList {
+                })),
+                (Expr::Literal(_), Expr::Column(_)) => Some(Cow::Owned(InList {
                     expr: right.clone(),
-                    list: vec![unboxed_left],
+                    list: vec![*left.clone()],
                     negated: false,
-                }),
+                })),
                 _ => None,
             }
         }