You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "Kimahriman (via GitHub)" <gi...@apache.org> on 2024/11/28 13:54:18 UTC
[PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Kimahriman opened a new pull request, #49005:
URL: https://github.com/apache/spark/pull/49005
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
Add the option to `applyInArrow` to take a function that takes an iterator of `RecordBatch` and returns an iterator of `RecordBatch`, and respect `spark.sql.execution.arrow.maxRecordsPerBatch` on the input iterator.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
Being limited to returning a single Table requires collecting all results in memory for a single batch. This can require excessive memory for certain edge cases with large groups. Currently the Python worker immediately converts a table into it's underlying batches, so there's barely any changes required to accommodate this. There are larger changes required to support max records per batch on the input side.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
Yes, a new function signature supported by `applyInArrow`
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
Updated existing UTs to test both Table signatures and RecordBatch signatures
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
No
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-3288608490
I can try to update with new eval types and using type hints as the mechanism. I do think your update will fit in fine, as its just a different way to implement the JVM side batching that I implemented here. Basically you just used a trait instead of a subclass. I do think it would still be beneficial for both batched and non-batched to go through the same code path, otherwise including more eval types will make it even more complicated
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-3327875321
I have a new version of this with a new eval type and using type hints ready to go after https://github.com/apache/spark/pull/52303 gets merged
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman closed pull request #49005: [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow`
URL: https://github.com/apache/spark/pull/49005
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1879133851
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
Okay I'll take out the Cogroup Python changes and make a separate PR for them
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1863492390
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
The problem is more on the Python side, there's no way to force the user to fully use the left side before using the right side that I can think of.
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-3283499742
> Gentle ping again, it's getting tricky to keep up with/figure out new merge conflicts each time there are conflicting changes that make it in
@Kimahriman we plan to optimize the batch size in multiple python plans (e.g. SQL_GROUPED_MAP_PANDAS_UDF /SQL_GROUPED_MAP_ARROW_UDF /SQL_GROUPED_AGG_ARROW_UDF/SQL_GROUPED_AGG_PANDAS_UDF/SQL_WINDOW_AGG_PANDAS_UDF/SQL_WINDOW_AGG_ARROW_UDF/etc), [this](https://github.com/apache/spark/pull/52303) is the first one for SQL_GROUPED_MAP_PANDAS_UDF /SQL_GROUPED_MAP_ARROW_UDF. Can the iterator API reuse this change?
Regarding this PR, I think we should:
1, exclude any changes in cogroup;
2, add a new eval type for the new iterator API, because it is a user-facing change. For example, we have `SQL_SCALAR_PANDAS_ITER_UDF` for the iterator API in Pandas UDF.
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-3000734767
Gentle ping again now that 4.0 is out
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-3330118360
Closing in favor of https://github.com/apache/spark/pull/52440
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-2593356260
Gentle ping for potential inclusion in 4.0
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1878974193
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
Sorry the main question is, do we update the `Cogrouped.applyInArrow` in this PR as well, which is _better_ than it currently is but still not completely lazy (the left side has to be eagerly loaded in memory on the Python side), or do we not touch the Cogrouped API since it's not completely optimal and address that later?
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-2506187789
@zhengruifeng @HyukjinKwon @xinrong-meng this is the alternative/follow-on to https://github.com/apache/spark/pull/48038 that includes implementing the input side JVM batching in addition to the Python API updates
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1863472521
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/FlatMapGroupsInBatchExec.scala:
##########
@@ -82,13 +83,14 @@ trait FlatMapGroupsInBatchExec extends SparkPlan with UnaryExecNode with PythonS
val data = groupedData(iter, dedupAttributes)
- val runner = new ArrowPythonRunner(
+ val runner = new GroupedArrowPythonRunner(
Review Comment:
No a single group will always be a single Table in the UDF call still, the Python side will collect all batches for a stream into a Table if using the Table API
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "ConeyLiu (via GitHub)" <gi...@apache.org>.
ConeyLiu commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-3034866648
Hi, @HyukjinKwon @zhengruifeng could you please review this again? We indeed encountered this problem in our production jobs when users call applyInPandas, which returns a large DataFrame.
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1866981882
##########
python/pyspark/sql/pandas/_typing/__init__.pyi:
##########
@@ -348,10 +349,22 @@ PandasCogroupedMapFunction = Union[
ArrowGroupedMapFunction = Union[
Callable[[pyarrow.Table], pyarrow.Table],
Callable[[Tuple[pyarrow.Scalar, ...], pyarrow.Table], pyarrow.Table],
+ Callable[[Iterator[pyarrow.RecordBatch]], Iterator[pyarrow.RecordBatch]],
Review Comment:
I thought we need a new `PythonEvalType` and Exec node for this new signature.
If we don't need them, then existing change is fine.
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1863210090
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/CoGroupedArrowPythonRunner.scala:
##########
@@ -45,91 +36,24 @@ class CoGroupedArrowPythonRunner(
leftSchema: StructType,
rightSchema: StructType,
timeZoneId: String,
+ largeVarTypes: Boolean,
+ arrowMaxRecordsPerBatch: Int,
conf: Map[String, String],
override val pythonMetrics: Map[String, SQLMetric],
jobArtifactUUID: Option[String],
profiler: Option[String])
- extends BasePythonRunner[
- (Iterator[InternalRow], Iterator[InternalRow]), ColumnarBatch](
- funcs.map(_._1), evalType, argOffsets, jobArtifactUUID)
- with BasicPythonArrowOutput {
-
- override val pythonExec: String =
- SQLConf.get.pysparkWorkerPythonExecutable.getOrElse(
- funcs.head._1.funcs.head.pythonExec)
-
- override val faultHandlerEnabled: Boolean = SQLConf.get.pythonUDFWorkerFaulthandlerEnabled
-
- override val simplifiedTraceback: Boolean = SQLConf.get.pysparkSimplifiedTraceback
-
- protected def newWriter(
- env: SparkEnv,
- worker: PythonWorker,
- inputIterator: Iterator[(Iterator[InternalRow], Iterator[InternalRow])],
- partitionIndex: Int,
- context: TaskContext): Writer = {
-
- new Writer(env, worker, inputIterator, partitionIndex, context) {
-
- protected override def writeCommand(dataOut: DataOutputStream): Unit = {
-
- // Write config for the worker as a number of key -> value pairs of strings
- dataOut.writeInt(conf.size)
- for ((k, v) <- conf) {
- PythonRDD.writeUTF(k, dataOut)
- PythonRDD.writeUTF(v, dataOut)
- }
-
- PythonUDFRunner.writeUDFs(dataOut, funcs, argOffsets, profiler)
- }
-
- override def writeNextInputToStream(dataOut: DataOutputStream): Boolean = {
- // For each we first send the number of dataframes in each group then send
- // first df, then send second df. End of data is marked by sending 0.
- if (inputIterator.hasNext) {
- val startData = dataOut.size()
- dataOut.writeInt(2)
- val (nextLeft, nextRight) = inputIterator.next()
- writeGroup(nextLeft, leftSchema, dataOut, "left")
- writeGroup(nextRight, rightSchema, dataOut, "right")
-
- val deltaData = dataOut.size() - startData
- pythonMetrics("pythonDataSent") += deltaData
- true
- } else {
- dataOut.writeInt(0)
- false
- }
- }
-
- private def writeGroup(
- group: Iterator[InternalRow],
- schema: StructType,
- dataOut: DataOutputStream,
- name: String): Unit = {
- val arrowSchema =
- ArrowUtils.toArrowSchema(schema, timeZoneId, errorOnDuplicatedFieldNames = true)
- val allocator = ArrowUtils.rootAllocator.newChildAllocator(
- s"stdout writer for $pythonExec ($name)", 0, Long.MaxValue)
- val root = VectorSchemaRoot.create(arrowSchema, allocator)
-
- Utils.tryWithSafeFinally {
- val writer = new ArrowStreamWriter(root, null, dataOut)
- val arrowWriter = ArrowWriter.create(root)
- writer.start()
-
- while (group.hasNext) {
- arrowWriter.write(group.next())
- }
- arrowWriter.finish()
- writer.writeBatch()
- writer.end()
- }{
- root.close()
- allocator.close()
- }
- }
- }
+ extends BaseGroupedArrowPythonRunner[(Iterator[InternalRow], Iterator[InternalRow])](
+ funcs, evalType, argOffsets, timeZoneId, largeVarTypes, arrowMaxRecordsPerBatch, conf,
+ pythonMetrics, jobArtifactUUID, profiler) {
+
+ override protected def writeNextGroup(
+ group: (Iterator[InternalRow], Iterator[InternalRow]),
+ dataOut: DataOutputStream): Unit = {
+ val (leftGroup, rightGroup) = group
+
+ dataOut.writeInt(2)
+ writeSingleStream(leftGroup, leftSchema, dataOut, Some("left"))
+ writeSingleStream(rightGroup, rightSchema, dataOut, Some("right"))
Review Comment:
does here means for a cogroup (key, group A, group B), write the whole group A in batches, and then write the whole group B in batches?
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/FlatMapGroupsInBatchExec.scala:
##########
@@ -82,13 +83,14 @@ trait FlatMapGroupsInBatchExec extends SparkPlan with UnaryExecNode with PythonS
val data = groupedData(iter, dedupAttributes)
- val runner = new ArrowPythonRunner(
+ val runner = new GroupedArrowPythonRunner(
Review Comment:
So for a UDF using existing `Table -> Table` signature, it will be split into batches?
I guess this might cause breaking change:
suppose a UDF doing aggregation, and a group larger than `arrowMaxRecordsPerBatch`, it outputs single row before, but will generate multiples rows after this change?
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
It still may cause memory issue, is it possible to do it in this way
```
cogroup: (Iterator[InternalRow], Iterator[InternalRow])
(left, right) = cogroup
while (left.hasNext || right.hasNext) {
if (left.hasNext) {
writeBatch(... , "left")
} else {
writeBatch(A empty Batch , "left")
}
if (right.hasNext) {
writeBatch(... , "right")
} else {
writeBatch(A empty Batch , "right")
}
}
```
##########
python/pyspark/sql/pandas/_typing/__init__.pyi:
##########
@@ -348,10 +349,22 @@ PandasCogroupedMapFunction = Union[
ArrowGroupedMapFunction = Union[
Callable[[pyarrow.Table], pyarrow.Table],
Callable[[Tuple[pyarrow.Scalar, ...], pyarrow.Table], pyarrow.Table],
+ Callable[[Iterator[pyarrow.RecordBatch]], Iterator[pyarrow.RecordBatch]],
Review Comment:
what about adding a new `ArrowGroupedMapIterFunction` for the new signature?
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-2507742395
> what about adding a new test that likely fails with `Table->Table` signature but succeeds with new iterator signature?
The only thing that should fail with the Table -> Table is something that goes out of memory
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1879112482
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
If we're going to address them in a separate PR, I am fine. Just wanted to make sure we fix it.
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1868635769
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
I hope we can address the memory issue in `applyInArrow`, since it is not completely resolved for `CoGrouped.applyInArrow`, I'd rather let it alone.
WDYT? @HyukjinKwon @xinrong-meng
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1863481467
##########
python/pyspark/sql/pandas/_typing/__init__.pyi:
##########
@@ -348,10 +349,22 @@ PandasCogroupedMapFunction = Union[
ArrowGroupedMapFunction = Union[
Callable[[pyarrow.Table], pyarrow.Table],
Callable[[Tuple[pyarrow.Scalar, ...], pyarrow.Table], pyarrow.Table],
+ Callable[[Iterator[pyarrow.RecordBatch]], Iterator[pyarrow.RecordBatch]],
Review Comment:
Sure I can do that, and then just Union them in the function signature directly? Or create like `ArrowGroupedMapTableFunction` and `ArrowGroupedMapIterFunction` and then union them to `ArrowGroupedMapFunction`?
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1863471167
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/CoGroupedArrowPythonRunner.scala:
##########
@@ -45,91 +36,24 @@ class CoGroupedArrowPythonRunner(
leftSchema: StructType,
rightSchema: StructType,
timeZoneId: String,
+ largeVarTypes: Boolean,
+ arrowMaxRecordsPerBatch: Int,
conf: Map[String, String],
override val pythonMetrics: Map[String, SQLMetric],
jobArtifactUUID: Option[String],
profiler: Option[String])
- extends BasePythonRunner[
- (Iterator[InternalRow], Iterator[InternalRow]), ColumnarBatch](
- funcs.map(_._1), evalType, argOffsets, jobArtifactUUID)
- with BasicPythonArrowOutput {
-
- override val pythonExec: String =
- SQLConf.get.pysparkWorkerPythonExecutable.getOrElse(
- funcs.head._1.funcs.head.pythonExec)
-
- override val faultHandlerEnabled: Boolean = SQLConf.get.pythonUDFWorkerFaulthandlerEnabled
-
- override val simplifiedTraceback: Boolean = SQLConf.get.pysparkSimplifiedTraceback
-
- protected def newWriter(
- env: SparkEnv,
- worker: PythonWorker,
- inputIterator: Iterator[(Iterator[InternalRow], Iterator[InternalRow])],
- partitionIndex: Int,
- context: TaskContext): Writer = {
-
- new Writer(env, worker, inputIterator, partitionIndex, context) {
-
- protected override def writeCommand(dataOut: DataOutputStream): Unit = {
-
- // Write config for the worker as a number of key -> value pairs of strings
- dataOut.writeInt(conf.size)
- for ((k, v) <- conf) {
- PythonRDD.writeUTF(k, dataOut)
- PythonRDD.writeUTF(v, dataOut)
- }
-
- PythonUDFRunner.writeUDFs(dataOut, funcs, argOffsets, profiler)
- }
-
- override def writeNextInputToStream(dataOut: DataOutputStream): Boolean = {
- // For each we first send the number of dataframes in each group then send
- // first df, then send second df. End of data is marked by sending 0.
- if (inputIterator.hasNext) {
- val startData = dataOut.size()
- dataOut.writeInt(2)
- val (nextLeft, nextRight) = inputIterator.next()
- writeGroup(nextLeft, leftSchema, dataOut, "left")
- writeGroup(nextRight, rightSchema, dataOut, "right")
-
- val deltaData = dataOut.size() - startData
- pythonMetrics("pythonDataSent") += deltaData
- true
- } else {
- dataOut.writeInt(0)
- false
- }
- }
-
- private def writeGroup(
- group: Iterator[InternalRow],
- schema: StructType,
- dataOut: DataOutputStream,
- name: String): Unit = {
- val arrowSchema =
- ArrowUtils.toArrowSchema(schema, timeZoneId, errorOnDuplicatedFieldNames = true)
- val allocator = ArrowUtils.rootAllocator.newChildAllocator(
- s"stdout writer for $pythonExec ($name)", 0, Long.MaxValue)
- val root = VectorSchemaRoot.create(arrowSchema, allocator)
-
- Utils.tryWithSafeFinally {
- val writer = new ArrowStreamWriter(root, null, dataOut)
- val arrowWriter = ArrowWriter.create(root)
- writer.start()
-
- while (group.hasNext) {
- arrowWriter.write(group.next())
- }
- arrowWriter.finish()
- writer.writeBatch()
- writer.end()
- }{
- root.close()
- allocator.close()
- }
- }
- }
+ extends BaseGroupedArrowPythonRunner[(Iterator[InternalRow], Iterator[InternalRow])](
+ funcs, evalType, argOffsets, timeZoneId, largeVarTypes, arrowMaxRecordsPerBatch, conf,
+ pythonMetrics, jobArtifactUUID, profiler) {
+
+ override protected def writeNextGroup(
+ group: (Iterator[InternalRow], Iterator[InternalRow]),
+ dataOut: DataOutputStream): Unit = {
+ val (leftGroup, rightGroup) = group
+
+ dataOut.writeInt(2)
+ writeSingleStream(leftGroup, leftSchema, dataOut, Some("left"))
+ writeSingleStream(rightGroup, rightSchema, dataOut, Some("right"))
Review Comment:
Correct, and then the Python side will eagerly collect group A and then lazily iterate group B
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1867611465
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
The JVM side for grouped map was adapted from and consolidate with the cogroup code to use an Arrow stream per group, and that has benefits of at least not collecting an entire group on the JVM side before serializing it, so I think that should stay. I can undo the Python side changes and leave only the Table -> Table API for cogroup if that's desired. There are at least _some_ improvements to this implementation of cogroup, especially with being able to return an iterator of RecordBatches
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
The JVM side for grouped map was adapted from and consolidated with the cogroup code to use an Arrow stream per group, and that has benefits of at least not collecting an entire group on the JVM side before serializing it, so I think that should stay. I can undo the Python side changes and leave only the Table -> Table API for cogroup if that's desired. There are at least _some_ improvements to this implementation of cogroup, especially with being able to return an iterator of RecordBatches
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #49005:
URL: https://github.com/apache/spark/pull/49005#discussion_r1877266775
##########
python/pyspark/sql/pandas/serializers.py:
##########
@@ -728,8 +743,9 @@ def load_stream(self, stream):
dataframes_in_group = read_int(stream)
if dataframes_in_group == 2:
+ # We need to fully load the left batches, but we can lazily load the right batches
Review Comment:
We should address memory issue, yes. that's why we allow iterator of record batches?
--
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
Re: [PR] [SPARK-49547][SQL][PYTHON] Add iterator of `RecordBatch` API to `applyInArrow` [spark]
Posted by "Kimahriman (via GitHub)" <gi...@apache.org>.
Kimahriman commented on PR #49005:
URL: https://github.com/apache/spark/pull/49005#issuecomment-3282507366
Gentle ping again, it's getting tricky to keep up with/figure out new merge conflicts each time there are conflicting changes that make it in
--
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