viirya commented on a change in pull request #25717: [SPARK-29013][SQL] 
Structurally equivalent subexpression elimination
URL: https://github.com/apache/spark/pull/25717#discussion_r324868570
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/EquivalentExpressions.scala
 ##########
 @@ -40,23 +40,50 @@ class EquivalentExpressions {
     override def hashCode: Int = e.semanticHash()
   }
 
+  /**
+   * Wrapper around an Expression that provides structural semantic equality.
+   */
+  case class StructuralExpr(e: Expression) {
+    def normalized(expr: Expression): Expression = {
+      expr.transformUp {
+        case b: BoundReference =>
+          b.copy(ordinal = -1)
+      }
+    }
+    override def equals(o: Any): Boolean = o match {
+      case other: StructuralExpr =>
+        normalized(e).semanticEquals(normalized(other.e))
+      case _ => false
+    }
+
+    override def hashCode: Int = normalized(e).semanticHash()
+  }
+
+  type EquivalenceMap = mutable.HashMap[Expr, mutable.ArrayBuffer[Expression]]
+
   // For each expression, the set of equivalent expressions.
   private val equivalenceMap = mutable.HashMap.empty[Expr, 
mutable.ArrayBuffer[Expression]]
 
+  // For each expression, the set of structurally equivalent expressions.
+  // Among expressions with same structure, there are different sub-set of 
expressions
+  // which are semantically different to each others. Thus, under each key, 
the value is
+  // the map structure used to do semantically sub-expression elimination.
+  private val structEquivalenceMap = mutable.HashMap.empty[StructuralExpr, 
EquivalenceMap]
+
   /**
    * Adds each expression to this data structure, grouping them with existing 
equivalent
    * expressions. Non-recursive.
    * Returns true if there was already a matching expression.
    */
-  def addExpr(expr: Expression): Boolean = {
+  def addExpr(expr: Expression, exprMap: EquivalenceMap = 
this.equivalenceMap): Boolean = {
 
 Review comment:
   addExpr is also used at PhysicalAggregation:
   
   
https://github.com/apache/spark/blob/2f3997fddca44a89b3b8e8b9cc114e26ee4c1d39/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala#L222-L229

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


With regards,
Apache Git Services

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

Reply via email to