bogao007 commented on code in PR #41558:
URL: https://github.com/apache/spark/pull/41558#discussion_r1227308844


##########
connector/connect/common/src/main/scala/org/apache/spark/sql/connect/common/UdfUtils.scala:
##########
@@ -95,6 +96,26 @@ private[sql] object UdfUtils extends Serializable {
       }
   }
 
+  def mapValuesAdaptor[K, V, S, U, IV](
+      f: (K, Iterator[V], GroupState[S]) => U,
+      valueMapFunc: IV => V): (K, Iterator[IV], GroupState[S]) => U = {
+    (k: K, itr: Iterator[IV], s: GroupState[S]) =>
+    {
+      f(k, itr.map(v => valueMapFunc(v)), s)
+    }
+  }
+
+  def mapGroupsWithStateFuncToScalaFunc[K, V, S, U](
+      f: MapGroupsWithStateFunction[K, V, S, U]): (K, Iterator[V], 
GroupState[S]) => U = {
+    (key, data, groupState) => f.call(key, data.asJava, groupState)
+  }
+
+  def flatMapGroupsWithStateFuncToScalaFunc[K, V, S, U](

Review Comment:
   Will include those, thanks!



##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/KeyValueGroupedDataset.scala:
##########
@@ -572,6 +726,101 @@ private class KeyValueGroupedDatasetImpl[K, V, IK, IV](
     agg(aggregator)
   }
 
+  override def mapGroupsWithState[S: Encoder, U: Encoder](
+      timeoutConf: GroupStateTimeout,
+      initialState: KeyValueGroupedDataset[K, S])(
+      func: (K, Iterator[V], GroupState[S]) => U): Dataset[U] = {
+    val nf = if (valueMapFunc == UdfUtils.identical()) {
+      func
+    } else {
+      UdfUtils.mapValuesAdaptor(func, valueMapFunc)
+    }
+    val outputEncoder = encoderFor[U]
+    sparkSession.newDataset[U](outputEncoder) { builder =>
+      builder.getFlatMapGroupsWithStateBuilder
+        .setInput(plan.getRoot)
+        .addAllGroupingExpressions(groupingExprs)
+        .setFunc(getUdf(nf, outputEncoder)(ivEncoder))
+        .setIsMapGroupsWithState(true)
+        .setOutputMode(OutputMode.Update.toString)
+        .setTimeoutConf(timeoutConf.toString)
+        .addAllInitialGroupingExpressions(initialState.getGroupingExpressions)
+    }
+  }
+
+  override def mapGroupsWithState[S: Encoder, U: Encoder](timeoutConf: 
GroupStateTimeout)(
+    func: (K, Iterator[V], GroupState[S]) => U): Dataset[U] = {

Review Comment:
   This actually failed the scalastyle check, will run the scalastyle check 
locally, that should fix all these kind of formatting issues



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