Repository navigation
[SPARK-54337][PS] Add support for PyCapsule to Pyspark - #53391
devin-petersohn wants to merge 19 commits into
Conversation
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com> Co-authored-by: Devin Petersohn <devin.petersohn@snowflake.com>
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
|
Okay I think the title is a bit misleading - the PR is not about Also I believe Before digging into the code details too much, I think we need to discuss about the general direction. If we are going to implement either protocol, we probably want to do it right. The current implementation basically ignores a lot of the required methods and implemented the minimum amount of methods to make the Then about arrow. Do we want to rely on undocumented arrow classes for spark to work? Both If I understand the protocol correctly, you don't require arrow at all to implement Anyway this is a very interesting feature, but also a big commitment. Users will expect this to work properly if we do this, so I think we need to give it more thoughts before going forward. |
|
Thanks for the comment @gaogaotiantian. From my understanding, I am okay removing What do you think about removing |
|
So I'm not that familiar with the standard itself - I don't see From the documentation on Arrow, the Arrow PyCapsule Interface is still experimental. I'm not sure what's the policy for features at such stage. For the implemtation itself, |
This is a requirement of Pyspark |
Agreed, I will remove |
Yeah I don't quite understand why that's the case. Like I said that might be the lack of understanding of arrow or pyspark. Someone else might be more familiar with the code and see if this is necessary. It seems like we copied/transformed the data a few times. |
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
…fer. Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
| byte_df = df.mapInArrow(batch_to_bytes_iter, binary_schema) | ||
| # A row is actually a batch of data in Arrow IPC format. Fetch the batches one by one. | ||
| for row in byte_df.toLocalIterator(): | ||
| with pyarrow.ipc.open_stream(row.arrow_ipc_bytes) as reader: |
There was a problem hiding this comment.
In the future, I feel we can optimize out this UDF execution mapInArrow:
1, use some helper methods like Dataset.toArrowBatchRdd for classic dataframe;
2, use some internal methods inside spark.client.to_table for connect dataframe.
This can be done in followup PRs.
There was a problem hiding this comment.
I agree, and we should make sure that we leverage the streaming nature of the PyCapsule protocol in the design.
Co-authored-by: Takuya UESHIN <ueshin@happy-camper.st>
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
gaogaotiantian
left a comment
There was a problem hiding this comment.
Forgive to ask something stupid, just trying to figure out this - batch is actually the same as arrow_batch right? We need to encode it to an IPC bytes then decode because batch_to_bytes_iter runs in a worker and _get_arrow_array_partition_stream runs in the driver?
| DUCKDB_TESTS = False | ||
|
|
||
|
|
||
| @unittest.skipIf(not DUCKDB_TESTS, "duckdb is not installed") |
There was a problem hiding this comment.
Is the only way to test this with duckdb? We are introducing a new library just to test a feature. If it implements an arrow protocol, shouldn't we just be able to test with arrow or something?
There was a problem hiding this comment.
I added a test for arrow specifically, but I think it's still worth checking duckdb. arrow -> arrow is good to test as well, but there is power in testing integration. We can add more over time, but duckdb is a solid one to start with.
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
Yes, generally correct. The workers convert their data to serialized arrow (done in |
|
Gentle request for another review pass. |
|
@devin-petersohn it's holiday season in the US so plenty of the reviewers I knew are on vacation :) |
|
Are there anything other changes needed to get this one across the finish line? |
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
|
thanks @devin-petersohn |
What changes were proposed in this pull request?
Add support for Pycapsule protocol to facilitate interchange between Spark and other Python libraries. Here is a demo of what this enables with Polars and DuckDB:
Polars will now be able to consume a full Pyspark dataframe (or
pyspark.pandas), and DuckDB can consume a stream built from the Pyspark dataframe. Importantly, thestream = pa.RecordBatchReader.from_stream(psdf)line does not trigger any computation, it simply creates a stream object which is incrementally consumed by DuckDB when thefetchallcall is executed.Why are the changes needed?
Currently, Pyspark (and to a lesser degree Pyspark pandas) does not integrate well with the broader Python ecosystem. Currently, the best practice is to go through pandas with
toPandas, but that materializes all data on the driver all at once. This new API and protocol allows data to stream, one Arrow Batch at a time, enabling libraries like DuckDB and Polars to consume the data as a stream.Does this PR introduce any user-facing change?
Yes, new user-level API.
How was this patch tested?
Locally
Was this patch authored or co-authored using generative AI tooling?
No