Skip to content
Merged
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
26 changes: 22 additions & 4 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(See

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.
)

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).
Expand Down
25 changes: 25 additions & 0 deletions tests/table/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down