Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 548
Implement update for remove-snapshots action#1561
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b6ba319
Implement update for `remove-snapshot` action
grihabor 88064c0
Add tests
grihabor 818f771
Make sure parent_snapshot_id is consistent
grihabor 9b31690
Set parent_snapshot_id to None
grihabor c5cbfe4
cherry-pick: Support Remove Branch or Tag APIs (#822)
grihabor 29feaf7
Make sure refs are removed when removing snapshots
grihabor 9e3f7c7
Fix ruff and pydocstyle
grihabor b9c912d
Remove statistics
grihabor d515e74
Fix name collision
grihabor c7481fe
Remove entries from snapshot log
grihabor da4db9f
Introduce constants for test readability
grihabor 19e17f0
Allow to remove main branch
grihabor 32e1e85
Revert changes (moved to #1598)
grihabor e68dbb9
Merge remote-tracking branch 'upstream/main' into remove-snapshots-up…
grihabor fb8f350
Use verbose variable names
grihabor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,6 +16,7 @@ | ||
| # under the License. | ||
| from __future__ import annotations | ||
| import itertools | ||
| import uuid | ||
| from abc import ABC, abstractmethod | ||
| from datetime import datetime | ||
| @@ -466,6 +467,46 @@ def _(update: SetSnapshotRefUpdate, base_metadata: TableMetadata, context: _Tabl | ||
| return base_metadata.model_copy(update=metadata_updates) | ||
| @_apply_table_update.register(RemoveSnapshotsUpdate) | ||
| def _(update: RemoveSnapshotsUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata: | ||
| for remove_snapshot_id in update.snapshot_ids: | ||
| if not any(snapshot.snapshot_id == remove_snapshot_id for snapshot in base_metadata.snapshots): | ||
| raise ValueError(f"Snapshot with snapshot id {remove_snapshot_id} does not exist: {base_metadata.snapshots}") | ||
| snapshots = [ | ||
| ( | ||
| snapshot.model_copy(update={"parent_snapshot_id": None}) | ||
| if snapshot.parent_snapshot_id in update.snapshot_ids | ||
| else snapshot | ||
| ) | ||
| for snapshot in base_metadata.snapshots | ||
| if snapshot.snapshot_id not in update.snapshot_ids | ||
| ] | ||
| snapshot_log = [ | ||
| snapshot_log_entry | ||
| for snapshot_log_entry in base_metadata.snapshot_log | ||
| if snapshot_log_entry.snapshot_id not in update.snapshot_ids | ||
| ] | ||
| remove_ref_updates = ( | ||
| RemoveSnapshotRefUpdate(ref_name=ref_name) | ||
| for ref_name, ref in base_metadata.refs.items() | ||
| if ref.snapshot_id in update.snapshot_ids | ||
| ) | ||
| remove_statistics_updates = ( | ||
| RemoveStatisticsUpdate(statistics_file.snapshot_id) | ||
| for statistics_file in base_metadata.statistics | ||
| if statistics_file.snapshot_id in update.snapshot_ids | ||
| ) | ||
| updates = itertools.chain(remove_ref_updates, remove_statistics_updates) | ||
| new_metadata = base_metadata | ||
| for upd in updates: | ||
| new_metadata = _apply_table_update(upd, new_metadata, context) | ||
kevinjqliu marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| context.add_update(update) | ||
| return new_metadata.model_copy(update={"snapshots": snapshots, "snapshot_log": snapshot_log}) | ||
| @_apply_table_update.register(RemoveSnapshotRefUpdate) | ||
| def _(update: RemoveSnapshotRefUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata: | ||
Fokko marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if update.ref_name not in base_metadata.refs: | ||
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 |
|---|---|---|
| @@ -79,6 +79,7 @@ | ||
| AssertTableUUID, | ||
| RemovePropertiesUpdate, | ||
| RemoveSnapshotRefUpdate, | ||
| RemoveSnapshotsUpdate, | ||
| RemoveStatisticsUpdate, | ||
| SetDefaultSortOrderUpdate, | ||
| SetPropertiesUpdate, | ||
| @@ -795,6 +796,42 @@ def test_update_metadata_set_snapshot_ref(table_v2: Table) -> None: | ||
| def test_update_remove_snapshots(table_v2: Table) -> None: | ||
| REMOVE_SNAPSHOT = 3051729675574597004 | ||
| KEEP_SNAPSHOT = 3055729675574597004 | ||
| # assert fixture data to easily understand the test assumptions | ||
| assert len(table_v2.metadata.snapshots) == 2 | ||
| assert len(table_v2.metadata.snapshot_log) == 2 | ||
| assert len(table_v2.metadata.refs) == 2 | ||
| update = RemoveSnapshotsUpdate(snapshot_ids=[REMOVE_SNAPSHOT]) | ||
| new_metadata = update_table_metadata(table_v2.metadata, (update,)) | ||
| assert len(new_metadata.snapshots) == 1 | ||
grihabor marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| assert new_metadata.snapshots[0].snapshot_id == KEEP_SNAPSHOT | ||
| assert new_metadata.snapshots[0].parent_snapshot_id is None | ||
| assert new_metadata.current_snapshot_id == KEEP_SNAPSHOT | ||
| assert new_metadata.last_updated_ms > table_v2.metadata.last_updated_ms | ||
| assert len(new_metadata.snapshot_log) == 1 | ||
| assert new_metadata.snapshot_log[0].snapshot_id == KEEP_SNAPSHOT | ||
| assert len(new_metadata.refs) == 1 | ||
| assert new_metadata.refs["main"].snapshot_id == KEEP_SNAPSHOT | ||
| def test_update_remove_snapshots_doesnt_exist(table_v2: Table) -> None: | ||
| update = RemoveSnapshotsUpdate( | ||
| snapshot_ids=[123], | ||
| ) | ||
| with pytest.raises(ValueError, match="Snapshot with snapshot id 123 does not exist"): | ||
| update_table_metadata(table_v2.metadata, (update,)) | ||
| def test_update_remove_snapshots_remove_current_snapshot_id(table_v2: Table) -> None: | ||
| update = RemoveSnapshotsUpdate(snapshot_ids=[3055729675574597004]) | ||
| new_metadata = update_table_metadata(table_v2.metadata, (update,)) | ||
| assert len(new_metadata.refs) == 1 | ||
| assert new_metadata.refs["test"].snapshot_id == 3051729675574597004 | ||
| assert new_metadata.current_snapshot_id is None | ||
| def test_update_remove_snapshot_ref(table_v2: Table) -> None: | ||
| # assert fixture data to easily understand the test assumptions | ||
| assert len(table_v2.metadata.refs) == 2 | ||
| update = RemoveSnapshotRefUpdate(ref_name="test") | ||
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.
this is the java reference
https://github.com/apache/iceberg/blob/be6e9daf8901cdee63197e77fcf95624bb694f39/core/src/main/java/org/apache/iceberg/TableMetadata.java#L1396-L1426