Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 545
Support snapshot management operations like creating tags by adding ManageSnapshots API#728
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
30 commits
Select commit
Hold shift + click to select a range
5f129b6
create API for set_ref_snapshot
chinmay-bhat ed2e647
remove parent_snapshot_id, add AssertTableUUID
chinmay-bhat fb3e25c
wrap `set_ref_snapshot` API in Table
chinmay-bhat aee9c31
rename variable to match `SetSnapshotRefUpdate`
chinmay-bhat 8cd7846
add tests for table and transaction api
chinmay-bhat b6f8596
hide set_ref_snapshot from public API
chinmay-bhat 815cf1f
deprecate add_snapshot()
chinmay-bhat 1c70f62
add create_tag, create_branch apis
chinmay-bhat 6c1e78f
small updates
chinmay-bhat ed05dc9
update tests
chinmay-bhat 6447f6c
expose ManageSnapshot to Table
chinmay-bhat 0349bbe
rename transaction to _transaction
chinmay-bhat 1343d45
apply suggestions
chinmay-bhat 6537229
update only if there are updates
chinmay-bhat 042a1e7
add docstring
chinmay-bhat fbfd463
remove print
chinmay-bhat 8cf9a8c
update to integration tests
chinmay-bhat 5ba32f1
resolve mypy error: does not explicitly export attribute
chinmay-bhat febd36b
improve docstring
chinmay-bhat 5d2b20e
fix failing tests
chinmay-bhat 5f54ff9
move AssertRef to _set_ref_snapshot
chinmay-bhat 8a923a4
remove unused parent_snapshot_id
chinmay-bhat 658c45e
improve docstring for public api manage_snapshots()
chinmay-bhat 97f142a
rename tests
chinmay-bhat 603d5b3
update docs
chinmay-bhat 1eba937
update commit()
chinmay-bhat fe686fc
update tests
chinmay-bhat 1f32a6f
update docs
chinmay-bhat 9f068a6
remove AssertTableUUID
chinmay-bhat 41f5f16
rename test file to fix mypy error
chinmay-bhat 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
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 |
|---|---|---|
| @@ -138,6 +138,7 @@ | ||
| ) | ||
| from pyiceberg.utils.concurrent import ExecutorFactory | ||
| from pyiceberg.utils.datetime import datetime_to_millis | ||
| from pyiceberg.utils.deprecated import deprecated | ||
| from pyiceberg.utils.singleton import _convert_to_hashable_type | ||
| if TYPE_CHECKING: | ||
| @@ -351,6 +352,88 @@ def set_properties(self, properties: Properties = EMPTY_DICT, **kwargs: Any) -> | ||
| updates = properties or kwargs | ||
| return self._apply((SetPropertiesUpdate(updates=updates),)) | ||
| @deprecated( | ||
| deprecated_in="0.7.0", | ||
| removed_in="0.8.0", | ||
| help_message="Please use one of the functions in ManageSnapshots instead", | ||
| ) | ||
| def add_snapshot(self, snapshot: Snapshot) -> Transaction: | ||
| """Add a new snapshot to the table. | ||
| Returns: | ||
| The transaction with the add-snapshot staged. | ||
| """ | ||
| updates = (AddSnapshotUpdate(snapshot=snapshot),) | ||
| return self._apply(updates, ()) | ||
| @deprecated( | ||
| deprecated_in="0.7.0", | ||
| removed_in="0.8.0", | ||
| help_message="Please use one of the functions in ManageSnapshots instead", | ||
| ) | ||
| def set_ref_snapshot( | ||
| self, | ||
| snapshot_id: int, | ||
| parent_snapshot_id: Optional[int], | ||
| ref_name: str, | ||
| type: str, | ||
| max_ref_age_ms: Optional[int] = None, | ||
| max_snapshot_age_ms: Optional[int] = None, | ||
| min_snapshots_to_keep: Optional[int] = None, | ||
| ) -> Transaction: | ||
| """Update a ref to a snapshot. | ||
| Returns: | ||
| The transaction with the set-snapshot-ref staged | ||
| """ | ||
| updates = ( | ||
| SetSnapshotRefUpdate( | ||
| snapshot_id=snapshot_id, | ||
| ref_name=ref_name, | ||
| type=type, | ||
| max_ref_age_ms=max_ref_age_ms, | ||
| max_snapshot_age_ms=max_snapshot_age_ms, | ||
| min_snapshots_to_keep=min_snapshots_to_keep, | ||
| ), | ||
| ) | ||
| requirements = (AssertRefSnapshotId(snapshot_id=parent_snapshot_id, ref="main"),) | ||
| return self._apply(updates, requirements) | ||
| def _set_ref_snapshot( | ||
| self, | ||
| snapshot_id: int, | ||
| ref_name: str, | ||
| type: str, | ||
| max_ref_age_ms: Optional[int] = None, | ||
| max_snapshot_age_ms: Optional[int] = None, | ||
| min_snapshots_to_keep: Optional[int] = None, | ||
| ) -> UpdatesAndRequirements: | ||
| """Update a ref to a snapshot. | ||
| Returns: | ||
| The updates and requirements for the set-snapshot-ref staged | ||
| """ | ||
| updates = ( | ||
| SetSnapshotRefUpdate( | ||
| snapshot_id=snapshot_id, | ||
| ref_name=ref_name, | ||
| type=type, | ||
| max_ref_age_ms=max_ref_age_ms, | ||
| max_snapshot_age_ms=max_snapshot_age_ms, | ||
| min_snapshots_to_keep=min_snapshots_to_keep, | ||
| ), | ||
| ) | ||
| requirements = ( | ||
| AssertRefSnapshotId( | ||
| snapshot_id=self.table_metadata.refs[ref_name].snapshot_id if ref_name in self.table_metadata.refs else None, | ||
| ref=ref_name, | ||
| ), | ||
| ) | ||
| return updates, requirements | ||
| def update_schema(self, allow_incompatible_changes: bool = False, case_sensitive: bool = True) -> UpdateSchema: | ||
| """Create a new UpdateSchema to alter the columns of this table. | ||
| @@ -1323,6 +1406,21 @@ def history(self) -> List[SnapshotLogEntry]: | ||
| """Get the snapshot history of this table.""" | ||
| return self.metadata.snapshot_log | ||
| def manage_snapshots(self) -> ManageSnapshots: | ||
| """ | ||
| Shorthand to run snapshot management operations like create branch, create tag, etc. | ||
| Use table.manage_snapshots().<operation>().commit() to run a specific operation. | ||
| Use table.manage_snapshots().<operation-one>().<operation-two>().commit() to run multiple operations. | ||
| Pending changes are applied on commit. | ||
| We can also use context managers to make more changes. For example, | ||
| with table.manage_snapshots() as ms: | ||
| ms.create_tag(snapshot_id1, "Tag_A").create_tag(snapshot_id2, "Tag_B") | ||
| """ | ||
| return ManageSnapshots(transaction=Transaction(self, autocommit=True)) | ||
| def update_schema(self, allow_incompatible_changes: bool = False, case_sensitive: bool = True) -> UpdateSchema: | ||
| """Create a new UpdateSchema to alter the columns of this table. | ||
| @@ -1835,6 +1933,84 @@ def __enter__(self) -> U: | ||
| return self # type: ignore | ||
| class ManageSnapshots(UpdateTableMetadata["ManageSnapshots"]): | ||
Fokko marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| Run snapshot management operations using APIs. | ||
| APIs include create branch, create tag, etc. | ||
| Use table.manage_snapshots().<operation>().commit() to run a specific operation. | ||
| Use table.manage_snapshots().<operation-one>().<operation-two>().commit() to run multiple operations. | ||
| Pending changes are applied on commit. | ||
| We can also use context managers to make more changes. For example, | ||
| with table.manage_snapshots() as ms: | ||
| ms.create_tag(snapshot_id1, "Tag_A").create_tag(snapshot_id2, "Tag_B") | ||
| """ | ||
| _updates: Tuple[TableUpdate, ...] = () | ||
| _requirements: Tuple[TableRequirement, ...] = () | ||
| def _commit(self) -> UpdatesAndRequirements: | ||
| """Apply the pending changes and commit.""" | ||
| return self._updates, self._requirements | ||
| def create_tag(self, snapshot_id: int, tag_name: str, max_ref_age_ms: Optional[int] = None) -> ManageSnapshots: | ||
| """ | ||
chinmay-bhat marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Create a new tag pointing to the given snapshot id. | ||
| Args: | ||
| snapshot_id (int): snapshot id of the existing snapshot to tag | ||
| tag_name (str): name of the tag | ||
| max_ref_age_ms (Optional[int]): max ref age in milliseconds | ||
| Returns: | ||
| This for method chaining | ||
| """ | ||
| update, requirement = self._transaction._set_ref_snapshot( | ||
| snapshot_id=snapshot_id, | ||
| ref_name=tag_name, | ||
| type="tag", | ||
| max_ref_age_ms=max_ref_age_ms, | ||
| ) | ||
| self._updates += update | ||
| self._requirements += requirement | ||
| return self | ||
| def create_branch( | ||
| self, | ||
| snapshot_id: int, | ||
| branch_name: str, | ||
| max_ref_age_ms: Optional[int] = None, | ||
| max_snapshot_age_ms: Optional[int] = None, | ||
| min_snapshots_to_keep: Optional[int] = None, | ||
| ) -> ManageSnapshots: | ||
| """ | ||
| Create a new branch pointing to the given snapshot id. | ||
| Args: | ||
| snapshot_id (int): snapshot id of existing snapshot at which the branch is created. | ||
| branch_name (str): name of the new branch | ||
| max_ref_age_ms (Optional[int]): max ref age in milliseconds | ||
| max_snapshot_age_ms (Optional[int]): max age of snapshots to keep in milliseconds | ||
| min_snapshots_to_keep (Optional[int]): min number of snapshots to keep in milliseconds | ||
| Returns: | ||
| This for method chaining | ||
| """ | ||
| update, requirement = self._transaction._set_ref_snapshot( | ||
| snapshot_id=snapshot_id, | ||
| ref_name=branch_name, | ||
| type="branch", | ||
| max_ref_age_ms=max_ref_age_ms, | ||
| max_snapshot_age_ms=max_snapshot_age_ms, | ||
| min_snapshots_to_keep=min_snapshots_to_keep, | ||
| ) | ||
| self._updates += update | ||
| self._requirements += requirement | ||
| return self | ||
| class UpdateSchema(UpdateTableMetadata["UpdateSchema"]): | ||
| _schema: Schema | ||
| _last_column_id: itertools.count[int] | ||
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,42 @@ | ||
| # 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. | ||
| import pytest | ||
| from pyiceberg.catalog import Catalog | ||
| from pyiceberg.table.refs import SnapshotRef | ||
| @pytest.mark.integration | ||
| @pytest.mark.parametrize("catalog", [pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("session_catalog")]) | ||
| def test_create_tag(catalog: Catalog) -> None: | ||
| identifier = "default.test_table_snapshot_operations" | ||
| tbl = catalog.load_table(identifier) | ||
| assert len(tbl.history()) > 3 | ||
| tag_snapshot_id = tbl.history()[-3].snapshot_id | ||
| tbl.manage_snapshots().create_tag(snapshot_id=tag_snapshot_id, tag_name="tag123").commit() | ||
| assert tbl.metadata.refs["tag123"] == SnapshotRef(snapshot_id=tag_snapshot_id, snapshot_ref_type="tag") | ||
| @pytest.mark.integration | ||
| @pytest.mark.parametrize("catalog", [pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("session_catalog")]) | ||
| def test_create_branch(catalog: Catalog) -> None: | ||
| identifier = "default.test_table_snapshot_operations" | ||
| tbl = catalog.load_table(identifier) | ||
| assert len(tbl.history()) > 2 | ||
| branch_snapshot_id = tbl.history()[-2].snapshot_id | ||
| tbl.manage_snapshots().create_branch(snapshot_id=branch_snapshot_id, branch_name="branch123").commit() | ||
| assert tbl.metadata.refs["branch123"] == SnapshotRef(snapshot_id=branch_snapshot_id, snapshot_ref_type="branch") |
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
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.