Skip to content

Commit 74d5569

Browse files
committed
fix to/from logic and allow optional to_snapshot arg in validation_history
1 parent f2f3a88 commit 74d5569

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

pyiceberg/table/snapshots.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,13 @@ def ancestors_of(current_snapshot: Optional[Snapshot], table_metadata: TableMeta
432432

433433

434434
def ancestors_between(
435-
current_snapshot: Snapshot, oldest_snapshot: Optional[Snapshot], table_metadata: TableMetadata
435+
to_snapshot: Snapshot, from_snapshot: Optional[Snapshot], table_metadata: TableMetadata
436436
) -> Iterable[Snapshot]:
437437
"""Get the ancestors of and including the given snapshot between the latest and oldest snapshot."""
438-
if oldest_snapshot is not None:
439-
for snapshot in ancestors_of(current_snapshot, table_metadata):
440-
if snapshot.snapshot_id == oldest_snapshot.snapshot_id:
438+
if from_snapshot is not None:
439+
for snapshot in ancestors_of(to_snapshot, table_metadata):
440+
if snapshot == from_snapshot:
441441
break
442442
yield snapshot
443443
else:
444-
for snapshot in ancestors_of(current_snapshot, table_metadata):
445-
yield snapshot
444+
yield from ancestors_of(to_snapshot, table_metadata)

pyiceberg/table/update/validate.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
from typing import Optional
18+
1719
from pyiceberg.manifest import ManifestContent, ManifestFile
1820
from pyiceberg.table import Table
1921
from pyiceberg.table.snapshots import Operation, Snapshot, ancestors_between
@@ -25,17 +27,17 @@ class ValidationException(Exception):
2527

2628
def validation_history(
2729
table: Table,
28-
starting_snapshot: Snapshot,
29-
parent_snapshot: Snapshot,
30+
from_snapshot: Snapshot,
31+
to_snapshot: Optional[Snapshot],
3032
matching_operations: set[Operation],
3133
manifest_content_filter: ManifestContent,
3234
) -> tuple[list[ManifestFile], set[Snapshot]]:
3335
"""Return newly added manifests and snapshot IDs between the starting snapshot and parent snapshot.
3436
3537
Args:
3638
table: Table to get the history from
37-
starting_snapshot: Starting snapshot
38-
parent_snapshot: Parent snapshot to get the history from
39+
from_snapshot: Parent snapshot to get the history from
40+
to_snapshot: Starting snapshot
3941
matching_operations: Operations to match on
4042
manifest_content_filter: Manifest content type to filter
4143
@@ -49,7 +51,7 @@ def validation_history(
4951
snapshots: set[Snapshot] = set()
5052

5153
last_snapshot = None
52-
for snapshot in ancestors_between(starting_snapshot, parent_snapshot, table.metadata):
54+
for snapshot in ancestors_between(from_snapshot, to_snapshot, table.metadata):
5355
last_snapshot = snapshot
5456
summary = snapshot.summary
5557
if summary is None:
@@ -64,7 +66,7 @@ def validation_history(
6466
]
6567
)
6668

67-
if last_snapshot is None or last_snapshot.snapshot_id == starting_snapshot.snapshot_id:
69+
if last_snapshot is None or last_snapshot.snapshot_id == from_snapshot.snapshot_id:
6870
raise ValidationException("No matching snapshot found.")
6971

7072
return manifests_files, snapshots

tests/table/test_snapshots.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
# pylint:disable=redefined-outer-name,eval-used
18+
from typing import cast
19+
1820
import pytest
1921

2022
from pyiceberg.manifest import DataFile, DataFileContent, ManifestContent, ManifestFile
@@ -392,11 +394,12 @@ def test_ancestors_of_recursive_error(table_v2_with_extensive_snapshots: Table)
392394

393395
def test_ancestors_between(table_v2_with_extensive_snapshots: Table) -> None:
394396
oldest_snapshot = table_v2_with_extensive_snapshots.snapshots()[0]
397+
current_snapshot = cast(Snapshot, table_v2_with_extensive_snapshots.current_snapshot())
395398
assert (
396399
len(
397400
list(
398401
ancestors_between(
399-
table_v2_with_extensive_snapshots.current_snapshot(),
402+
current_snapshot,
400403
oldest_snapshot,
401404
table_v2_with_extensive_snapshots.metadata,
402405
)

0 commit comments

Comments
 (0)