From 9a297d63f8b82a887fb7bfc526f612be137406df Mon Sep 17 00:00:00 2001 From: Sreesh Maheshwar Date: Thu, 15 Oct 2026 20:25:46 +0100 Subject: [PATCH 1/2] Docs: Document incremental append scans Add an API docs section for incremental_append_scan: a worked example, optional and inclusive range bounds, reading new rows since the last run, the shared scan arguments, and the caveat that only append snapshots are read, so it is not a changelog scan. Say the same in the scan docstrings and document the precondition of ancestors_between_ids. Co-Authored-By: Claude Opus 5.5 --- mkdocs/docs/api.md | 142 +++++++++++++++++++++++++++++++++++ pyiceberg/table/__init__.py | 10 ++- pyiceberg/table/snapshots.py | 3 +- 3 files changed, 151 insertions(+), 4 deletions(-) diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md index aa7c111329..fd050f1c85 100644 --- a/mkdocs/docs/api.md +++ b/mkdocs/docs/api.md @@ -2318,6 +2318,148 @@ DataFrame() +---+---+ ``` +## Incremental append scan + +`incremental_append_scan()` reads only the rows that were appended to a table between two snapshots, so new data can be processed without reading the whole table again. It doesn't report deletes or updates; see [Only appends are read](#only-appends-are-read). + +For example, consider a table with four appends, each of which commits a new snapshot: + +```python +import pyarrow as pa +from pyiceberg.catalog import load_catalog + +catalog = load_catalog("default") + +df = pa.Table.from_pylist([{"city": "Amsterdam", "lat": 52.371807, "long": 4.896029}]) +tbl = catalog.create_table("default.cities", schema=df.schema) +tbl.append(df) + +tbl.append(pa.Table.from_pylist([{"city": "San Francisco", "lat": 37.773972, "long": -122.431297}])) +tbl.append(pa.Table.from_pylist([{"city": "Drachten", "lat": 53.11254, "long": 6.0989}])) +tbl.append(pa.Table.from_pylist([{"city": "Paris", "lat": 48.864716, "long": 2.349014}])) + +snapshot_ids = [snapshot.snapshot_id for snapshot in tbl.snapshots()] +``` + +Read the rows appended after the first snapshot, up to and including the third: + +```python +tbl.incremental_append_scan( + from_snapshot_id_exclusive=snapshot_ids[0], + to_snapshot_id_inclusive=snapshot_ids[2], +).to_arrow() +``` + +This returns the rows of the second and third appends: + +```python +pyarrow.Table +city: string +lat: double +long: double +---- +city: [["San Francisco"],["Drachten"]] +lat: [[37.773972],[53.11254]] +long: [[-122.431297],[6.0989]] +``` + +Both snapshot IDs are optional. Without `to_snapshot_id_inclusive`, the scan ends at the table's current snapshot. Without `from_snapshot_id_exclusive`, it starts at the beginning of the end snapshot's history: + +```python +# San Francisco, Drachten and Paris +tbl.incremental_append_scan(from_snapshot_id_exclusive=snapshot_ids[0]).to_arrow() + +# Amsterdam and San Francisco +tbl.incremental_append_scan(to_snapshot_id_inclusive=snapshot_ids[1]).to_arrow() +``` + +The start snapshot must be an ancestor of the end snapshot: an earlier snapshot in its history, not the end snapshot itself. Otherwise, the scan raises a `ValueError`. + +To include the start snapshot in the range, call `from_snapshot_id_inclusive()` on the scan instead. This returns the same rows as the first example: + +```python +tbl.incremental_append_scan( + to_snapshot_id_inclusive=snapshot_ids[2], +).from_snapshot_id_inclusive(snapshot_ids[1]).to_arrow() +``` + +### Reading new rows since the last run + +To process new data as it arrives, save the ID of the snapshot that each run read up to, and start the next run after it: + +```python +# None on the first run, so that every appended row is read +last_snapshot_id = None + +tbl = catalog.load_table("default.cities") +snapshot = tbl.current_snapshot() + +# Skip the scan when nothing was committed since the last run +if snapshot is not None and snapshot.snapshot_id != last_snapshot_id: + new_rows = tbl.incremental_append_scan( + from_snapshot_id_exclusive=last_snapshot_id, + to_snapshot_id_inclusive=snapshot.snapshot_id, + ).to_arrow() + + # Process new_rows, then save the snapshot ID for the next run + last_snapshot_id = snapshot.snapshot_id +``` + +Without the check, a run with no new commits would raise a `ValueError`, because the start snapshot can't be the end snapshot. + +### Filtering and output + +`incremental_append_scan()` accepts the same `row_filter`, `selected_fields`, `case_sensitive` and `limit` arguments as `scan()`, and has the same output methods, such as `to_arrow()`, `to_arrow_batch_reader()`, `to_pandas()` and `to_polars()`. See [Query the data](#query-the-data). + +```python +tbl.incremental_append_scan( + from_snapshot_id_exclusive=snapshot_ids[0], + row_filter="lat > 40.0", + selected_fields=("city", "lat"), +).to_pandas() +``` + +This returns the cities appended after the first snapshot with a latitude above 40: + +```python + city lat +0 Drachten 53.112540 +1 Paris 48.864716 +``` + + + +!!! note "Current schema" + Rows are read with the table's current schema, even if they were appended before a schema change. A column that was added since reads as `null`. + +!!! note "Scan planning" + PyIceberg always plans an incremental append scan itself by reading the table's manifests, even when a REST catalog uses server-side scan planning for `scan()`. + + + +### Only appends are read + +An incremental append scan only reads snapshots whose operation is `append`. Snapshots with any other operation (`overwrite`, `delete` or `replace`) are skipped, including any rows they add. This means it isn't a changelog: deletes and updates aren't reported, and rows that were deleted after they were appended are still returned. For example, Paris is still returned after it's deleted: + +```python +tbl.delete(delete_filter="city == 'Paris'") + +tbl.incremental_append_scan(from_snapshot_id_exclusive=snapshot_ids[2]).to_arrow() +``` + +```python +pyarrow.Table +city: string +lat: double +long: double +---- +city: [["Paris"]] +lat: [[48.864716]] +long: [[2.349014]] +``` + +In PyIceberg, `append()` and `add_files()` commit `append` snapshots, and `delete()` commits `delete` or `overwrite` snapshots. `overwrite()`, `dynamic_partition_overwrite()` and `upsert()` remove the rows they replace in a `delete` or `overwrite` snapshot and write the new rows in an `append` snapshot. The scan returns the new rows but doesn't reflect the removal, so after an `upsert()` it can return both the old and the new version of an updated row. + ## Type mapping ### PyArrow diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index b66c99c74d..c70f5131be 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -1555,6 +1555,7 @@ def incremental_append_scan( The incremental append scan returns the rows added by append snapshots in a snapshot range that match the provided row_filter, projected onto the table's current schema. + Non-append snapshots in the range are skipped, so this is not a changelog scan. Args: from_snapshot_id_exclusive: @@ -2700,6 +2701,8 @@ def count(self) -> int: class IncrementalAppendScan(BaseScan): """An incremental scan of a table's data that accumulates appended data between two snapshots. + Non-append snapshots in the range are skipped, so this is not a changelog scan. + Args: from_snapshot_id: ID of the snapshot to start the incremental scan from. If None, the scan starts from @@ -2823,7 +2826,7 @@ def plan_files(self) -> Iterable[FileScanTask]: ) def to_arrow(self) -> pa.Table: - """Read an Arrow table eagerly from this IncrementalAppendScan. + """Read the rows added by append snapshots in this scan's range into an Arrow table. All rows will be loaded into memory at once. @@ -2833,7 +2836,7 @@ def to_arrow(self) -> pa.Table: return _to_arrow_via_file_scan_tasks(self, self.projection(), self.plan_files()) def to_arrow_batch_reader(self) -> pa.RecordBatchReader: - """Return an Arrow RecordBatchReader from this IncrementalAppendScan. + """Return an Arrow RecordBatchReader over the rows added by append snapshots in this scan's range. For large results, using a RecordBatchReader requires less memory than loading an Arrow Table for the same IncrementalAppendScan, because a @@ -2848,7 +2851,8 @@ def to_arrow_batch_reader(self) -> pa.RecordBatchReader: def _validate_and_resolve_snapshots(self) -> tuple[int | None, int]: """Resolve the configured range to ``(from_snapshot_id_exclusive, to_snapshot_id_inclusive)``. - A ``None`` "from" means the scan starts from the oldest ancestor of the end snapshot. + A ``None`` "from" means the scan starts from the oldest ancestor of the end snapshot. An inclusive + "from" is resolved to its parent, which becomes the exclusive boundary. """ # Resolve the inclusive end snapshot, defaulting to the table's current snapshot. if self.to_snapshot_id is not None: diff --git a/pyiceberg/table/snapshots.py b/pyiceberg/table/snapshots.py index 624ce486b0..b59dc0671b 100644 --- a/pyiceberg/table/snapshots.py +++ b/pyiceberg/table/snapshots.py @@ -468,7 +468,8 @@ def ancestors_between_ids( ) -> Iterable[Snapshot]: """Get the ancestors of and including the given "to" snapshot, up to but not including the "from" snapshot. - If ``from_snapshot_id_exclusive`` is None, all ancestors of the "to" snapshot are returned. + If ``from_snapshot_id_exclusive`` is None, or is neither the "to" snapshot nor one of its ancestors, all ancestors + of the "to" snapshot are returned, so callers should validate it first (see ``is_parent_ancestor_of``). Raises: ValueError: if ``to_snapshot_id_inclusive`` is not present in the table metadata. From d73c7a2705c4b5e3a21a85d1ed56e19b5557ffba Mon Sep 17 00:00:00 2001 From: Sreesh Maheshwar Date: Sun, 29 Nov 2026 23:46:13 +0000 Subject: [PATCH 2/2] Docs: Shorten the incremental append scan section Cut the section down to the snapshot range and the append-only caveat, and drop the docstring rewording that isn't needed for the docs. Co-Authored-By: Claude Opus 5.5 --- mkdocs/docs/api.md | 134 ++--------------------------------- pyiceberg/table/__init__.py | 9 +-- pyiceberg/table/snapshots.py | 3 +- 3 files changed, 11 insertions(+), 135 deletions(-) diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md index 40640b8bfe..093623eac9 100644 --- a/mkdocs/docs/api.md +++ b/mkdocs/docs/api.md @@ -2343,146 +2343,26 @@ DataFrame() ## Incremental append scan -`incremental_append_scan()` reads only the rows that were appended to a table between two snapshots, so new data can be processed without reading the whole table again. It doesn't report deletes or updates; see [Only appends are read](#only-appends-are-read). - -For example, consider a table with four appends, each of which commits a new snapshot: - -```python -import pyarrow as pa -from pyiceberg.catalog import load_catalog - -catalog = load_catalog("default") - -df = pa.Table.from_pylist([{"city": "Amsterdam", "lat": 52.371807, "long": 4.896029}]) -tbl = catalog.create_table("default.cities", schema=df.schema) -tbl.append(df) - -tbl.append(pa.Table.from_pylist([{"city": "San Francisco", "lat": 37.773972, "long": -122.431297}])) -tbl.append(pa.Table.from_pylist([{"city": "Drachten", "lat": 53.11254, "long": 6.0989}])) -tbl.append(pa.Table.from_pylist([{"city": "Paris", "lat": 48.864716, "long": 2.349014}])) - -snapshot_ids = [snapshot.snapshot_id for snapshot in tbl.snapshots()] -``` - -Read the rows appended after the first snapshot, up to and including the third: +`incremental_append_scan()` reads only the rows appended to a table between two snapshots: ```python tbl.incremental_append_scan( - from_snapshot_id_exclusive=snapshot_ids[0], - to_snapshot_id_inclusive=snapshot_ids[2], + from_snapshot_id_exclusive=from_snapshot_id, + to_snapshot_id_inclusive=to_snapshot_id, ).to_arrow() ``` -This returns the rows of the second and third appends: +Both bounds are optional. Without a start, the scan reads from the beginning of the table's history, and without an end, up to the current snapshot. The start must be an earlier snapshot in the end snapshot's history, or a `ValueError` is raised. -```python -pyarrow.Table -city: string -lat: double -long: double ----- -city: [["San Francisco"],["Drachten"]] -lat: [[37.773972],[53.11254]] -long: [[-122.431297],[6.0989]] -``` - -Both snapshot IDs are optional. Without `to_snapshot_id_inclusive`, the scan ends at the table's current snapshot. Without `from_snapshot_id_exclusive`, it starts at the beginning of the end snapshot's history: - -```python -# San Francisco, Drachten and Paris -tbl.incremental_append_scan(from_snapshot_id_exclusive=snapshot_ids[0]).to_arrow() - -# Amsterdam and San Francisco -tbl.incremental_append_scan(to_snapshot_id_inclusive=snapshot_ids[1]).to_arrow() -``` - -The start snapshot must be an ancestor of the end snapshot: an earlier snapshot in its history, not the end snapshot itself. Otherwise, the scan raises a `ValueError`. - -To include the start snapshot in the range, call `from_snapshot_id_inclusive()` on the scan instead. This returns the same rows as the first example: - -```python -tbl.incremental_append_scan( - to_snapshot_id_inclusive=snapshot_ids[2], -).from_snapshot_id_inclusive(snapshot_ids[1]).to_arrow() -``` - -### Reading new rows since the last run - -To process new data as it arrives, save the ID of the snapshot that each run read up to, and start the next run after it: - -```python -# None on the first run, so that every appended row is read -last_snapshot_id = None - -tbl = catalog.load_table("default.cities") -snapshot = tbl.current_snapshot() - -# Skip the scan when nothing was committed since the last run -if snapshot is not None and snapshot.snapshot_id != last_snapshot_id: - new_rows = tbl.incremental_append_scan( - from_snapshot_id_exclusive=last_snapshot_id, - to_snapshot_id_inclusive=snapshot.snapshot_id, - ).to_arrow() - - # Process new_rows, then save the snapshot ID for the next run - last_snapshot_id = snapshot.snapshot_id -``` - -Without the check, a run with no new commits would raise a `ValueError`, because the start snapshot can't be the end snapshot. - -### Filtering and output - -`incremental_append_scan()` accepts the same `row_filter`, `selected_fields`, `case_sensitive` and `limit` arguments as `scan()`, and has the same output methods, such as `to_arrow()`, `to_arrow_batch_reader()`, `to_pandas()` and `to_polars()`. See [Query the data](#query-the-data). - -```python -tbl.incremental_append_scan( - from_snapshot_id_exclusive=snapshot_ids[0], - row_filter="lat > 40.0", - selected_fields=("city", "lat"), -).to_pandas() -``` - -This returns the cities appended after the first snapshot with a latitude above 40: - -```python - city lat -0 Drachten 53.112540 -1 Paris 48.864716 -``` +It takes the same filtering and projection arguments, and has the same output methods, as [`scan()`](#query-the-data). -!!! note "Current schema" - Rows are read with the table's current schema, even if they were appended before a schema change. A column that was added since reads as `null`. - -!!! note "Scan planning" - PyIceberg always plans an incremental append scan itself by reading the table's manifests, even when a REST catalog uses server-side scan planning for `scan()`. +!!! warning "Only appends are read" + Only `append` snapshots, such as those committed by `append()` and `add_files()`, are read. Other snapshots are skipped, including any rows they add. Deletes aren't reported, so a row that was deleted after it was appended is still returned, and after an `upsert()` an updated row can be returned with both its old and its new values. -### Only appends are read - -An incremental append scan only reads snapshots whose operation is `append`. Snapshots with any other operation (`overwrite`, `delete` or `replace`) are skipped, including any rows they add. This means it isn't a changelog: deletes and updates aren't reported, and rows that were deleted after they were appended are still returned. For example, Paris is still returned after it's deleted: - -```python -tbl.delete(delete_filter="city == 'Paris'") - -tbl.incremental_append_scan(from_snapshot_id_exclusive=snapshot_ids[2]).to_arrow() -``` - -```python -pyarrow.Table -city: string -lat: double -long: double ----- -city: [["Paris"]] -lat: [[48.864716]] -long: [[2.349014]] -``` - -In PyIceberg, `append()` and `add_files()` commit `append` snapshots, and `delete()` commits `delete` or `overwrite` snapshots. `overwrite()`, `dynamic_partition_overwrite()` and `upsert()` remove the rows they replace in a `delete` or `overwrite` snapshot and write the new rows in an `append` snapshot. The scan returns the new rows but doesn't reflect the removal, so after an `upsert()` it can return both the old and the new version of an updated row. - ## Type mapping ### PyArrow diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index 340ebbf420..d61eb9c8d9 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -2759,8 +2759,6 @@ def count(self) -> int: class IncrementalAppendScan(BaseScan): """An incremental scan of a table's data that accumulates appended data between two snapshots. - Non-append snapshots in the range are skipped, so this is not a changelog scan. - Args: from_snapshot_id: ID of the snapshot to start the incremental scan from. If None, the scan starts from @@ -2884,7 +2882,7 @@ def plan_files(self) -> Iterable[FileScanTask]: ) def to_arrow(self) -> pa.Table: - """Read the rows added by append snapshots in this scan's range into an Arrow table. + """Read an Arrow table eagerly from this IncrementalAppendScan. All rows will be loaded into memory at once. @@ -2894,7 +2892,7 @@ def to_arrow(self) -> pa.Table: return _to_arrow_via_file_scan_tasks(self, self.projection(), self.plan_files()) def to_arrow_batch_reader(self) -> pa.RecordBatchReader: - """Return an Arrow RecordBatchReader over the rows added by append snapshots in this scan's range. + """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 @@ -2919,8 +2917,7 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: def _validate_and_resolve_snapshots(self) -> tuple[int | None, int]: """Resolve the configured range to ``(from_snapshot_id_exclusive, to_snapshot_id_inclusive)``. - A ``None`` "from" means the scan starts from the oldest ancestor of the end snapshot. An inclusive - "from" is resolved to its parent, which becomes the exclusive boundary. + A ``None`` "from" means the scan starts from the oldest ancestor of the end snapshot. """ # Resolve the inclusive end snapshot, defaulting to the table's current snapshot. if self.to_snapshot_id is not None: diff --git a/pyiceberg/table/snapshots.py b/pyiceberg/table/snapshots.py index b59dc0671b..624ce486b0 100644 --- a/pyiceberg/table/snapshots.py +++ b/pyiceberg/table/snapshots.py @@ -468,8 +468,7 @@ def ancestors_between_ids( ) -> Iterable[Snapshot]: """Get the ancestors of and including the given "to" snapshot, up to but not including the "from" snapshot. - If ``from_snapshot_id_exclusive`` is None, or is neither the "to" snapshot nor one of its ancestors, all ancestors - of the "to" snapshot are returned, so callers should validate it first (see ``is_parent_ancestor_of``). + If ``from_snapshot_id_exclusive`` is None, all ancestors of the "to" snapshot are returned. Raises: ValueError: if ``to_snapshot_id_inclusive`` is not present in the table metadata.