anishshri-db commented on code in PR #44903:
URL: https://github.com/apache/spark/pull/44903#discussion_r1473508849


##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateSuite.scala:
##########
@@ -59,6 +60,78 @@ class RunningCountStatefulProcessor extends 
StatefulProcessor[String, String, (S
   override def close(): Unit = {}
 }
 
+class RunningCountMostRecentStatefulProcessor
+  extends StatefulProcessor[String, (String, String), (String, String, String)]
+    with Logging {
+  @transient private var _countState: ValueState[Long] = _
+  @transient private var _mostRecent: ValueState[String] = _
+  @transient var _processorHandle: StatefulProcessorHandle = _
+
+  override def init(
+       handle: StatefulProcessorHandle,
+       outputMode: OutputMode) : Unit = {
+    _processorHandle = handle
+    assert(handle.getQueryInfo().getBatchId >= 0)
+    _countState = _processorHandle.getValueState[String, Long]("countState",
+      Encoders.STRING)
+    _mostRecent = _processorHandle.getValueState[String, String]("mostRecent",
+      Encoders.STRING)
+  }
+
+  override def handleInputRows(
+      key: String,
+      inputRows: Iterator[(String, String)],
+      timerValues: TimerValues): Iterator[(String, String, String)] = {
+    val count = _countState.getOption().getOrElse(0L) + 1
+    val mostRecent = _mostRecent.getOption().getOrElse("")
+
+    var output = List[(String, String, String)]()
+    inputRows.foreach { row =>
+      _mostRecent.update(row._2)
+      _countState.update(count)
+      output = (key, count.toString, mostRecent) :: output
+    }
+    output.iterator
+  }
+
+  override def close(): Unit = {}
+}
+
+class RunningCountMostRecentStatefulProcessorWithDeletion
+  extends RunningCountMostRecentStatefulProcessor
+  with Logging {
+  @transient private var _countState: ValueState[Long] = _
+  @transient private var _mostRecent: ValueState[String] = _
+
+  override def init(
+     handle: StatefulProcessorHandle,
+     outputMode: OutputMode) : Unit = {
+    _processorHandle = handle
+    assert(handle.getQueryInfo().getBatchId >= 0)
+    _processorHandle.deleteIfExists("countState")
+    _countState = _processorHandle.getValueState[String, Long]("countState",

Review Comment:
   Discussed offline



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