feat: support arrow pycapsule streams#3447
Open
abnobdoss wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2680
Closes #1655
Rationale for this change
PyIceberg is coupled to PyArrow at its read/write boundary:
append/overwritereject anything that isn't apa.Table/pa.RecordBatchReader, and external Arrow consumers can't read a table/scan withoutto_arrow(). Users of other Arrow-native libraries (polars, arro3, nanoarrow, …) therefore have to convert to PyArrow explicitly.This PR adopts the Arrow PyCapsule interface on both sides:
append/overwriteaccept any object implementing__arrow_c_stream__, in addition to PyArrow types.TableandDataScanimplement__arrow_c_stream__, so they can be handed to any Arrow consumer.Native PyArrow inputs are unchanged; any other producer is imported as a streaming
RecordBatchReader, so streaming is preserved. PyArrow stays an internal write dependency; this only removes the requirement that the caller use PyArrow.One small writer-side adjustment falls out of this: bin-packing still prefers Arrow's logical
nbytesestimate, but falls back to referenced buffer size for Arrow view types likestring_view, which current Polars exports can produce and PyArrow cannot always size withnbytes.Not in scope:
upsert/dynamic_partition_overwritestill require a materializedpa.Table(they do random access / joins and don't accept aRecordBatchReadertoday). A PyCapsule producer toappend/overwriteon a partitioned table raisesNotImplementedError, the same restriction that already applies topa.RecordBatchReader, since the producer is consumed as a reader and streaming writes to partitioned tables aren't supported. A materializedpa.Tableis unaffected.Are these changes tested?
Yes.
tests/table/test_arrow_capsule.py(runs undermake test, no Docker) covers coercion-helper branches;appendover all input forms (pa.Table, reader, single- and multi-batch PyCapsule producers);overwritewith a producer; the native-pa.Table-on-partitioned regression;pa.table(table)/pa.table(table.scan())round-trips plus filter/projection; and thedst.append(src.scan())round-trip.tests/io/test_pyarrow.pycovers thestring_viewbin-packing fallback.Are there any user-facing changes?
Yes, additive and backwards compatible.
append/overwriteaccept Arrow PyCapsule producers (__arrow_c_stream__), andTable/DataScanimplement__arrow_c_stream__so they can be passed to any Arrow consumer (e.g.pa.table(...),polars.DataFrame(...)). No change for existing PyArrow inputs.