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 16:39:57 UTC

[GitHub] [arrow-datafusion] andygrove opened a new pull request, #3884: Improve formatting of binary expressions

andygrove opened a new pull request, #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes https://github.com/apache/arrow-datafusion/issues/3865 and https://github.com/apache/arrow-datafusion/issues/3878
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   See issue for details
   
   # What changes are included in this PR?
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   Put parentheses around child binary expressions
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r998642719


##########
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:
   @Ted-Jiang @alamb  This is the main change. Would be good to get some initial feedback here before I update the remaining tests.
   
   



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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r1004534865


##########
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:
   Ah, good point



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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r1004535958


##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -671,8 +671,8 @@ mod test {
             )?
             .build()?;
 
-        let expected = "Aggregate: groupBy=[[]], aggr=[[SUM(test.a * Int32(1) - test.bInt32(1) - test.btest.bInt32(1)test.a AS test.a * Int32(1) - test.b), SUM(test.a * Int32(1) - test.bInt32(1) - test.btest.bInt32(1)test.a AS test.a * Int32(1) - test.b * Int32(1) + test.c)]]\
-        \n  Projection: test.a * Int32(1) - test.b AS test.a * Int32(1) - test.bInt32(1) - test.btest.bInt32(1)test.a, test.a, test.b, test.c\
+        let expected = "Aggregate: groupBy=[[]], aggr=[[SUM(test.a * (Int32(1) - test.b)Int32(1) - test.btest.bInt32(1)test.a AS test.a * Int32(1) - test.b), SUM(test.a * (Int32(1) - test.b)Int32(1) - test.btest.bInt32(1)test.a AS test.a * Int32(1) - test.b * (Int32(1) + test.c))]]\

Review Comment:
   I think this is related to https://github.com/apache/arrow-datafusion/issues/3786 ?



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


[GitHub] [arrow-datafusion] Ted-Jiang commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r1004027472


##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -671,8 +671,8 @@ mod test {
             )?
             .build()?;
 
-        let expected = "Aggregate: groupBy=[[]], aggr=[[SUM(test.a * Int32(1) - test.bInt32(1) - test.btest.bInt32(1)test.a AS test.a * Int32(1) - test.b), SUM(test.a * Int32(1) - test.bInt32(1) - test.btest.bInt32(1)test.a AS test.a * Int32(1) - test.b * Int32(1) + test.c)]]\
-        \n  Projection: test.a * Int32(1) - test.b AS test.a * Int32(1) - test.bInt32(1) - test.btest.bInt32(1)test.a, test.a, test.b, test.c\
+        let expected = "Aggregate: groupBy=[[]], aggr=[[SUM(test.a * (Int32(1) - test.b)Int32(1) - test.btest.bInt32(1)test.a AS test.a * Int32(1) - test.b), SUM(test.a * (Int32(1) - test.b)Int32(1) - test.btest.bInt32(1)test.a AS test.a * Int32(1) - test.b * (Int32(1) + test.c))]]\

Review Comment:
   ```
   (Int32(1) - test.b)Int32(1)
   ```
   this is a little confused, should we change it to 🤔
   ```
   (Int32(1) - test.bInt32(1))
   ```
   Maybe we can file another issue



##########
benchmarks/expected-plans/q19.txt:
##########
@@ -3,7 +3,7 @@ Projection: SUM(lineitem.l_extendedprice * Int64(1) - lineitem.l_discount) AS re
     Projection: lineitem.l_extendedprice, lineitem.l_discount
       Filter: part.p_brand = Utf8("Brand#12") AND part.p_container IN ([Utf8("SM CASE"), Utf8("SM BOX"), Utf8("SM PACK"), Utf8("SM PKG")]) AND lineitem.l_quantity >= Decimal128(Some(100),15,2) AND lineitem.l_quantity <= Decimal128(Some(1100),15,2) AND part.p_size <= Int32(5) OR part.p_brand = Utf8("Brand#23") AND part.p_container IN ([Utf8("MED BAG"), Utf8("MED BOX"), Utf8("MED PKG"), Utf8("MED PACK")]) AND lineitem.l_quantity >= Decimal128(Some(1000),15,2) AND lineitem.l_quantity <= Decimal128(Some(2000),15,2) AND part.p_size <= Int32(10) OR part.p_brand = Utf8("Brand#34") AND part.p_container IN ([Utf8("LG CASE"), Utf8("LG BOX"), Utf8("LG PACK"), Utf8("LG PKG")]) AND lineitem.l_quantity >= Decimal128(Some(2000),15,2) AND lineitem.l_quantity <= Decimal128(Some(3000),15,2) AND part.p_size <= Int32(15)
         Inner Join: lineitem.l_partkey = part.p_partkey
