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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/EquivalentExpressions.scala
##########
@@ -93,25 +72,36 @@ class EquivalentExpressions {
    */
   private def addCommonExprs(
       exprs: Seq[Expression],
-      addFunc: Expression => Boolean = addExpr): Unit = {
-    val exprSetForAll = mutable.Set[Expr]()
-    addExprTree(exprs.head, addExprToSet(_, exprSetForAll))
-
-    val candidateExprs = exprs.tail.foldLeft(exprSetForAll) { (exprSet, expr) 
=>
-      val otherExprSet = mutable.Set[Expr]()
-      addExprTree(expr, addExprToSet(_, otherExprSet))
-      exprSet.intersect(otherExprSet)
+      map: mutable.HashMap[ExpressionEquals, ExpressionStats]): Unit = {
+    assert(exprs.length > 1)
+    var localEquivalenceMap = mutable.HashMap.empty[ExpressionEquals, 
ExpressionStats]
+    addExprTree0(exprs.head, localEquivalenceMap)
+
+    exprs.tail.foreach { expr =>
+      val otherLocalEquivalenceMap = mutable.HashMap.empty[ExpressionEquals, 
ExpressionStats]
+      addExprTree0(expr, otherLocalEquivalenceMap)
+      localEquivalenceMap = localEquivalenceMap.filter { case (key, _) =>
+        otherLocalEquivalenceMap.contains(key)
+      }
     }
 
-    // Not all expressions in the set should be added. We should filter out 
the related
-    // children nodes.
-    val commonExprSet = candidateExprs.filter { candidateExpr =>
-      candidateExprs.forall { expr =>
-        expr == candidateExpr || 
expr.e.find(_.semanticEquals(candidateExpr.e)).isEmpty
+    localEquivalenceMap.foreach { case (commonExpr, state) =>
+      val possibleParents = localEquivalenceMap.filter { case (_, v) => 
v.height > state.height }
+      val notChild = possibleParents.forall { case (k, _) =>
+        k == commonExpr || k.e.find(_.semanticEquals(commonExpr.e)).isEmpty
+      }
+      if (notChild) {
+        // `commonExpr` appears in every expression in `exprs`, so we need to 
call `addExprToMap`
+        // `exprs.length` times, to make `useCount` accurate.
+        exprs.indices.foreach(_ => addExprToMap(commonExpr.e, map))

Review comment:
       `a + b + c` is not a common subexpression. You can think it as `(a + b > 
1)` and `a + b + c` both run once. Only if there is another `a + b + c`, `a + b 
+ c` will be considered a common subexpression.




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



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

Reply via email to