Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,10 @@ def __datafusion_table_provider__(self, session: Any | None = None) -> IcebergDa
).__datafusion_table_provider__
return provider(session)

def __arrow_c_stream__(self, requested_schema: object | None = None) -> object:
"""Export this table as an Arrow C stream through the PyCapsule interface."""
return self.scan().to_arrow_batch_reader().__arrow_c_stream__(requested_schema)


class StaticTable(Table):
"""Load a table directly from a metadata file (i.e., without using a catalog)."""
Expand Down Expand Up @@ -2677,6 +2681,10 @@ def to_arrow_batch_reader(self, dictionary_columns: tuple[str, ...] = ()) -> pa.
self, self.projection(), self.plan_files(), dictionary_columns=dictionary_columns
)

def __arrow_c_stream__(self, requested_schema: object | None = None) -> object:
"""Export this scan as an Arrow C stream through the PyCapsule interface."""
return self.to_arrow_batch_reader().__arrow_c_stream__(requested_schema)

def count(self) -> int:
from pyiceberg.io.pyarrow import ArrowScan

Expand Down Expand Up @@ -2856,6 +2864,10 @@ def to_arrow_batch_reader(self) -> pa.RecordBatchReader:
"""
return _to_arrow_batch_reader_via_file_scan_tasks(self, self.projection(), self.plan_files())

def __arrow_c_stream__(self, requested_schema: object | None = None) -> object:
"""Export this scan as an Arrow C stream through the PyCapsule interface."""
return self.to_arrow_batch_reader().__arrow_c_stream__(requested_schema)

def _validate_and_resolve_snapshots(self) -> tuple[int | None, int]:
"""Resolve the configured range to ``(from_snapshot_id_exclusive, to_snapshot_id_inclusive)``.

Expand Down
80 changes: 80 additions & 0 deletions tests/table/test_arrow_capsule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from pathlib import PosixPath

import pyarrow as pa
import pytest

from pyiceberg.schema import Schema
from pyiceberg.types import IntegerType, NestedField, StringType
from tests.catalog.test_base import InMemoryCatalog

SCHEMA = Schema(
NestedField(1, "id", IntegerType(), required=False),
NestedField(2, "region", StringType(), required=False),
)
ARROW_SCHEMA = pa.schema(
[
pa.field("id", pa.int32(), nullable=True),
pa.field("region", pa.string(), nullable=True),
]
)


@pytest.fixture
def catalog(tmp_path: PosixPath) -> InMemoryCatalog:
catalog = InMemoryCatalog("test.in_memory.catalog", warehouse=tmp_path.absolute().as_posix())
catalog.create_namespace("default")
return catalog


def _data(ids: list[int], regions: list[str]) -> pa.Table:
return pa.table({"id": pa.array(ids, type=pa.int32()), "region": regions}, schema=ARROW_SCHEMA)


def _rows(table: pa.Table) -> list[dict[str, object]]:
return sorted(table.to_pylist(), key=lambda row: row["id"])


def test_table_exports_arrow_c_stream(catalog: InMemoryCatalog) -> None:
tbl = catalog.create_table("default.stream_table", schema=SCHEMA)
tbl.append(_data([1, 2, 3], ["us", "eu", "us"]))

consumed = pa.table(tbl)

assert _rows(consumed) == _rows(_data([1, 2, 3], ["us", "eu", "us"]))


def test_scan_exports_arrow_c_stream_with_filter_and_projection(catalog: InMemoryCatalog) -> None:
tbl = catalog.create_table("default.stream_scan", schema=SCHEMA)
tbl.append(_data([1, 2, 3], ["us", "eu", "us"]))

consumed = pa.table(tbl.scan(row_filter="region == 'us'", selected_fields=("id",)))

assert consumed.column_names == ["id"]
assert sorted(consumed.column("id").to_pylist()) == [1, 3]


def test_incremental_append_scan_exports_arrow_c_stream(catalog: InMemoryCatalog) -> None:
tbl = catalog.create_table("default.stream_incremental", schema=SCHEMA)
tbl.append(_data([1, 2], ["us", "eu"]))
tbl.append(_data([3], ["us"]))

consumed = pa.table(tbl.incremental_append_scan())

assert _rows(consumed) == _rows(_data([1, 2, 3], ["us", "eu", "us"]))
Loading