-
Notifications
You must be signed in to change notification settings - Fork 414
feat: Add Set Current Snapshot to ManageSnapshots API #2871
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
geruh
wants to merge
1
commit into
apache:main
Choose a base branch
from
geruh:set-snapshot
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.
+319
−2
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
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,179 @@ | ||
| # 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 | ||
| from uuid import uuid4 | ||
|
|
||
| import pytest | ||
|
|
||
| from pyiceberg.table import CommitTableResponse, Table | ||
| from pyiceberg.table.update import SetSnapshotRefUpdate, TableUpdate | ||
|
|
||
|
|
||
| def _mock_commit_response(table: Table) -> CommitTableResponse: | ||
| return CommitTableResponse( | ||
| metadata=table.metadata, | ||
| metadata_location="s3://bucket/tbl", | ||
| uuid=uuid4(), | ||
| ) | ||
|
|
||
|
|
||
| def _get_updates(mock_catalog: MagicMock) -> tuple[TableUpdate, ...]: | ||
| args, _ = mock_catalog.commit_table.call_args | ||
| return args[2] | ||
|
|
||
|
|
||
| def test_set_current_snapshot_basic(table_v2: Table) -> None: | ||
| snapshot_one = 3051729675574597004 | ||
|
|
||
| table_v2.catalog = MagicMock() | ||
| table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) | ||
|
|
||
| table_v2.manage_snapshots().set_current_snapshot(snapshot_id=snapshot_one).commit() | ||
|
|
||
| table_v2.catalog.commit_table.assert_called_once() | ||
|
|
||
| updates = _get_updates(table_v2.catalog) | ||
| set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] | ||
|
|
||
| assert len(set_ref_updates) == 1 | ||
| update = set_ref_updates[0] | ||
| assert update.snapshot_id == snapshot_one | ||
| assert update.ref_name == "main" | ||
| assert update.type == "branch" | ||
|
|
||
|
|
||
| def test_set_current_snapshot_unknown_id(table_v2: Table) -> None: | ||
| invalid_snapshot_id = 1234567890000 | ||
| table_v2.catalog = MagicMock() | ||
|
|
||
| with pytest.raises(ValueError, match="Cannot set current snapshot to unknown snapshot id"): | ||
| table_v2.manage_snapshots().set_current_snapshot(snapshot_id=invalid_snapshot_id).commit() | ||
|
|
||
| table_v2.catalog.commit_table.assert_not_called() | ||
|
|
||
|
|
||
| def test_set_current_snapshot_to_current(table_v2: Table) -> None: | ||
| current_snapshot = table_v2.current_snapshot() | ||
| assert current_snapshot is not None | ||
|
|
||
| table_v2.catalog = MagicMock() | ||
| table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) | ||
|
|
||
| table_v2.manage_snapshots().set_current_snapshot(snapshot_id=current_snapshot.snapshot_id).commit() | ||
|
|
||
| table_v2.catalog.commit_table.assert_called_once() | ||
|
|
||
|
|
||
| def test_set_current_snapshot_chained_with_tag(table_v2: Table) -> None: | ||
| snapshot_one = 3051729675574597004 | ||
| table_v2.catalog = MagicMock() | ||
| table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) | ||
|
|
||
| (table_v2.manage_snapshots().set_current_snapshot(snapshot_id=snapshot_one).create_tag(snapshot_one, "my-tag").commit()) | ||
|
|
||
| table_v2.catalog.commit_table.assert_called_once() | ||
|
|
||
| updates = _get_updates(table_v2.catalog) | ||
| set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] | ||
|
|
||
| assert len(set_ref_updates) == 2 | ||
| assert {u.ref_name for u in set_ref_updates} == {"main", "my-tag"} | ||
|
|
||
|
|
||
| def test_set_current_snapshot_with_extensive_snapshots(table_v2_with_extensive_snapshots: Table) -> None: | ||
| snapshots = table_v2_with_extensive_snapshots.metadata.snapshots | ||
| assert len(snapshots) > 100 | ||
|
|
||
| target_snapshot = snapshots[50].snapshot_id | ||
|
|
||
| table_v2_with_extensive_snapshots.catalog = MagicMock() | ||
| table_v2_with_extensive_snapshots.catalog.commit_table.return_value = _mock_commit_response(table_v2_with_extensive_snapshots) | ||
|
|
||
| table_v2_with_extensive_snapshots.manage_snapshots().set_current_snapshot(snapshot_id=target_snapshot).commit() | ||
|
|
||
| table_v2_with_extensive_snapshots.catalog.commit_table.assert_called_once() | ||
|
|
||
| updates = _get_updates(table_v2_with_extensive_snapshots.catalog) | ||
| set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] | ||
|
|
||
| assert len(set_ref_updates) == 1 | ||
| assert set_ref_updates[0].snapshot_id == target_snapshot | ||
|
|
||
|
|
||
| def test_set_current_snapshot_by_ref_name(table_v2: Table) -> None: | ||
| current_snapshot = table_v2.current_snapshot() | ||
| assert current_snapshot is not None | ||
|
|
||
| table_v2.catalog = MagicMock() | ||
| table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) | ||
|
|
||
| table_v2.manage_snapshots().set_current_snapshot(ref_name="main").commit() | ||
|
|
||
| updates = _get_updates(table_v2.catalog) | ||
| set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] | ||
|
|
||
| assert len(set_ref_updates) == 1 | ||
| assert set_ref_updates[0].snapshot_id == current_snapshot.snapshot_id | ||
| assert set_ref_updates[0].ref_name == "main" | ||
|
|
||
|
|
||
| def test_set_current_snapshot_unknown_ref(table_v2: Table) -> None: | ||
| table_v2.catalog = MagicMock() | ||
|
|
||
| with pytest.raises(ValueError, match="Cannot find matching snapshot ID for ref: nonexistent"): | ||
| table_v2.manage_snapshots().set_current_snapshot(ref_name="nonexistent").commit() | ||
|
|
||
| table_v2.catalog.commit_table.assert_not_called() | ||
|
|
||
|
|
||
| def test_set_current_snapshot_requires_one_argument(table_v2: Table) -> None: | ||
| table_v2.catalog = MagicMock() | ||
|
|
||
| with pytest.raises(ValueError, match="Either snapshot_id or ref_name must be provided, not both"): | ||
| table_v2.manage_snapshots().set_current_snapshot().commit() | ||
|
|
||
| with pytest.raises(ValueError, match="Either snapshot_id or ref_name must be provided, not both"): | ||
| table_v2.manage_snapshots().set_current_snapshot(snapshot_id=123, ref_name="main").commit() | ||
|
|
||
| table_v2.catalog.commit_table.assert_not_called() | ||
|
|
||
|
|
||
| def test_set_current_snapshot_chained_with_create_tag(table_v2: Table) -> None: | ||
| snapshot_one = 3051729675574597004 | ||
| table_v2.catalog = MagicMock() | ||
| table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) | ||
|
|
||
| # create a tag and immediately use it to set current snapshot | ||
| ( | ||
| table_v2.manage_snapshots() | ||
| .create_tag(snapshot_id=snapshot_one, tag_name="new-tag") | ||
| .set_current_snapshot(ref_name="new-tag") | ||
| .commit() | ||
| ) | ||
|
|
||
| table_v2.catalog.commit_table.assert_called_once() | ||
|
|
||
| updates = _get_updates(table_v2.catalog) | ||
| set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] | ||
|
|
||
| # should have the tag and the main branch update | ||
| assert len(set_ref_updates) == 2 | ||
| assert {u.ref_name for u in set_ref_updates} == {"new-tag", "main"} | ||
|
|
||
| # The main branch should point to the same snapshot as the tag | ||
| main_update = next(u for u in set_ref_updates if u.ref_name == "main") | ||
| assert main_update.snapshot_id == snapshot_one |
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.
We can rip this helper out of transaction and leave in managesnapshots api.