Repository navigation
feat: support arrow pycapsule streams - #3447
Conversation
|
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions. |
271f50d to
38e61ea
Compare
Adopt the Arrow PyCapsule interface on both sides of the read/write boundary. append/overwrite accept any object implementing __arrow_c_stream__ (coerced to a streaming RecordBatchReader), and Table plus every scan expose __arrow_c_stream__ via BaseScan (not just DataScan) so any Arrow consumer can ingest them.
38e61ea to
6b1fbd8
Compare
|
Still active - happy to address any feedback. |
|
|
||
| def _coerce_arrow_input(df: pa.Table | pa.RecordBatchReader | ArrowStreamExportable) -> pa.Table | pa.RecordBatchReader: | ||
| """Normalize Arrow write input to a pa.Table or pa.RecordBatchReader. | ||
|
|
||
| Native pyarrow inputs pass through unchanged; any object implementing the | ||
| Arrow PyCapsule stream interface (``__arrow_c_stream__``) is imported as a | ||
| streaming RecordBatchReader. | ||
| """ | ||
| if isinstance(df, (pa.Table, pa.RecordBatchReader)): | ||
| return df | ||
|
|
||
| # Any object implementing the Arrow PyCapsule stream interface. | ||
| if hasattr(df, "__arrow_c_stream__"): | ||
| return pa.RecordBatchReader.from_stream(df) | ||
|
|
||
| raise ValueError( | ||
| f"Expected pa.Table, pa.RecordBatchReader, or an object implementing the " | ||
| f"Arrow PyCapsule interface (__arrow_c_stream__), got: {df!r}" | ||
| ) |
There was a problem hiding this comment.
This looks to be the core change in this PR and looks valid 👍
|
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions. |
|
Still active - happy to address any feedback. |
|
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions. |
|
still active |
|
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions. |
|
not stale |
|
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions. |
|
This pull request has been closed due to lack of activity. This is not a judgement on the merit of the PR in any way. It is just a way of keeping the PR queue manageable. If you think that is incorrect, or the pull request requires review, you can revive the PR at any time. |
|
Thanks for working on this @abnobdoss! First of all, sorry for leaving this linger for so long without a review. I think this is very close. I left two comments I'd like to see addressed before merging, but we're super close. |
|
Thanks a lot @Fokko, @kylebarron and @rambleraptor for taking the time to review this. All the comments are addressed and I replied on each thread. Beyond the review comments, I re-tested with real Polars frames after merging main (PyArrow 25 now) and changed a few more things:
The description is updated to match. |
Fokko
left a comment
There was a problem hiding this comment.
Thanks @abnobdoss, this looks great! I've created #4499 as a follow up to split oversized batches on streaming writes.
PR description
Closes #2680
Closes #1655
Rationale for this change
appendandoverwriteonly accept apa.Tableorpa.RecordBatchReader, and other Arrow libraries can only read a scan throughto_arrow(). Users of Polars, arro3, nanoarrow and similar libraries have to convert to PyArrow by hand in both directions.This PR adds the Arrow PyCapsule stream interface on both sides:
appendandoverwriteaccept any object that implements__arrow_c_stream__.DataScanandIncrementalAppendScanimplement__arrow_c_stream__.A capsule object is streamed into an unpartitioned table and read into memory for a partitioned one. PyArrow inputs behave as before, and PyArrow is still used internally for the write.
Things worth knowing:
Tableitself does not implement the interface, sotbl.append(other_tbl)is still rejected.tbl.append(other_tbl.scan())is the explicit form.to_arrow()does. A write to another table would otherwise match columns by the source table's IDs.string_view,binary_view) are now cast tolarge_string/large_binaryon every write, where before this only happened for partitioned tables. Polars exports strings asstring_view, and a table written with them could not be scanned with a filter.Not in scope:
upsertanddynamic_partition_overwritestill need apa.Table.Are these changes tested?
Yes.
tests/table/test_arrow_capsule.pycovers append and overwrite from a capsule object and from a Polars dataframe, partitioned and unpartitioned tables, the schema check happening before the stream is read, and the scan export with filters, a requested schema and no field IDs.tests/io/test_pyarrow.pycovers the view type cast for unpartitioned writes.Are there any user-facing changes?
Yes, additive.
appendandoverwriteaccept Arrow PyCapsule stream objects, and scans can be passed to any Arrow consumer. The one change for existing PyArrow input is that view type columns are stored aslarge_string/large_binary.