Repository navigation
Add ExpireSnapshots.retain_last - #4437
Open
rambleraptor wants to merge 1 commit into
Open
rambleraptor wants to merge 1 commit into
rambleraptor wants to merge 1 commit into
Conversation
Port Java's ExpireSnapshots.retainLast: snapshots that older_than would expire are kept if they are among the N most recent ancestors of a branch. Snapshots expired by ID are unaffected.
rambleraptor
force-pushed
the
expire-snapshots-retain-last
branch
from
November 19, 2026 23:25
142a52d to
0b81a7f
Compare
ebyhr
reviewed
Nov 20, 2026
| self._snapshot_ids_older_than.add(snapshot.snapshot_id) | ||
| return self | ||
|
|
||
| def retain_last(self, num_snapshots: int) -> ExpireSnapshots: |
Member
There was a problem hiding this comment.
In Java, retainLast(n) alone still expires snapshots older than history.expire.max-snapshot-age-ms, keeping n per branch (RemoveSnapshots, test: apache/iceberg#19049). Here it does nothing without older_than.
Could we note that in the docstring? Honoring history.expire.max-snapshot-age-ms and history.expire.min-snapshots-to-keep (and the ref-level overrides) looks like a separate follow-up.
| 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.
Could we add a test that retain_last alone expires nothing? It pins the current behavior, so it's a visible change if the history.expire.* defaults get wired in later.
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}This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Rationale for this change
This helper method lets you retain the N most recent ancestors of a branch. I've included some doc examples of how you can use this for chaining multiple commands together.
Are these changes tested?
Unit tests included
Are there any user-facing changes?
retain_lastmethod to ExpireSnapshots.