You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "xinrong-meng (via GitHub)" <gi...@apache.org> on 2023/05/10 16:43:45 UTC

[GitHub] [spark] xinrong-meng commented on a diff in pull request #41026: [SPARK-43132] [SS] [CONNECT] Add DataStreamWriter foreach() API

xinrong-meng commented on code in PR #41026:
URL: https://github.com/apache/spark/pull/41026#discussion_r1190166838


##########
python/pyspark/sql/utils.py:
##########
@@ -119,6 +124,79 @@ class Java:
         implements = ["org.apache.spark.sql.execution.streaming.sources.PythonForeachBatchFunction"]
 
 
+def construct_foreach_function(f: Union[Callable[[Row], None], "SupportsProcess"]):
+    from pyspark.taskcontext import TaskContext
+
+    if callable(f):
+        # The provided object is a callable function that is supposed to be called on each row.
+        # Construct a function that takes an iterator and calls the provided function on each
+        # row.
+        def func_without_process(_: Any, iterator: Iterator) -> Iterator:
+            for x in iterator:
+                f(x)  # type: ignore[operator]
+            return iter([])
+
+        return func_without_process
+
+    else:
+        # The provided object is not a callable function. Then it is expected to have a
+        # 'process(row)' method, and optional 'open(partition_id, epoch_id)' and
+        # 'close(error)' methods.
+
+        if not hasattr(f, "process"):
+            raise AttributeError("Provided object does not have a 'process' method")

Review Comment:
   How about raising a `PySparkAttributeError`?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org