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


##########
connector/connect/client/jvm/src/test/scala/org/apache/spark/sql/streaming/StreamingQuerySuite.scala:
##########
@@ -292,3 +310,14 @@ class TestForeachWriter[T] extends ForeachWriter[T] {
 case class TestClass(value: Int) {
   override def toString: String = value.toString
 }
+
+object StreamingQuerySuite {
+
+  // A foreach batch function for use in test above. Stores the count of 
records in a temp view.
+  def foreachBatchFnForTest1(df: DataFrame, batchId: Long): Unit = {
+    val count = df.count()
+    df.sparkSession
+      .createDataFrame(Seq((batchId, count)))
+      .createOrReplaceTempView("foreachBatchTest1")
+  }
+}

Review Comment:
   Nit: missing an empty line at the end of file



##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/StreamingForeachBatchHelper.scala:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.connect.planner
+
+import java.util.UUID
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.connect.service.SessionHolder
+
+/**
+ * A helper class for handling ForeachBatch related functionality in Spark 
Connect servers
+ */
+object StreamingForeachBatchHelper extends Logging {
+
+  type ForeachBatchFnType = (DataFrame, Long) => Unit
+
+  /**
+   * Return a new ForeachBatch function that wraps `fn`. It sets up DataFrame 
cache
+   * so that the user function can access it. The cache is cleared once 
ForeachBatch returns.
+   */
+  def dataFrameCachingWrapper(fn: ForeachBatchFnType, sessionHolder: 
SessionHolder)
+    : ForeachBatchFnType = {
+    (df: DataFrame, batchId: Long) => {
+      val dfId = UUID.randomUUID().toString
+      log.info(s"Caching DataFrame with id $dfId") // TODO: Add query id to 
the log.
+
+      // TODO: Sanity check there is no other active DataFrame for this query. 
Need to include
+      //       query id available in the cache for this check.
+
+      sessionHolder.cacheDataFrameById(dfId, df)
+      try {
+        fn(df, batchId)
+      } finally {
+        log.info(s"Removing DataFrame with id $dfId from the cache")
+        sessionHolder.removeCachedDataFrame(dfId)
+      }
+    }
+  }
+
+  /**
+   * Handles setting up Scala remote session and other Spark Connect 
environment and then
+   * runs the provided foreachBatch function `fn`.
+   *
+   * HACK ALERT: This version does not atually set up Spark connect. Directly 
passes the DataFrame,

Review Comment:
   I have 2 major questions regarding this:
   - Is the missing part about setting up a Spark Connect session and 
converting the legacy DataFrame to Spark Connect DataFrame and being executed 
inside the Spark Connect session? Do we have any Scala example on setting up 
Spark Connect session on server side and use it?
   - When is `getDataFrameOrThrow()` being called? Is it only needed for Python 
or do we need to get the DataFrame by ID inside the Spark Connect session for 
Scala?



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