-
Notifications
You must be signed in to change notification settings - Fork 302
Added ExpireSnapshots Feature #1880
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
ForeverAngry
wants to merge
33
commits into
apache:main
Choose a base branch
from
ForeverAngry:1880-add-expire-snapshots
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.
+198
−5
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
0a94d96
Added initial units tests and Class for Removing a Snapshot
ForeverAngry 5f0b62b
Added methods needed to expire snapshots by id, and optionally cleanu…
ForeverAngry f995daa
Update test_expire_snapshots.py
ForeverAngry 65365e1
Added the builder method to __init__.py, updated the snapshot api wit…
ForeverAngry e28815f
Snapshots are not being transacted on, but need to re-assign refs
ForeverAngry 4628ede
Fixed the test case.
ForeverAngry e80c41c
adding print statements to help with debugging
ForeverAngry cb9f0c9
Draft ready
ForeverAngry ebcff2b
Applied suggestions to Fix CICD
ForeverAngry 97399bf
Merge branch 'main' into main
ForeverAngry 95e5af2
Rebuild the poetry lock file.
ForeverAngry 5ab5890
Merge branch 'main' into main
ForeverAngry 5acd690
Refactor implementation of `ExpireSnapshots`
ForeverAngry d30a08c
Fixed format and linting issues
ForeverAngry e62ab58
Merge branch 'main' into main
ForeverAngry 1af3258
Fixed format and linting issues
ForeverAngry 352b48f
Merge branch 'main' of https://github.com/ForeverAngry/iceberg-python
ForeverAngry 382e0ea
Merge branch 'main' into main
ForeverAngry 549c183
rebased: from main
ForeverAngry 386cb15
fixed: typo
ForeverAngry 12729fa
removed errant files
ForeverAngry ce3515c
Added: public method signature to the init table file.
ForeverAngry 28fce4b
Removed: `expire_snapshots_older_than` method, in favor of implementi…
ForeverAngry 2c3153e
Update tests/table/test_expire_snapshots.py
ForeverAngry 27c3ece
Removed: unrelated changes, Added: logic to expire snapshot method.
ForeverAngry 05793c0
Merge branch 'main' into 1880-add-expire-snapshots
ForeverAngry 8ec1889
Update test_partition_evolution.py
ForeverAngry b23ac6a
Update test_literals.py
ForeverAngry 5c458f2
Update snapshot.py
ForeverAngry a08eb6b
Update snapshot.py
ForeverAngry 689310d
Fixed: Linting
ForeverAngry 9031f06
Update test_expire_snapshots.py
ForeverAngry 3488314
Update test_expire_snapshots.py
ForeverAngry 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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from unittest.mock import MagicMock | ||
ForeverAngry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from uuid import uuid4 | ||
|
||
import pytest | ||
|
||
from pyiceberg.table import CommitTableResponse, Table | ||
|
||
|
||
def test_cannot_expire_protected_head_snapshot(table_v2: Table) -> None: | ||
"""Test that a HEAD (branch) snapshot cannot be expired.""" | ||
HEAD_SNAPSHOT = 3051729675574597004 | ||
KEEP_SNAPSHOT = 3055729675574597004 | ||
|
||
# Mock the catalog's commit_table method | ||
table_v2.catalog = MagicMock() | ||
# Simulate refs protecting HEAD_SNAPSHOT as a branch | ||
table_v2.metadata = table_v2.metadata.model_copy( | ||
update={ | ||
"refs": { | ||
"main": MagicMock(snapshot_id=HEAD_SNAPSHOT, snapshot_ref_type="branch"), | ||
"tag1": MagicMock(snapshot_id=KEEP_SNAPSHOT, snapshot_ref_type="tag"), | ||
} | ||
} | ||
) | ||
# Assert fixture data | ||
assert any(ref.snapshot_id == HEAD_SNAPSHOT for ref in table_v2.metadata.refs.values()) | ||
|
||
# Attempt to expire the HEAD snapshot and expect a ValueError | ||
with pytest.raises(ValueError, match=f"Snapshot with ID {HEAD_SNAPSHOT} is protected and cannot be expired."): | ||
table_v2.expire_snapshots().expire_snapshot_by_id(HEAD_SNAPSHOT).commit() | ||
|
||
table_v2.catalog.commit_table.assert_not_called() | ||
|
||
|
||
def test_cannot_expire_tagged_snapshot(table_v2: Table) -> None: | ||
"""Test that a tagged snapshot cannot be expired.""" | ||
TAGGED_SNAPSHOT = 3051729675574597004 | ||
KEEP_SNAPSHOT = 3055729675574597004 | ||
|
||
table_v2.catalog = MagicMock() | ||
# Simulate refs protecting TAGGED_SNAPSHOT as a tag | ||
table_v2.metadata = table_v2.metadata.model_copy( | ||
update={ | ||
"refs": { | ||
"tag1": MagicMock(snapshot_id=TAGGED_SNAPSHOT, snapshot_ref_type="tag"), | ||
"main": MagicMock(snapshot_id=KEEP_SNAPSHOT, snapshot_ref_type="branch"), | ||
} | ||
} | ||
) | ||
assert any(ref.snapshot_id == TAGGED_SNAPSHOT for ref in table_v2.metadata.refs.values()) | ||
|
||
with pytest.raises(ValueError, match=f"Snapshot with ID {TAGGED_SNAPSHOT} is protected and cannot be expired."): | ||
table_v2.expire_snapshots().expire_snapshot_by_id(TAGGED_SNAPSHOT).commit() | ||
|
||
table_v2.catalog.commit_table.assert_not_called() | ||
|
||
|
||
def test_expire_unprotected_snapshot(table_v2: Table) -> None: | ||
"""Test that an unprotected snapshot can be expired.""" | ||
EXPIRE_SNAPSHOT = 3051729675574597004 | ||
KEEP_SNAPSHOT = 3055729675574597004 | ||
|
||
mock_response = CommitTableResponse( | ||
metadata=table_v2.metadata.model_copy(update={"snapshots": [KEEP_SNAPSHOT]}), | ||
metadata_location="mock://metadata/location", | ||
uuid=uuid4(), | ||
) | ||
table_v2.catalog = MagicMock() | ||
table_v2.catalog.commit_table.return_value = mock_response | ||
|
||
# Remove any refs that protect the snapshot to be expired | ||
table_v2.metadata = table_v2.metadata.model_copy( | ||
update={ | ||
"refs": { | ||
"main": MagicMock(snapshot_id=KEEP_SNAPSHOT, snapshot_ref_type="branch"), | ||
"tag1": MagicMock(snapshot_id=KEEP_SNAPSHOT, snapshot_ref_type="tag"), | ||
} | ||
} | ||
) | ||
|
||
# Assert fixture data | ||
assert all(ref.snapshot_id != EXPIRE_SNAPSHOT for ref in table_v2.metadata.refs.values()) | ||
|
||
# Expire the snapshot | ||
table_v2.expire_snapshots().expire_snapshot_by_id(EXPIRE_SNAPSHOT).commit() | ||
|
||
table_v2.catalog.commit_table.assert_called_once() | ||
remaining_snapshots = table_v2.metadata.snapshots | ||
assert EXPIRE_SNAPSHOT not in remaining_snapshots | ||
assert len(table_v2.metadata.snapshots) == 1 | ||
|
||
|
||
def test_expire_nonexistent_snapshot_raises(table_v2: Table) -> None: | ||
"""Test that trying to expire a non-existent snapshot raises an error.""" | ||
NONEXISTENT_SNAPSHOT = 9999999999999999999 | ||
|
||
table_v2.catalog = MagicMock() | ||
table_v2.metadata = table_v2.metadata.model_copy(update={"refs": {}}) | ||
|
||
with pytest.raises(ValueError, match=f"Snapshot with ID {NONEXISTENT_SNAPSHOT} does not exist."): | ||
table_v2.expire_snapshots().expire_snapshot_by_id(NONEXISTENT_SNAPSHOT).commit() | ||
|
||
table_v2.catalog.commit_table.assert_not_called() |
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.
Uh oh!
There was an error while loading. Please reload this page.