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


##########
datafusion/expr/src/expr.rs:
##########
@@ -267,6 +267,28 @@ impl BinaryExpr {
     }
 }
 
+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)`. A future improvement 
here would be
+        // to 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
+        if matches!(*self.left, Expr::BinaryExpr(_)) {
+            write!(f, "({})", self.left)?;
+        } else {
+            write!(f, "{}", self.left)?;
+        }
+        write!(f, " {} ", self.op)?;
+        if matches!(*self.right, Expr::BinaryExpr(_)) {
+            write!(f, "({})", self.right)?;
+        } else {
+            write!(f, "{}", self.right)?;
+        }
+        Ok(())
+    }
+}

Review Comment:
   I am torn between doing the bare minimum (the current PR changes) and 
implementing this fully with operator precedence. This would be much more work, 
and we would need comprehensive tests. Maybe there is some middle ground where 
we respect operator precedence for the common cases (AND/OR and +.-.*./) and 
just wrap anything else in parentheses. We can then improve this over time. 
I'll try this approach tomorrow.



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