WweiL commented on code in PR #41026: URL: https://github.com/apache/spark/pull/41026#discussion_r1190253907
########## 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: Thanks! Will do! -- 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-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org