viirya commented on a change in pull request #30245:
URL: https://github.com/apache/spark/pull/30245#discussion_r518584261



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/EquivalentExpressions.scala
##########
@@ -65,11 +65,46 @@ class EquivalentExpressions {
     }
   }
 
+  /**
+   * Adds only expressions which are common in each of given expressions, in a 
recursive way.
+   * For example, given two expressions `(a + (b + (c + 1)))` and `(d + (e + 
(c + 1)))`,
+   * the common expression `(c + 1)` will be added into `equivalenceMap`.
+   */
+  def addCommonExprs(exprs: Seq[Expression], addFunc: Expression => Boolean = 
addExpr): Unit = {
+    var exprSetForAll = ExpressionSet()
+
+    addExprTree(exprs.head, (expr: Expression) => {
+      if (exprSetForAll.contains(expr)) {
+        true
+      } else {
+        exprSetForAll += expr
+        false
+      }
+    })
+
+    exprs.tail.foreach { expr =>
+      var exprSet = ExpressionSet()
+      addExprTree(expr, (expr: Expression) => {
+        if (exprSet.contains(expr)) {
+          true
+        } else {
+          exprSet += expr
+          false
+        }
+      })
+      exprSetForAll = exprSetForAll.intersect(exprSet)
+    }

Review comment:
       For expression `head`, we add underlying expressions into 
`exprSetForAll` set. But for expressions in `tail`, we keep intersect between 
`exprSetForAll` and `exprSet`.
   
   We can merge two blocks, but in the block we need to check if current 
expression is head expression and do different logic based on the check.
   
   I prefer current one since it looks simpler.




----------------------------------------------------------------
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to