sigmod commented on a change in pull request #32030:
URL: https://github.com/apache/spark/pull/32030#discussion_r609857814



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
##########
@@ -246,11 +246,50 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] 
extends Product {
     arr
   }
 
+  private def childrenTheSame(
+      originalChildren: IndexedSeq[BaseType], newChildren: 
IndexedSeq[BaseType]): Boolean = {
+    val size = originalChildren.size
+    var i = 0
+    var childrenTheSame = true
+    while (i < size && childrenTheSame) {

Review comment:
       can we use early return in the loop body?
   
   (it seems more readable to me)

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
##########
@@ -246,11 +246,50 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] 
extends Product {
     arr
   }
 
+  private def childrenTheSame(
+      originalChildren: IndexedSeq[BaseType], newChildren: 
IndexedSeq[BaseType]): Boolean = {
+    val size = originalChildren.size
+    var i = 0
+    var childrenTheSame = true
+    while (i < size && childrenTheSame) {
+      childrenTheSame &&= originalChildren(i) fastEquals newChildren(i)
+      i += 1
+    }
+    childrenTheSame
+  }
+
+  // This is a temporary solution, we will change the type of children to 
IndexedSeq in a
+  // followup PR
+  private def asIndexedSeq(seq: Seq[BaseType]): IndexedSeq[BaseType] = {
+    if (seq.isInstanceOf[IndexedSeq[BaseType]]) {
+      seq.asInstanceOf[IndexedSeq[BaseType]]
+    } else {
+      seq.toIndexedSeq
+    }
+  }
+
+  def withNewChildren(newChildren: Seq[BaseType]): BaseType = {

Review comment:
       Mark this function as final?

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
##########
@@ -246,11 +246,50 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] 
extends Product {
     arr
   }
 
+  private def childrenTheSame(
+      originalChildren: IndexedSeq[BaseType], newChildren: 
IndexedSeq[BaseType]): Boolean = {
+    val size = originalChildren.size
+    var i = 0
+    var childrenTheSame = true
+    while (i < size && childrenTheSame) {
+      childrenTheSame &&= originalChildren(i) fastEquals newChildren(i)
+      i += 1
+    }
+    childrenTheSame
+  }
+
+  // This is a temporary solution, we will change the type of children to 
IndexedSeq in a
+  // followup PR
+  private def asIndexedSeq(seq: Seq[BaseType]): IndexedSeq[BaseType] = {
+    if (seq.isInstanceOf[IndexedSeq[BaseType]]) {
+      seq.asInstanceOf[IndexedSeq[BaseType]]
+    } else {
+      seq.toIndexedSeq
+    }
+  }
+
+  def withNewChildren(newChildren: Seq[BaseType]): BaseType = {
+    val childrenIndexedSeq = asIndexedSeq(children)
+    val newChildrenIndexedSeq = asIndexedSeq(newChildren)
+    assert(newChildrenIndexedSeq.size == childrenIndexedSeq.size, "Incorrect 
number of children")
+    if (childrenIndexedSeq.isEmpty || childrenTheSame(newChildrenIndexedSeq, 
childrenIndexedSeq)) {
+      this
+    } else {
+      CurrentOrigin.withOrigin(origin) {
+        val res = withNewChildrenInternal(newChildrenIndexedSeq)
+        res.copyTagsFrom(this)
+        res
+      }
+    }
+  }
+
+  protected def withNewChildrenInternal(newChildren: IndexedSeq[BaseType]): 
BaseType
+
   /**
    * Returns a copy of this node with the children replaced.
    * TODO: Validate somewhere (in debug mode?) that children are ordered 
correctly.
    */
-  def withNewChildren(newChildren: Seq[BaseType]): BaseType = {
+  def legacyWithNewChildren(newChildren: Seq[BaseType]): BaseType = {

Review comment:
       mark this function as final?

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
##########
@@ -246,11 +246,50 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] 
extends Product {
     arr
   }
 
+  private def childrenTheSame(

Review comment:
       Nit:
   childrenFastEquals
   
   (so as to be consistent with the use of "fastEquals")




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