Skip to content

Commit be3cd92

Browse files
committed
feat: Support passing kwargs to polars.scan_iceberg in Table.to_polars (#3128)
1 parent 068aae5 commit be3cd92

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎pyiceberg/table/__init__.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,15 +1896,19 @@ def to_bodo(self) -> bd.DataFrame:
18961896

18971897
return bd.read_iceberg_table(self)
18981898

1899-
def to_polars(self) -> pl.LazyFrame:
1899+
def to_polars(self, **kwargs: Any) -> pl.LazyFrame:
19001900
"""Lazily read from this Apache Iceberg table.
19011901
1902+
Args:
1903+
**kwargs: Additional keyword arguments to forward to :func:`polars.scan_iceberg`
1904+
(e.g. ``storage_options``, ``snapshot_id``, ``reader_override``).
1905+
19021906
Returns:
1903-
pl.LazyFrame: Unmaterialized Polars LazyFrame created from the Iceberg table
1907+
pl.LazyFrame: Unmaterialized Polars LazyFrame created from the Iceberg table.
19041908
"""
19051909
import polars as pl
19061910

1907-
return pl.scan_iceberg(self)
1911+
return pl.scan_iceberg(self, **kwargs)
19081912

19091913
def __datafusion_table_provider__(self, session: Any | None = None) -> IcebergDataFusionTable:
19101914
"""Return the DataFusion table provider PyCapsule interface.

‎tests/table/test_init.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,3 +2067,29 @@ def _spy(*args: Any, **kwargs: Any) -> FileIO:
20672067

20682068
assert seen_locations, "expected at least one load_file_io call"
20692069
assert all(loc is not None for loc in seen_locations), f"load_file_io called without a location: {seen_locations}"
2070+
2071+
2072+
def test_table_to_polars_forwards_kwargs(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None:
2073+
import polars as pl
2074+
2075+
recorded_calls: list[tuple[Any, dict[str, Any]]] = []
2076+
2077+
def mock_scan_iceberg(source: Any, **kwargs: Any) -> Any:
2078+
recorded_calls.append((source, kwargs))
2079+
return "mock_lazy_frame"
2080+
2081+
monkeypatch.setattr(pl, "scan_iceberg", mock_scan_iceberg)
2082+
2083+
# Test without kwargs
2084+
res1 = table_v2.to_polars()
2085+
assert res1 == "mock_lazy_frame"
2086+
assert recorded_calls[0] == (table_v2, {})
2087+
2088+
# Test with kwargs
2089+
storage_options = {"s3": {"endpoint": "https://s3.custom.endpoint"}}
2090+
res2 = table_v2.to_polars(storage_options=storage_options, snapshot_id=12345, reader_override="native")
2091+
assert res2 == "mock_lazy_frame"
2092+
assert recorded_calls[1] == (
2093+
table_v2,
2094+
{"storage_options": storage_options, "snapshot_id": 12345, "reader_override": "native"},
2095+
)

0 commit comments

Comments
 (0)