Skip to content

Add ExpireSnapshots.retain_last - #4437

Open
rambleraptor wants to merge 2 commits into
apache:mainfrom
rambleraptor:expire-snapshots-retain-last
Open

rambleraptor wants to merge 2 commits into
apache:mainfrom
rambleraptor:expire-snapshots-retain-last

Conversation

@rambleraptor

@rambleraptor rambleraptor commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

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?

  • Added retain_last method to ExpireSnapshots.

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
rambleraptor requested a review from Fokko November 19, 2026 23:25
@rambleraptor
rambleraptor force-pushed the expire-snapshots-retain-last branch from 142a52d to 0b81a7f Compare November 19, 2026 23:25
Comment thread pyiceberg/table/update/snapshot.py Resolved
Comment thread tests/table/test_expire_snapshots.py Resolved

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @rambleraptor! Nice addition, the chaining example in the docs reads well.

Verified locally: all 17 tests in tests/table/test_expire_snapshots.py pass, CI is green.

One small thing I checked and is fine: by_id snapshots still expire even with retain_last, since they are added directly to _snapshot_ids_to_expire. Also like that the ordering of older_than/retain_last does not matter since the union happens in _commit.

LGTM!

Unlike Java, retain_last alone does not apply history.expire.max-snapshot-age-ms,
so it expires nothing. Note this and pin the behavior with a test.
@rambleraptor

Copy link
Copy Markdown
Collaborator Author

Thanks for the review @Fokko and @ebyhr! I've made the changes that @ebyhr suggested, so this should be good to merge!

@rambleraptor
rambleraptor requested a review from Fokko November 23, 2026 20:36

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants