You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/10/18 20:07:10 UTC
[GitHub] [arrow-datafusion] Dandandan commented on a diff in pull request #3884: Improve formatting of binary expressions
Dandandan commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r998658717
##########
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 think to avoid too much parentheses we have to take into account the operator precedence.
E.g. not put parentheses in these expressions:
`a AND b OR c AND d`
`a * 10 + a * 3`
etc.
--
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