zhuliquan commented on code in PR #13315:
URL: https://github.com/apache/datafusion/pull/13315#discussion_r1843946416


##########
datafusion/expr/src/expr.rs:
##########
@@ -1674,6 +1674,69 @@ impl Expr {
     }
 }
 
+impl Normalizeable for Expr {
+    fn can_normalize(&self) -> bool {
+        #[allow(clippy::match_like_matches_macro)]
+        match self {
+            Expr::BinaryExpr(BinaryExpr {
+                op:
+                    _op @ (Operator::Plus
+                    | Operator::Multiply
+                    | Operator::BitwiseAnd
+                    | Operator::BitwiseOr
+                    | Operator::BitwiseXor
+                    | Operator::Eq
+                    | Operator::NotEq),
+                ..
+            }) => true,
+            _ => false,
+        }
+    }
+}
+
+impl NormalizeEq for Expr {
+    fn normalize_eq(&self, other: &Self) -> bool {
+        match (self, other) {
+            (
+                Expr::BinaryExpr(BinaryExpr {
+                    left: self_left,
+                    op: self_op,
+                    right: self_right,
+                }),
+                Expr::BinaryExpr(BinaryExpr {
+                    left: other_left,
+                    op: other_op,
+                    right: other_right,
+                }),
+            ) => {
+                if self_op != other_op {
+                    return false;
+                }
+
+                if matches!(
+                    self_op,
+                    Operator::Plus
+                        | Operator::Multiply
+                        | Operator::BitwiseAnd
+                        | Operator::BitwiseOr
+                        | Operator::BitwiseXor
+                        | Operator::Eq
+                        | Operator::NotEq
+                ) {
+                    (self_left.normalize_eq(other_left)
+                        && self_right.normalize_eq(other_right))
+                        || (self_left.normalize_eq(other_right)
+                            && self_right.normalize_eq(other_left))
+                } else {
+                    self_left.normalize_eq(other_left)
+                        && self_right.normalize_eq(other_right)
+                }
+            }
+            (_, _) => self == other,

Review Comment:
   > Yeah, that is not easy to handle now, but IMO this PR looks great without 
that as well.
   > 
   > Actually, I'm working on something that stores and updates certain 
statistics and properties (like the hash) of nodes automatically during 
transformations. I think once the hashes of a node's children will be cheap we 
can use it to sort the children and call `normalize_eq()` on the pairs.
   
   Yeah, I agree store and update hash for nodes greatly. Now, computing node's 
hash (this is recursive process and incrementally accumulate the hash of the 
child nodes) and invoke `normalize_eq` are separate.
   
   1. computing node hash here:
   
https://github.com/apache/datafusion/blob/cc11692226da7e5dd49caaee2a8c3e66af920d4c/datafusion/common/src/cse.rs#L325-L331
   
   2. invoke `normalize_eq` here:
   
https://github.com/apache/datafusion/blob/cc11692226da7e5dd49caaee2a8c3e66af920d4c/datafusion/common/src/cse.rs#L396-L399
   
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to