Repository navigation
Add ExpireSnapshots.retain_last #4437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rambleraptor
wants to merge
1
commit into
apache:main
Choose a base branch
from
rambleraptor:expire-snapshots-retain-last
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,16 @@ | |
| # under the License. | ||
| import threading | ||
| from datetime import datetime, timedelta | ||
| from typing import Any | ||
| from unittest.mock import MagicMock, Mock | ||
| from uuid import uuid4 | ||
|
|
||
| import pytest | ||
|
|
||
| from pyiceberg.catalog.noop import NoopCatalog | ||
| from pyiceberg.io import load_file_io | ||
| from pyiceberg.table import CommitTableResponse, Table | ||
| from pyiceberg.table.metadata import TableMetadataV2 | ||
| from pyiceberg.table.statistics import PartitionStatisticsFile | ||
| from pyiceberg.table.update import RemoveSnapshotsUpdate, SetPartitionStatisticsUpdate, update_table_metadata | ||
| from pyiceberg.table.update.snapshot import ExpireSnapshots | ||
|
|
@@ -343,3 +347,77 @@ def test_update_remove_snapshots_with_partition_statistics(table_v2: Table) -> N | |
|
|
||
| assert [snapshot.snapshot_id for snapshot in new_metadata.snapshots] == [KEEP_SNAPSHOT] | ||
| assert [stat.snapshot_id for stat in new_metadata.partition_statistics] == [KEEP_SNAPSHOT] | ||
|
|
||
|
|
||
| def _table_with_branches(example_table_metadata_v2: dict[str, Any]) -> Table: | ||
| """Create a table where main is 1 <- 2 <- 3 <- 4 and branch "b" is 1 <- 2 <- 5.""" | ||
| parents = {1: None, 2: 1, 3: 2, 4: 3, 5: 2} | ||
| snapshots = [ | ||
| { | ||
| "snapshot-id": snapshot_id, | ||
| **({"parent-snapshot-id": parent_id} if parent_id is not None else {}), | ||
| "timestamp-ms": snapshot_id * 1000, | ||
| "sequence-number": snapshot_id, | ||
| "summary": {"operation": "append"}, | ||
| "manifest-list": f"s3://a/b/{snapshot_id}.avro", | ||
| } | ||
| for snapshot_id, parent_id in parents.items() | ||
| ] | ||
| metadata = TableMetadataV2( | ||
| **{ | ||
| **example_table_metadata_v2, | ||
| "current-snapshot-id": 4, | ||
| "last-sequence-number": 5, | ||
| "snapshots": snapshots, | ||
| "snapshot-log": [], | ||
| "refs": {"main": {"snapshot-id": 4, "type": "branch"}, "b": {"snapshot-id": 5, "type": "branch"}}, | ||
| } | ||
| ) | ||
| return Table( | ||
| identifier=("database", "table"), | ||
| metadata=metadata, | ||
| metadata_location=f"{metadata.location}/uuid.metadata.json", | ||
| io=load_file_io(), | ||
| catalog=NoopCatalog("NoopCatalog"), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "num_snapshots, expected_remaining", | ||
| [(1, {4, 5}), (2, {2, 3, 4, 5}), (10, {1, 2, 3, 4, 5})], | ||
| ) | ||
| def test_expire_snapshots_retain_last( | ||
| example_table_metadata_v2: dict[str, Any], num_snapshots: int, expected_remaining: set[int] | ||
| ) -> None: | ||
| table = _table_with_branches(example_table_metadata_v2) | ||
| future_datetime = datetime.now() + timedelta(days=1) | ||
|
|
||
| txn = table.transaction() | ||
| ExpireSnapshots(txn).older_than(future_datetime).retain_last(num_snapshots).commit() | ||
|
|
||
| assert {snapshot.snapshot_id for snapshot in txn.table_metadata.snapshots} == expected_remaining | ||
|
|
||
|
|
||
| def test_expire_snapshots_retain_last_before_older_than(example_table_metadata_v2: dict[str, Any]) -> None: | ||
| table = _table_with_branches(example_table_metadata_v2) | ||
| future_datetime = datetime.now() + timedelta(days=1) | ||
|
|
||
| txn = table.transaction() | ||
| ExpireSnapshots(txn).retain_last(2).older_than(future_datetime).commit() | ||
|
|
||
| assert {snapshot.snapshot_id for snapshot in txn.table_metadata.snapshots} == {2, 3, 4, 5} | ||
|
|
||
|
|
||
| def test_expire_snapshots_retain_last_does_not_retain_by_id(example_table_metadata_v2: dict[str, Any]) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a test that def test_expire_snapshots_retain_last_without_older_than(example_table_metadata_v2: dict[str, Any]) -> None:
table = _table_with_branches(example_table_metadata_v2)
txn = table.transaction()
ExpireSnapshots(txn).retain_last(1).commit()
assert {snapshot.snapshot_id for snapshot in txn.table_metadata.snapshots} == {1, 2, 3, 4, 5} |
||
| table = _table_with_branches(example_table_metadata_v2) | ||
|
|
||
| txn = table.transaction() | ||
| ExpireSnapshots(txn).retain_last(10).by_id(3).commit() | ||
|
|
||
| assert {snapshot.snapshot_id for snapshot in txn.table_metadata.snapshots} == {1, 2, 4, 5} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("num_snapshots", [0, -1]) | ||
| def test_expire_snapshots_retain_last_invalid(table_v2: Table, num_snapshots: int) -> None: | ||
| with pytest.raises(ValueError, match=f"Number of snapshots to retain must be at least 1, cannot be: {num_snapshots}"): | ||
| table_v2.maintenance.expire_snapshots().retain_last(num_snapshots) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Java,
retainLast(n)alone still expires snapshots older thanhistory.expire.max-snapshot-age-ms, keepingnper branch (RemoveSnapshots, test: apache/iceberg#19049). Here it does nothing withoutolder_than.Could we note that in the docstring? Honoring
history.expire.max-snapshot-age-msandhistory.expire.min-snapshots-to-keep(and the ref-level overrides) looks like a separate follow-up.