You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "HyukjinKwon (via GitHub)" <gi...@apache.org> on 2023/08/29 04:59:29 UTC

[GitHub] [spark] HyukjinKwon commented on a diff in pull request #42715: [SPARK-45001][PYTHON][CONNECT] Implement DataFrame.foreachPartition

HyukjinKwon commented on code in PR #42715:
URL: https://github.com/apache/spark/pull/42715#discussion_r1308196424


##########
python/pyspark/sql/connect/dataframe.py:
##########
@@ -2005,6 +2001,31 @@ def mapInArrow(
 
     mapInArrow.__doc__ = PySparkDataFrame.mapInArrow.__doc__
 
+    def foreachPartition(self, f: Callable[[Iterator[Row]], None]) -> None:
+        assert self._plan is not None
+
+        schema = self.schema
+        field_converters = [
+            ArrowTableToRowsConversion._create_converter(f.dataType) for f in schema.fields
+        ]
+
+        def foreach_partition_func(itr: Iterator[pa.ArrowBatch]) -> None:
+            def flatten() -> Iterator[Row]:
+                for table in itr:
+                    columnar_data = [column.to_pylist() for column in table.columns]
+                    for i in range(0, table.num_rows):
+                        values = [
+                            field_converters[j](columnar_data[j][i])
+                            for j in range(table.num_columns)
+                        ]
+                        yield _create_row(fields=schema.fieldNames(), values=values)
+
+            f(flatten())
+
+        self.mapInArrow(foreach_partition_func, schema=StructType())

Review Comment:
   yeah .. :-).



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