You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "peter-toth (via GitHub)" <gi...@apache.org> on 2024/04/17 14:06:14 UTC

[PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

peter-toth opened a new pull request, #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115

   ## Which issue does this PR close?
   
   Part of https://github.com/apache/arrow-datafusion/issues/9637, follow-up to https://github.com/apache/arrow-datafusion/pull/10087.
   
   ## Rationale for this change
   
   Eliminates remaining `Expr` clones.
   
   ## What changes are included in this PR?
   
   This PR:
   - Refactors `UnwrapCastExprRewriter` to remove `Expr` clones.
   - Adds handling of a few unhandled errors in the `BinaryExpr` case as if any error occurs this optimizer rule should return the original expression.
   
   ## Are these changes tested?
   
   Yes, with existing UTs.
   
   ## Are there any user-facing changes?
   
   No.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#issuecomment-2061642783

   The test failure:
   ```
   [SQL] SELECT * FROM (
       SELECT 1 as c, 2 as d
       UNION ALL
       SELECT 1 as c, 3 AS d
   ) as a FULL JOIN (SELECT 1 as e, 3 AS f) AS rhs ON a.c=rhs.e;
   [Diff] (-expected|+actual)
   -   1 2 1 3
   -   1 3 1 3
   +   1 3 1 3
   +   1 2 1 3
   at test_files/joins.slt:3676
   ```
   seems unrelated.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1569309601


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {
+                    let Ok(left_type) = left.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
                     };
+                    let Ok(right_type) = right.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
+                    };
+                    is_support_data_type(&left_type)
+                        && is_support_data_type(&right_type)
+                        && is_comparison_op(op)
+                } =>
+            {
+                match (left.as_mut(), right.as_mut()) {
+                    (
+                        Expr::Literal(left_lit_value),
+                        Expr::TryCast(TryCast {
+                            expr: right_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: right_expr, ..
+                        }),
+                    ) => {
+                        // if the left_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = right_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(left_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        **left = lit(value);
+                        // unwrap the cast/try_cast for the right expr
+                        **right =
+                            mem::replace(right_expr, Expr::Literal(ScalarValue::Null));
+                        Ok(Transformed::yes(expr))
+                    }
+                    (
+                        Expr::TryCast(TryCast {
+                            expr: left_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: left_expr, ..
+                        }),
+                        Expr::Literal(right_lit_value),
+                    ) => {
+                        // if the right_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = left_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(right_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        // unwrap the cast/try_cast for the left expr
+                        **left =
+                            mem::replace(left_expr, Expr::Literal(ScalarValue::Null));

Review Comment:
   `mem::take` is a nice thing to avoid cloning, I think I used it to avoid String cloning other day, and we can have a default Expr with panicking inside I'd say...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 merged PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1569103777


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {
+                    let Ok(left_type) = left.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
                     };
+                    let Ok(right_type) = right.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
+                    };
+                    is_support_data_type(&left_type)
+                        && is_support_data_type(&right_type)
+                        && is_comparison_op(op)
+                } =>
+            {
+                match (left.as_mut(), right.as_mut()) {
+                    (
+                        Expr::Literal(left_lit_value),
+                        Expr::TryCast(TryCast {
+                            expr: right_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: right_expr, ..
+                        }),
+                    ) => {
+                        // if the left_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = right_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(left_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        **left = lit(value);
+                        // unwrap the cast/try_cast for the right expr
+                        **right =
+                            mem::replace(right_expr, Expr::Literal(ScalarValue::Null));
+                        Ok(Transformed::yes(expr))
+                    }
+                    (
+                        Expr::TryCast(TryCast {
+                            expr: left_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: left_expr, ..
+                        }),
+                        Expr::Literal(right_lit_value),
+                    ) => {
+                        // if the right_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = left_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(right_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        // unwrap the cast/try_cast for the left expr
+                        **left =
+                            mem::replace(left_expr, Expr::Literal(ScalarValue::Null));

Review Comment:
   Would be nice to have a default `Expr` so that we could use `mem::take()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1569303691


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {
+                    let Ok(left_type) = left.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
                     };
+                    let Ok(right_type) = right.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
+                    };
+                    is_support_data_type(&left_type)
+                        && is_support_data_type(&right_type)
+                        && is_comparison_op(op)
+                } =>
+            {
+                match (left.as_mut(), right.as_mut()) {
+                    (
+                        Expr::Literal(left_lit_value),
+                        Expr::TryCast(TryCast {
+                            expr: right_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: right_expr, ..
+                        }),
+                    ) => {
+                        // if the left_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = right_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(left_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        **left = lit(value);
+                        // unwrap the cast/try_cast for the right expr
+                        **right =
+                            mem::replace(right_expr, Expr::Literal(ScalarValue::Null));
+                        Ok(Transformed::yes(expr))
+                    }
+                    (
+                        Expr::TryCast(TryCast {
+                            expr: left_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: left_expr, ..
+                        }),
+                        Expr::Literal(right_lit_value),
+                    ) => {
+                        // if the right_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = left_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(right_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        // unwrap the cast/try_cast for the left expr
+                        **left =
+                            mem::replace(left_expr, Expr::Literal(ScalarValue::Null));

Review Comment:
   Shall I open a follow-up PR that adds default for `Expr`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1570666526


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {
+                    let Ok(left_type) = left.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
                     };
+                    let Ok(right_type) = right.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
+                    };
+                    is_support_data_type(&left_type)
+                        && is_support_data_type(&right_type)
+                        && is_comparison_op(op)
+                } =>
+            {
+                match (left.as_mut(), right.as_mut()) {
+                    (
+                        Expr::Literal(left_lit_value),
+                        Expr::TryCast(TryCast {
+                            expr: right_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: right_expr, ..
+                        }),
+                    ) => {
+                        // if the left_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = right_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(left_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        **left = lit(value);
+                        // unwrap the cast/try_cast for the right expr
+                        **right =
+                            mem::replace(right_expr, Expr::Literal(ScalarValue::Null));
+                        Ok(Transformed::yes(expr))
+                    }
+                    (
+                        Expr::TryCast(TryCast {
+                            expr: left_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: left_expr, ..
+                        }),
+                        Expr::Literal(right_lit_value),
+                    ) => {
+                        // if the right_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = left_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(right_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        // unwrap the cast/try_cast for the left expr
+                        **left =
+                            mem::replace(left_expr, Expr::Literal(ScalarValue::Null));

Review Comment:
   I've opened a minor PR to add default for `Expr`: https://github.com/apache/arrow-datafusion/pull/10127, but as `Expr` is just an enum, I don't know how to panic inside it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1570666526


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {
+                    let Ok(left_type) = left.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
                     };
+                    let Ok(right_type) = right.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
+                    };
+                    is_support_data_type(&left_type)
+                        && is_support_data_type(&right_type)
+                        && is_comparison_op(op)
+                } =>
+            {
+                match (left.as_mut(), right.as_mut()) {
+                    (
+                        Expr::Literal(left_lit_value),
+                        Expr::TryCast(TryCast {
+                            expr: right_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: right_expr, ..
+                        }),
+                    ) => {
+                        // if the left_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = right_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(left_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        **left = lit(value);
+                        // unwrap the cast/try_cast for the right expr
+                        **right =
+                            mem::replace(right_expr, Expr::Literal(ScalarValue::Null));
+                        Ok(Transformed::yes(expr))
+                    }
+                    (
+                        Expr::TryCast(TryCast {
+                            expr: left_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: left_expr, ..
+                        }),
+                        Expr::Literal(right_lit_value),
+                    ) => {
+                        // if the right_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = left_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(right_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        // unwrap the cast/try_cast for the left expr
+                        **left =
+                            mem::replace(left_expr, Expr::Literal(ScalarValue::Null));

Review Comment:
   I've opened a minor PR to add the default `Expr`: https://github.com/apache/arrow-datafusion/pull/10127, but as `Expr` is just an enum, I don't know how to panic inside it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1569102800


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {

Review Comment:
   I was trying a few different patterns to remove `Expr` clones in this rule. The `match &mut expr` way seems to be most straightforward as `expr` is still available after pattern matching so we can return it unchanged if needed (`return Ok(Transformed::no(expr));`) but we can also mutate some parts of the `expr` if needed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1570666526


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {
+                    let Ok(left_type) = left.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
                     };
+                    let Ok(right_type) = right.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
+                    };
+                    is_support_data_type(&left_type)
+                        && is_support_data_type(&right_type)
+                        && is_comparison_op(op)
+                } =>
+            {
+                match (left.as_mut(), right.as_mut()) {
+                    (
+                        Expr::Literal(left_lit_value),
+                        Expr::TryCast(TryCast {
+                            expr: right_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: right_expr, ..
+                        }),
+                    ) => {
+                        // if the left_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = right_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(left_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        **left = lit(value);
+                        // unwrap the cast/try_cast for the right expr
+                        **right =
+                            mem::replace(right_expr, Expr::Literal(ScalarValue::Null));
+                        Ok(Transformed::yes(expr))
+                    }
+                    (
+                        Expr::TryCast(TryCast {
+                            expr: left_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: left_expr, ..
+                        }),
+                        Expr::Literal(right_lit_value),
+                    ) => {
+                        // if the right_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = left_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(right_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        // unwrap the cast/try_cast for the left expr
+                        **left =
+                            mem::replace(left_expr, Expr::Literal(ScalarValue::Null));

Review Comment:
   I've opened a minor PR to add the default `Expr`: https://github.com/apache/arrow-datafusion/pull/10127, but as `Expr` is just an enum so I don't know how to panic inside it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#issuecomment-2063787837

   Thanks all for the review!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#issuecomment-2061783799

   CI failure I think is due to https://github.com/apache/arrow-datafusion/issues/10119, so I retriggered the run


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1569283141


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {
+                    let Ok(left_type) = left.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
                     };
+                    let Ok(right_type) = right.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
+                    };
+                    is_support_data_type(&left_type)
+                        && is_support_data_type(&right_type)
+                        && is_comparison_op(op)
+                } =>
+            {
+                match (left.as_mut(), right.as_mut()) {
+                    (
+                        Expr::Literal(left_lit_value),
+                        Expr::TryCast(TryCast {
+                            expr: right_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: right_expr, ..
+                        }),
+                    ) => {
+                        // if the left_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = right_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(left_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        **left = lit(value);
+                        // unwrap the cast/try_cast for the right expr
+                        **right =
+                            mem::replace(right_expr, Expr::Literal(ScalarValue::Null));
+                        Ok(Transformed::yes(expr))
+                    }
+                    (
+                        Expr::TryCast(TryCast {
+                            expr: left_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: left_expr, ..
+                        }),
+                        Expr::Literal(right_lit_value),
+                    ) => {
+                        // if the right_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = left_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(right_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        // unwrap the cast/try_cast for the left expr
+                        **left =
+                            mem::replace(left_expr, Expr::Literal(ScalarValue::Null));

Review Comment:
   Oh yeah!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1570666526


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {
+                    let Ok(left_type) = left.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
                     };
+                    let Ok(right_type) = right.get_type(&self.schema) else {
+                        return Ok(Transformed::no(expr));
+                    };
+                    is_support_data_type(&left_type)
+                        && is_support_data_type(&right_type)
+                        && is_comparison_op(op)
+                } =>
+            {
+                match (left.as_mut(), right.as_mut()) {
+                    (
+                        Expr::Literal(left_lit_value),
+                        Expr::TryCast(TryCast {
+                            expr: right_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: right_expr, ..
+                        }),
+                    ) => {
+                        // if the left_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = right_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(left_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        **left = lit(value);
+                        // unwrap the cast/try_cast for the right expr
+                        **right =
+                            mem::replace(right_expr, Expr::Literal(ScalarValue::Null));
+                        Ok(Transformed::yes(expr))
+                    }
+                    (
+                        Expr::TryCast(TryCast {
+                            expr: left_expr, ..
+                        })
+                        | Expr::Cast(Cast {
+                            expr: left_expr, ..
+                        }),
+                        Expr::Literal(right_lit_value),
+                    ) => {
+                        // if the right_lit_value can be casted to the type of expr
+                        // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
+                        let Ok(expr_type) = left_expr.get_type(&self.schema) else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        let Ok(Some(value)) =
+                            try_cast_literal_to_type(right_lit_value, &expr_type)
+                        else {
+                            return Ok(Transformed::no(expr));
+                        };
+                        // unwrap the cast/try_cast for the left expr
+                        **left =
+                            mem::replace(left_expr, Expr::Literal(ScalarValue::Null));

Review Comment:
   I've opened a minor PR to add a default `Expr`: https://github.com/apache/arrow-datafusion/pull/10127, but as `Expr` is just an enum, I don't know how to panic inside it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1569186015


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {

Review Comment:
   Today I learned you can add statements in a `{}` as part of a match clause. Nice!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#discussion_r1569728570


##########
datafusion/optimizer/src/unwrap_cast_in_comparison.rs:
##########
@@ -140,140 +139,132 @@ struct UnwrapCastExprRewriter {
 impl TreeNodeRewriter for UnwrapCastExprRewriter {
     type Node = Expr;
 
-    fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
-        match &expr {
+    fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
+        match &mut expr {
             // For case:
             // try_cast/cast(expr as data_type) op literal
             // literal op try_cast/cast(expr as data_type)
-            Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
-                let left = left.as_ref().clone();
-                let right = right.as_ref().clone();
-                let left_type = left.get_type(&self.schema)?;
-                let right_type = right.get_type(&self.schema)?;
-                // Because the plan has been done the type coercion, the left and right must be equal
-                if is_support_data_type(&left_type)
-                    && is_support_data_type(&right_type)
-                    && is_comparison_op(op)
-                {
-                    match (&left, &right) {
-                        (
-                            Expr::Literal(left_lit_value),
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                        ) => {
-                            // if the left_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(left_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the right expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    lit(value),
-                                    *op,
-                                    expr.as_ref().clone(),
-                                )));
-                            }
-                        }
-                        (
-                            Expr::TryCast(TryCast { expr, .. })
-                            | Expr::Cast(Cast { expr, .. }),
-                            Expr::Literal(right_lit_value),
-                        ) => {
-                            // if the right_lit_value can be casted to the type of expr
-                            // we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
-                            let expr_type = expr.get_type(&self.schema)?;
-                            let casted_scalar_value =
-                                try_cast_literal_to_type(right_lit_value, &expr_type)?;
-                            if let Some(value) = casted_scalar_value {
-                                // unwrap the cast/try_cast for the left expr
-                                return Ok(Transformed::yes(binary_expr(
-                                    expr.as_ref().clone(),
-                                    *op,
-                                    lit(value),
-                                )));
-                            }
-                        }
-                        (_, _) => {
-                            // do nothing
-                        }
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if {

Review Comment:
   +1. I used to wrap another function.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Refactor `UnwrapCastInComparison` to remove `Expr` clones [arrow-datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #10115:
URL: https://github.com/apache/arrow-datafusion/pull/10115#issuecomment-2062784683

   Thanks @peter-toth @alamb @comphead 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org