-          Filter: lineitem.l_quantity >= Decimal128(Some(100),15,2) AND lineitem.l_quantity <= Decimal128(Some(1100),15,2) OR lineitem.l_quantity >= Decimal128(Some(1000),15,2) AND lineitem.l_quantity <= Decimal128(Some(2000),15,2) OR lineitem.l_quantity >= Decimal128(Some(2000),15,2) AND lineitem.l_quantity <= Decimal128(Some(3000),15,2) AND lineitem.l_shipmode IN ([Utf8("AIR"), Utf8("AIR REG")]) AND lineitem.l_shipinstruct = Utf8("DELIVER IN PERSON")
+          Filter: (lineitem.l_quantity >= Decimal128(Some(100),15,2) AND lineitem.l_quantity <= Decimal128(Some(1100),15,2) OR lineitem.l_quantity >= Decimal128(Some(1000),15,2) AND lineitem.l_quantity <= Decimal128(Some(2000),15,2) OR lineitem.l_quantity >= Decimal128(Some(2000),15,2) AND lineitem.l_quantity <= Decimal128(Some(3000),15,2)) AND lineitem.l_shipmode IN ([Utf8("AIR"), Utf8("AIR REG")]) AND lineitem.l_shipinstruct = Utf8("DELIVER IN PERSON")

Review Comment:
   👍  More clearly!



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


[GitHub] [arrow-datafusion] ursabot commented on pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#issuecomment-1290951225

   Benchmark runs are scheduled for baseline = 7cba7582a67c9353dfd851243be048149c849c2c and contender = 7559c4425e6f32655c6d09e8ed17c9c51896472b. 7559c4425e6f32655c6d09e8ed17c9c51896472b is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/e22da3ce86fa4a9db4767b4d8cfc8aef...73ca8e56685c478abfd25cc8c9654dac/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/6ec376e0594d4e2ca1e5ded5c0ff24e1...f672c4a070914dae8a0443c462b0c41f/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/dd890e6fd45c4d69b39fe4083f32c8c8...5c9adcd580a2482ab3de0fd02ef12a43/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/bcdc55d2365a433cb2c8c14b31c7101b...9b384899ccde4059894f13f573fb513e/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


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


[GitHub] [arrow-datafusion] HaoYang670 commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r998958741


##########
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 guess all binary operations are **left-associative** currently, which could also be taken into account to avoid too many parentheses.



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


[GitHub] [arrow-datafusion] Dandandan commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r998659091


##########
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 see you already added this as a comment



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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r1003329721


##########
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:
   @Dandandan @HaoYang670 I have now reimplemented this to only insert parentheses when needed. PTAL when you can.



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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#discussion_r1002593235


##########
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 going to update this PR to do the right thing based on the precedence of the operators



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


[GitHub] [arrow-datafusion] andygrove commented on pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
andygrove commented on PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#issuecomment-1289515670

   @Ted-Jiang could you help 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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] Dandandan commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
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


[GitHub] [arrow-datafusion] Dandandan merged pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
Dandandan merged PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884


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


[GitHub] [arrow-datafusion] Dandandan commented on a diff in pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] Ted-Jiang commented on pull request #3884: Improve formatting of binary expressions

Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on PR #3884:
URL: https://github.com/apache/arrow-datafusion/pull/3884#issuecomment-1289938972

   > @Ted-Jiang could you help review?
   
   Sure! I will do this today later.


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