Skip to content
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

Implement update for remove-snapshots action #1561

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pyiceberg/table/update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,19 @@ 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for remove_snapshot_id in update.snapshot_ids:
if remove_snapshot_id == base_metadata.current_snapshot_id:
raise ValueError(f"Can't remove current snapshot id {remove_snapshot_id}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we block the current snapshot?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert in iceberg spec, but it's not clear what should happen if you try to remove the current snapshot.

I'm also not sure if I should update parent_snapshot_id in every snapshot that was referencing removed snapshots

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to set parent_snapshot_id to None if the parent is gone

if not any(s.snapshot_id == remove_snapshot_id for s in base_metadata.snapshots):
raise ValueError(f"Snapshot with snapshot id {remove_snapshot_id} does not exist: {base_metadata.snapshots}")

snapshots = [s for s in base_metadata.snapshots if s.snapshot_id not in update.snapshot_ids]
context.add_update(update)
return base_metadata.model_copy(update={"snapshots": snapshots})


@_apply_table_update.register(AddSortOrderUpdate)
def _(update: AddSortOrderUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata:
context.add_update(update)
Expand Down
29 changes: 29 additions & 0 deletions tests/table/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
AssertRefSnapshotId,
AssertTableUUID,
RemovePropertiesUpdate,
RemoveSnapshotsUpdate,
RemoveStatisticsUpdate,
SetDefaultSortOrderUpdate,
SetPropertiesUpdate,
Expand Down Expand Up @@ -793,6 +794,34 @@ def test_update_metadata_set_snapshot_ref(table_v2: Table) -> None:
)


def test_update_remove_snapshots(table_v2: Table) -> None:
update = RemoveSnapshotsUpdate(
snapshot_ids=[3051729675574597004],
)
new_metadata = update_table_metadata(table_v2.metadata, (update,))
assert len(new_metadata.snapshots) == 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if theres 1 snapshot, isnt that the current snapshot. if so, wouldnt it error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what's left after the removal, originally fixture has 2 snapshots

assert new_metadata.snapshots[0].snapshot_id == 3055729675574597004
assert new_metadata.snapshots[0].parent_snapshot_id == None
assert new_metadata.current_snapshot_id == 3055729675574597004
assert new_metadata.last_updated_ms > table_v2.metadata.last_updated_ms


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_cant_remove_current_snapshot_id(table_v2: Table) -> None:
update = RemoveSnapshotsUpdate(
snapshot_ids=[3055729675574597004],
)
with pytest.raises(ValueError, match="Can't remove current snapshot id 3055729675574597004"):
update_table_metadata(table_v2.metadata, (update,))


def test_update_metadata_add_update_sort_order(table_v2: Table) -> None:
new_sort_order = SortOrder(order_id=table_v2.sort_order().order_id + 1)
new_metadata = update_table_metadata(
Expand Down
Loading