HeartSaVioR commented on code in PR #48570:
URL: https://github.com/apache/spark/pull/48570#discussion_r1816080047


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/streaming/WatermarkTrackerSuite.scala:
##########
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.streaming
+
+import java.util.UUID
+
+import scala.collection.mutable
+
+import org.apache.spark.sql.execution.{SparkPlan, UnionExec}
+import org.apache.spark.sql.functions.timestamp_seconds
+import org.apache.spark.sql.streaming.StreamTest
+
+class WatermarkTrackerSuite extends StreamTest {
+
+  import testImplicits._
+
+  test("SPARK-50046 proper watermark advancement with dropped watermark 
nodes") {
+    val inputStream1 = MemoryStream[Int]
+    val inputStream2 = MemoryStream[Int]
+    val inputStream3 = MemoryStream[Int]
+
+    val df1 = inputStream1.toDF()
+      .withColumn("eventTime", timestamp_seconds($"value"))
+      .withWatermark("eventTime", "10 seconds")
+
+    val df2 = inputStream2.toDF()
+      .withColumn("eventTime", timestamp_seconds($"value"))
+      .withWatermark("eventTime", "20 seconds")
+
+    val df3 = inputStream3.toDF()
+      .withColumn("eventTime", timestamp_seconds($"value"))
+      .withWatermark("eventTime", "30 seconds")
+
+    val union = df1.union(df2).union(df3)
+
+    testStream(union)(
+      // just to ensure that executedPlan has watermark nodes for every stream.
+      MultiAddData(
+        (inputStream1, Seq(0)),
+        (inputStream2, Seq(0)),
+        (inputStream3, Seq(0))
+      ),
+      ProcessAllAvailable(),
+      Execute { q =>
+        val initialPlan = q.logicalPlan
+        val executedPlan = q.lastExecution.executedPlan
+
+        val tracker = WatermarkTracker(spark.conf, initialPlan)
+        tracker.setWatermark(5)
+
+        val delayMsToNodeId = executedPlan.collect {
+          case e: EventTimeWatermarkExec => e.delayMs -> e.nodeId
+        }.toMap
+
+        def setupScenario(
+            data: Map[Long, Seq[Long]])(fnToPruneSubtree: UnionExec => 
UnionExec): SparkPlan = {
+          val eventTimeStatsMap = new mutable.HashMap[Long, 
EventTimeStatsAccum]()
+          executedPlan.foreach {
+            case e: EventTimeWatermarkExec =>
+              eventTimeStatsMap.put(e.delayMs, e.eventTimeStats)
+
+            case _ =>
+          }
+
+          data.foreach { case (delayMs, values) =>
+            val stats = eventTimeStatsMap(delayMs)
+            values.foreach { value =>
+              stats.add(value)
+            }
+          }
+
+          executedPlan.transform {
+            case e: UnionExec => fnToPruneSubtree(e)
+          }
+        }
+
+        def verifyWatermarkMap(expectation: Map[UUID, Option[Long]]): Unit = {
+          expectation.foreach { case (nodeId, watermarkValue) =>
+            assert(tracker.watermarkMap(nodeId) === watermarkValue,
+            s"Watermark value for nodeId $nodeId is 
${tracker.watermarkMap(nodeId)}, where " +
+              s"we expect $watermarkValue")
+          }
+        }
+
+        // Before SPARK-50046, WatermarkTracker simply assumes that the 
watermark node won't
+        // be ever dropped, and the order of watermark nodes won't be changed. 
We don't find
+        // a case which breaks this, but it had been happening for other 
operators (e.g.
+        // PruneFilters), hence we would be better to guard against this in 
prior.

Review Comment:
   Please refer to the code comment I left for this test:
   
https://github.com/apache/spark/blob/master/sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingQueryOptimizationCorrectnessSuite.scala#L547-L570
   



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