Skip to content

feat: support arrow pycapsule streams - #3447

Merged
Fokko merged 11 commits into
apache:mainfrom
abnobdoss:arrow-pycapsule-stream
Nov 26, 2026
Merged

Fokko merged 11 commits into
apache:mainfrom
abnobdoss:arrow-pycapsule-stream

Conversation

@abnobdoss

@abnobdoss abnobdoss commented May 31, 2026 •

Copy link
Copy Markdown
Contributor

PR description

Closes #2680
Closes #1655

Rationale for this change

append and overwrite only accept a pa.Table or pa.RecordBatchReader, and other Arrow libraries can only read a scan through to_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:

import polars as pl

df = pl.DataFrame(table.scan())
table.append(some_polars_frame)

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:

  • Table itself does not implement the interface, so tbl.append(other_tbl) is still rejected. tbl.append(other_tbl.scan()) is the explicit form.
  • The scan export drops field IDs, as to_arrow() does. A write to another table would otherwise match columns by the source table's IDs.
  • View types (string_view, binary_view) are now cast to large_string / large_binary on every write, where before this only happened for partitioned tables. Polars exports strings as string_view, and a table written with them could not be scanned with a filter.
  • A streamed batch is never split, so an in-memory dataframe that exports one large batch is written as one file on an unpartitioned table.

Not in scope: upsert and dynamic_partition_overwrite still need a pa.Table.

Are these changes tested?

Yes. tests/table/test_arrow_capsule.py covers 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.py covers the view type cast for unpartitioned writes.

Are there any user-facing changes?

Yes, additive. append and overwrite accept 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 as large_string / large_binary.

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale label Jul 1, 2026
@abnobdoss
abnobdoss force-pushed the arrow-pycapsule-stream branch from 271f50d to 38e61ea Compare July 1, 2026 02:01
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.
@abnobdoss
abnobdoss force-pushed the arrow-pycapsule-stream branch from 38e61ea to 6b1fbd8 Compare July 1, 2026 02:29
@abnobdoss

Copy link
Copy Markdown
Contributor Author

Still active - happy to address any feedback.

Comment thread pyiceberg/io/pyarrow.py Outdated
Comment on lines +3064 to +3082

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}"
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks to be the core change in this PR and looks valid 👍

@github-actions github-actions Bot removed the stale label Jul 2, 2026
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale label Aug 2, 2026
@abnobdoss

Copy link
Copy Markdown
Contributor Author

Still active - happy to address any feedback.

@github-actions github-actions Bot removed the stale label Aug 3, 2026
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale label Sep 3, 2026
@kylebarron

Copy link
Copy Markdown
Member

still active

Comment thread pyiceberg/io/pyarrow.py Resolved
Comment thread pyiceberg/io/pyarrow.py Resolved
Comment thread pyiceberg/table/__init__.py Resolved
@github-actions github-actions Bot removed the stale label Sep 4, 2026
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale label Oct 4, 2026
@kylebarron

Copy link
Copy Markdown
Member

not stale

@github-actions github-actions Bot removed the stale label Oct 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale label Nov 6, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot closed this Nov 14, 2026
@Fokko Fokko reopened this Nov 25, 2026
@Fokko Fokko removed the stale label Nov 25, 2026
@Fokko

Fokko commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

Comment thread pyiceberg/table/__init__.py Resolved
Comment thread pyiceberg/io/pyarrow.py Resolved
@abnobdoss

Copy link
Copy Markdown
Contributor Author

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:

  • Polars exports strings as string_view, and an unpartitioned table written with those could not be scanned with a filter. The view cast from PyArrow: Support view types when writing to a partitioned table #4121 now runs for every write.
  • With that cast in place, the size fallback wasn't needed anymore, so it's gone.
  • __arrow_c_stream__ is only on scans now, and it drops field IDs like to_arrow() does.

The description is updated to match.

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @abnobdoss, this looks great! I've created #4499 as a follow up to split oversized batches on streaming writes.

@Fokko
Fokko added this pull request to the merge queue Nov 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 26, 2026
@Fokko
Fokko added this pull request to the merge queue Nov 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 26, 2026
@Fokko
Fokko added this pull request to the merge queue Nov 26, 2026
Merged via the queue into apache:main with commit d6a229c Nov 26, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow Arrow Capsule Interface [feature] Investigate integrations leveraging the PyCapsule protocol

4 participants