From d9c786989570faa25de4e0b971e1d0f01bbf13b3 Mon Sep 17 00:00:00 2001 From: Sreesh Maheshwar Date: Thu, 15 Oct 2026 20:35:33 +0100 Subject: [PATCH] Add `dictionary_columns` to `IncrementalAppendScan` Arrow reads `DataScan.to_arrow` and `to_arrow_batch_reader` accept `dictionary_columns`, but the `IncrementalAppendScan` equivalents did not, even though the shared Arrow helpers already thread the option through. Forward it with the same signature and docstring as `DataScan`. Co-Authored-By: Claude Opus 5.5 --- pyiceberg/table/__init__.py | 26 ++++++++++++++++++++++---- tests/table/test_init.py | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index 1c6d4c3a33..1821f97ef5 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -2880,28 +2880,46 @@ def plan_files(self) -> Iterable[FileScanTask]: ), ) - def to_arrow(self) -> pa.Table: + def to_arrow(self, dictionary_columns: tuple[str, ...] = ()) -> pa.Table: """Read an Arrow table eagerly from this IncrementalAppendScan. All rows will be loaded into memory at once. + Args: + dictionary_columns: + A tuple of column names that PyArrow should read as + dictionary-encoded (``pa.DictionaryArray``). Dictionary + encoding can substantially reduce memory usage for columns + with low-cardinality repeated string values. + Only applies to Parquet files; silently ignored for ORC. + Returns: pa.Table: Materialized Arrow Table from the Iceberg table's IncrementalAppendScan """ - return _to_arrow_via_file_scan_tasks(self, self.projection(), self.plan_files()) + return _to_arrow_via_file_scan_tasks(self, self.projection(), self.plan_files(), dictionary_columns=dictionary_columns) - def to_arrow_batch_reader(self) -> pa.RecordBatchReader: + def to_arrow_batch_reader(self, dictionary_columns: tuple[str, ...] = ()) -> pa.RecordBatchReader: """Return an Arrow RecordBatchReader from this IncrementalAppendScan. For large results, using a RecordBatchReader requires less memory than loading an Arrow Table for the same IncrementalAppendScan, because a RecordBatch is read one at a time. + Args: + dictionary_columns: + A tuple of column names that PyArrow should read as + dictionary-encoded (``pa.DictionaryArray``). Dictionary + encoding can substantially reduce memory usage for columns + with low-cardinality repeated string values. + Only applies to Parquet files; silently ignored for ORC. + Returns: pa.RecordBatchReader: Arrow RecordBatchReader from the Iceberg table's IncrementalAppendScan which can be used to read a stream of record batches one by one. """ - return _to_arrow_batch_reader_via_file_scan_tasks(self, self.projection(), self.plan_files()) + return _to_arrow_batch_reader_via_file_scan_tasks( + self, self.projection(), self.plan_files(), dictionary_columns=dictionary_columns + ) def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: """Export this scan's result as an Arrow C stream (PyCapsule interface). diff --git a/tests/table/test_init.py b/tests/table/test_init.py index b26b4db4f6..437073234a 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -739,6 +739,31 @@ def test_incremental_append_scan_staged_table_raises(table_v2: Table) -> None: staged_table.incremental_append_scan() +def test_incremental_append_scan_dictionary_columns(catalog: Catalog) -> None: + import pyarrow as pa + + from pyiceberg.io.pyarrow import schema_to_pyarrow + + catalog.create_namespace("default") + schema = Schema( + NestedField(1, "id", IntegerType(), required=False), + NestedField(2, "label", StringType(), required=False), + ) + table = catalog.create_table("default.incremental_dictionary_columns", schema=schema) + for rows in ([(1, "a")], [(2, "b"), (3, "b")], [(4, "a")]): + table.append(pa.Table.from_pylist([{"id": i, "label": label} for i, label in rows], schema=schema_to_pyarrow(schema))) + + # Excludes the first append, so the scan spans the files added by the last two. + scan = table.incremental_append_scan(from_snapshot_id_exclusive=table.snapshots()[0].snapshot_id) + for result in ( + scan.to_arrow(dictionary_columns=("label",)), + scan.to_arrow_batch_reader(dictionary_columns=("label",)).read_all(), + ): + assert pa.types.is_dictionary(result.schema.field("label").type) + assert result.schema.field("id").type == pa.int32() + assert sorted(zip(result["id"].to_pylist(), result["label"].to_pylist(), strict=True)) == [(2, "b"), (3, "b"), (4, "a")] + + def test_static_table_same_as_table(table_v2: Table, metadata_location: str) -> None: static_table = StaticTable.from_metadata(metadata_location) assert isinstance(static_table, Table)