bhollis-dbx commented on code in PR #58800:
URL: https://github.com/apache/spark/pull/58800#discussion_r4075567492


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/trees/TreeNodeSuite.scala:
##########
@@ -180,6 +181,76 @@ class TreeNodeSuite extends SparkFunSuite with SQLHelper {
     assert(actual === expect)
   }
 
+  test("mapChildren returns the original node when all children are fast 
equal") {
+    val expression = Coalesce(Seq(Literal(1), Literal(2)))
+    val visited = new ArrayBuffer[Int]()
+    val result = expression.mapChildren {
+      case literal @ Literal(value: Int, _) =>
+        visited += value
+        literal
+      case other => other
+    }
+
+    assert(result eq expression)
+    assert(visited == Seq(1, 2))
+    val leaf = Dummy(None)
+    assert(leaf.mapChildren(identity) eq leaf)
+  }
+
+  test("mapChildren returns the original node when every child is equal but a 
distinct copy") {
+    val expression = Coalesce(Seq(Literal(1), Literal(2)))
+    // Return a fresh, structurally-equal (not reference-equal) copy for 
*every* child: this
+    // fills `equalCopies` yet must still return `this`, since no child 
materially changes.
+    val result = expression.mapChildren {
+      case Literal(value: Int, dt) => Literal(value, dt)
+      case other => other
+    }
+    assert(result eq expression)
+  }
+
+  test("mapChildren retains an equal replacement when another child changes") {
+    val tag = TreeNodeTag[String]("equal-copy")
+    val expression = Coalesce(Seq(Literal(1), Literal(2)))
+    val equalCopy = Literal(1)
+    equalCopy.setTagValue(tag, "retained")
+
+    val result = expression.mapChildren {
+      case Literal(1, _) => equalCopy
+      case Literal(2, _) => Literal(3)
+      case other => other
+    }
+
+    assert(result.children.head eq equalCopy)
+    assert(result.children.head.getTagValue(tag).contains("retained"))
+    assert(result.children(1) == Literal(3))
+  }
+
+  test("mapChildren retains non-adjacent equal replacements when a later child 
changes") {
+    val c0 = Literal(10)
+    val c1 = Literal(11)
+    val c2 = Literal(12)
+    val c3 = Literal(13)
+    val expression = Coalesce(Seq(c0, c1, c2, c3))
+    val copy0 = Literal(10)
+    val copy2 = Literal(12)
+
+    // Equal-but-distinct copies at non-adjacent indices 0 and 2, an unchanged 
same instance at 1,
+    // and a material change at 3. Exercises the replay loop's index 
bookkeeping across a gap.

Review Comment:
   The test now keeps c0 reference-equal before copy1, exercising prefix 
backfill. It also asserts all five child identities/order and retains a later 
material change at c4.



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