You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/03/02 07:07:01 UTC

[GitHub] [spark] c21 commented on a change in pull request #31318: [SPARK-34222][SQL] Enhance boolean simplification rule

c21 commented on a change in pull request #31318:
URL: https://github.com/apache/spark/pull/31318#discussion_r585308290



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala
##########
@@ -157,6 +157,28 @@ trait PredicateHelper extends AliasHelper with Logging {
     }
   }
 
+  /**
+   * Builds a balanced output predicate in bottom up approach, by applying binary operator op
+   * pair by pair on input predicates exprs recursively.
+   * Example:  exprs = [a, b, c, d], op = And, returns (a And b) And (c And d)
+   * exprs = [a, b, c, d, e, f], op = And, returns ((a And b) And (c And d)) And (e And f)
+   */
+  protected def buildBalancedPredicate[Expression](
+    exprs: Seq[Expression], op: (Expression, Expression) => Expression): Expression = {
+    exprs match {
+      case Seq(expression) => expression
+      case expressions =>
+        val grouped = expressions.grouped(2).toSeq
+        val pairwiseExprs = for (g <- grouped) yield {
+          g match {
+            case Seq(a, b) => op(a, b)
+            case Seq(x) => x
+          }
+        }
+        buildBalancedPredicate(pairwiseExprs, op)

Review comment:
       nit: shall we write in a more scala way?
   
   e.g.
   
   ```
   case expressions =>
     val pairwiseExprs = expressions.grouped(2).map {
       case Seq(e1, e2) => op(e1, e2)
       case Seq(e) => e
     }
     buildBalancedPredicate(pairwiseExprs.toSeq, op)
   ```




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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org