Dandandan commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r1004002507


##########
datafusion/expr/src/expr.rs:
##########
@@ -265,6 +265,58 @@ impl BinaryExpr {
     pub fn new(left: Box<Expr>, op: Operator, right: Box<Expr>) -> Self {
         Self { left, op, right }
     }
+
+    /// Get the operator precedence
+    /// use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a 
reference
+    pub fn precedence(&self) -> u8 {
+        match self.op {
+            Operator::Or => 5,
+            Operator::And => 10,
+            Operator::Like | Operator::NotLike => 19,
+            Operator::NotEq
+            | Operator::Eq
+            | Operator::Lt
+            | Operator::LtEq
+            | Operator::Gt
+            | Operator::GtEq => 20,
+            Operator::Plus | Operator::Minus => 30,
+            Operator::Multiply | Operator::Divide | Operator::Modulo => 40,
+            _ => 0,
+        }
+    }
+}
+
+impl Display for BinaryExpr {
+    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+        // Put parentheses around child binary expressions so that we can see 
the difference
+        // between `(a OR b) AND c` and `a OR (b AND c)`. We only insert 
parentheses when needed,
+        // based on operator precedence. For example, `(a AND b) OR c` and `a 
AND b OR c` are
+        // equivalent and the parentheses are not necessary.
+
+        fn write_child(
+            f: &mut Formatter<'_>,
+            expr: &Expr,
+            precedence: u8,
+        ) -> fmt::Result {
+            match expr {
+                Expr::BinaryExpr(child) => {
+                    let p = child.precedence();
+                    if p == 0 || p < precedence {

Review Comment:
   ```suggestion
                       if p <= precedence {
   ```
   Is the same?



-- 
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]

Reply via email to