|
138 | 138 | )
|
139 | 139 | from pyiceberg.utils.concurrent import ExecutorFactory
|
140 | 140 | from pyiceberg.utils.datetime import datetime_to_millis
|
| 141 | +from pyiceberg.utils.deprecated import deprecated |
141 | 142 | from pyiceberg.utils.singleton import _convert_to_hashable_type
|
142 | 143 |
|
143 | 144 | if TYPE_CHECKING:
|
@@ -351,6 +352,88 @@ def set_properties(self, properties: Properties = EMPTY_DICT, **kwargs: Any) ->
|
351 | 352 | updates = properties or kwargs
|
352 | 353 | return self._apply((SetPropertiesUpdate(updates=updates),))
|
353 | 354 |
|
| 355 | + @deprecated( |
| 356 | + deprecated_in="0.7.0", |
| 357 | + removed_in="0.8.0", |
| 358 | + help_message="Please use one of the functions in ManageSnapshots instead", |
| 359 | + ) |
| 360 | + def add_snapshot(self, snapshot: Snapshot) -> Transaction: |
| 361 | + """Add a new snapshot to the table. |
| 362 | +
|
| 363 | + Returns: |
| 364 | + The transaction with the add-snapshot staged. |
| 365 | + """ |
| 366 | + updates = (AddSnapshotUpdate(snapshot=snapshot),) |
| 367 | + |
| 368 | + return self._apply(updates, ()) |
| 369 | + |
| 370 | + @deprecated( |
| 371 | + deprecated_in="0.7.0", |
| 372 | + removed_in="0.8.0", |
| 373 | + help_message="Please use one of the functions in ManageSnapshots instead", |
| 374 | + ) |
| 375 | + def set_ref_snapshot( |
| 376 | + self, |
| 377 | + snapshot_id: int, |
| 378 | + parent_snapshot_id: Optional[int], |
| 379 | + ref_name: str, |
| 380 | + type: str, |
| 381 | + max_ref_age_ms: Optional[int] = None, |
| 382 | + max_snapshot_age_ms: Optional[int] = None, |
| 383 | + min_snapshots_to_keep: Optional[int] = None, |
| 384 | + ) -> Transaction: |
| 385 | + """Update a ref to a snapshot. |
| 386 | +
|
| 387 | + Returns: |
| 388 | + The transaction with the set-snapshot-ref staged |
| 389 | + """ |
| 390 | + updates = ( |
| 391 | + SetSnapshotRefUpdate( |
| 392 | + snapshot_id=snapshot_id, |
| 393 | + ref_name=ref_name, |
| 394 | + type=type, |
| 395 | + max_ref_age_ms=max_ref_age_ms, |
| 396 | + max_snapshot_age_ms=max_snapshot_age_ms, |
| 397 | + min_snapshots_to_keep=min_snapshots_to_keep, |
| 398 | + ), |
| 399 | + ) |
| 400 | + |
| 401 | + requirements = (AssertRefSnapshotId(snapshot_id=parent_snapshot_id, ref="main"),) |
| 402 | + return self._apply(updates, requirements) |
| 403 | + |
| 404 | + def _set_ref_snapshot( |
| 405 | + self, |
| 406 | + snapshot_id: int, |
| 407 | + ref_name: str, |
| 408 | + type: str, |
| 409 | + max_ref_age_ms: Optional[int] = None, |
| 410 | + max_snapshot_age_ms: Optional[int] = None, |
| 411 | + min_snapshots_to_keep: Optional[int] = None, |
| 412 | + ) -> UpdatesAndRequirements: |
| 413 | + """Update a ref to a snapshot. |
| 414 | +
|
| 415 | + Returns: |
| 416 | + The updates and requirements for the set-snapshot-ref staged |
| 417 | + """ |
| 418 | + updates = ( |
| 419 | + SetSnapshotRefUpdate( |
| 420 | + snapshot_id=snapshot_id, |
| 421 | + ref_name=ref_name, |
| 422 | + type=type, |
| 423 | + max_ref_age_ms=max_ref_age_ms, |
| 424 | + max_snapshot_age_ms=max_snapshot_age_ms, |
| 425 | + min_snapshots_to_keep=min_snapshots_to_keep, |
| 426 | + ), |
| 427 | + ) |
| 428 | + requirements = ( |
| 429 | + AssertRefSnapshotId( |
| 430 | + snapshot_id=self.table_metadata.refs[ref_name].snapshot_id if ref_name in self.table_metadata.refs else None, |
| 431 | + ref=ref_name, |
| 432 | + ), |
| 433 | + ) |
| 434 | + |
| 435 | + return updates, requirements |
| 436 | + |
354 | 437 | def update_schema(self, allow_incompatible_changes: bool = False, case_sensitive: bool = True) -> UpdateSchema:
|
355 | 438 | """Create a new UpdateSchema to alter the columns of this table.
|
356 | 439 |
|
@@ -1323,6 +1406,21 @@ def history(self) -> List[SnapshotLogEntry]:
|
1323 | 1406 | """Get the snapshot history of this table."""
|
1324 | 1407 | return self.metadata.snapshot_log
|
1325 | 1408 |
|
| 1409 | + def manage_snapshots(self) -> ManageSnapshots: |
| 1410 | + """ |
| 1411 | + Shorthand to run snapshot management operations like create branch, create tag, etc. |
| 1412 | +
|
| 1413 | + Use table.manage_snapshots().<operation>().commit() to run a specific operation. |
| 1414 | + Use table.manage_snapshots().<operation-one>().<operation-two>().commit() to run multiple operations. |
| 1415 | + Pending changes are applied on commit. |
| 1416 | +
|
| 1417 | + We can also use context managers to make more changes. For example, |
| 1418 | +
|
| 1419 | + with table.manage_snapshots() as ms: |
| 1420 | + ms.create_tag(snapshot_id1, "Tag_A").create_tag(snapshot_id2, "Tag_B") |
| 1421 | + """ |
| 1422 | + return ManageSnapshots(transaction=Transaction(self, autocommit=True)) |
| 1423 | + |
1326 | 1424 | def update_schema(self, allow_incompatible_changes: bool = False, case_sensitive: bool = True) -> UpdateSchema:
|
1327 | 1425 | """Create a new UpdateSchema to alter the columns of this table.
|
1328 | 1426 |
|
@@ -1835,6 +1933,84 @@ def __enter__(self) -> U:
|
1835 | 1933 | return self # type: ignore
|
1836 | 1934 |
|
1837 | 1935 |
|
| 1936 | +class ManageSnapshots(UpdateTableMetadata["ManageSnapshots"]): |
| 1937 | + """ |
| 1938 | + Run snapshot management operations using APIs. |
| 1939 | +
|
| 1940 | + APIs include create branch, create tag, etc. |
| 1941 | +
|
| 1942 | + Use table.manage_snapshots().<operation>().commit() to run a specific operation. |
| 1943 | + Use table.manage_snapshots().<operation-one>().<operation-two>().commit() to run multiple operations. |
| 1944 | + Pending changes are applied on commit. |
| 1945 | +
|
| 1946 | + We can also use context managers to make more changes. For example, |
| 1947 | +
|
| 1948 | + with table.manage_snapshots() as ms: |
| 1949 | + ms.create_tag(snapshot_id1, "Tag_A").create_tag(snapshot_id2, "Tag_B") |
| 1950 | + """ |
| 1951 | + |
| 1952 | + _updates: Tuple[TableUpdate, ...] = () |
| 1953 | + _requirements: Tuple[TableRequirement, ...] = () |
| 1954 | + |
| 1955 | + def _commit(self) -> UpdatesAndRequirements: |
| 1956 | + """Apply the pending changes and commit.""" |
| 1957 | + return self._updates, self._requirements |
| 1958 | + |
| 1959 | + def create_tag(self, snapshot_id: int, tag_name: str, max_ref_age_ms: Optional[int] = None) -> ManageSnapshots: |
| 1960 | + """ |
| 1961 | + Create a new tag pointing to the given snapshot id. |
| 1962 | +
|
| 1963 | + Args: |
| 1964 | + snapshot_id (int): snapshot id of the existing snapshot to tag |
| 1965 | + tag_name (str): name of the tag |
| 1966 | + max_ref_age_ms (Optional[int]): max ref age in milliseconds |
| 1967 | +
|
| 1968 | + Returns: |
| 1969 | + This for method chaining |
| 1970 | + """ |
| 1971 | + update, requirement = self._transaction._set_ref_snapshot( |
| 1972 | + snapshot_id=snapshot_id, |
| 1973 | + ref_name=tag_name, |
| 1974 | + type="tag", |
| 1975 | + max_ref_age_ms=max_ref_age_ms, |
| 1976 | + ) |
| 1977 | + self._updates += update |
| 1978 | + self._requirements += requirement |
| 1979 | + return self |
| 1980 | + |
| 1981 | + def create_branch( |
| 1982 | + self, |
| 1983 | + snapshot_id: int, |
| 1984 | + branch_name: str, |
| 1985 | + max_ref_age_ms: Optional[int] = None, |
| 1986 | + max_snapshot_age_ms: Optional[int] = None, |
| 1987 | + min_snapshots_to_keep: Optional[int] = None, |
| 1988 | + ) -> ManageSnapshots: |
| 1989 | + """ |
| 1990 | + Create a new branch pointing to the given snapshot id. |
| 1991 | +
|
| 1992 | + Args: |
| 1993 | + snapshot_id (int): snapshot id of existing snapshot at which the branch is created. |
| 1994 | + branch_name (str): name of the new branch |
| 1995 | + max_ref_age_ms (Optional[int]): max ref age in milliseconds |
| 1996 | + max_snapshot_age_ms (Optional[int]): max age of snapshots to keep in milliseconds |
| 1997 | + min_snapshots_to_keep (Optional[int]): min number of snapshots to keep in milliseconds |
| 1998 | + Returns: |
| 1999 | + This for method chaining |
| 2000 | + """ |
| 2001 | + update, requirement = self._transaction._set_ref_snapshot( |
| 2002 | + snapshot_id=snapshot_id, |
| 2003 | + ref_name=branch_name, |
| 2004 | + type="branch", |
| 2005 | + max_ref_age_ms=max_ref_age_ms, |
| 2006 | + max_snapshot_age_ms=max_snapshot_age_ms, |
| 2007 | + min_snapshots_to_keep=min_snapshots_to_keep, |
| 2008 | + ) |
| 2009 | + self._updates += update |
| 2010 | + self._requirements += requirement |
| 2011 | + return self |
| 2012 | + |
| 2013 | + |
1838 | 2014 | class UpdateSchema(UpdateTableMetadata["UpdateSchema"]):
|
1839 | 2015 | _schema: Schema
|
1840 | 2016 | _last_column_id: itertools.count[int]
|
|
0 commit comments