Skip to content

Commit 9a9a9f0

Browse files
committed
Add RemoveSchemasUpdate event
1 parent ca70442 commit 9a9a9f0

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pyiceberg/table/update/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ class RemoveStatisticsUpdate(IcebergBaseModel):
198198
snapshot_id: int = Field(alias="snapshot-id")
199199

200200

201+
class RemoveSchemasUpdate(IcebergBaseModel):
202+
action: Literal["remove-schemas"] = Field(default="remove-schemas")
203+
schema_ids: List[int] = Field(alias="schema-ids")
204+
205+
201206
TableUpdate = Annotated[
202207
Union[
203208
AssignUUIDUpdate,
@@ -217,6 +222,7 @@ class RemoveStatisticsUpdate(IcebergBaseModel):
217222
RemovePropertiesUpdate,
218223
SetStatisticsUpdate,
219224
RemoveStatisticsUpdate,
225+
RemoveSchemasUpdate,
220226
],
221227
Field(discriminator="action"),
222228
]
@@ -582,6 +588,23 @@ def _(update: RemoveStatisticsUpdate, base_metadata: TableMetadata, context: _Ta
582588
return base_metadata.model_copy(update={"statistics": statistics})
583589

584590

591+
@_apply_table_update.register(RemoveSchemasUpdate)
592+
def _(update: RemoveSchemasUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata:
593+
# This method should error if any schemas do not exist.
594+
# It should error if the default schema is being removed.
595+
# Otherwise, remove the schemas listed in update.schema_ids.
596+
for remove_schema_id in update.schema_ids:
597+
if not any(schema.schema_id == remove_schema_id for schema in base_metadata.schemas):
598+
raise ValueError(f"Schema with schema id {remove_schema_id} does not exist")
599+
if base_metadata.current_schema_id == remove_schema_id:
600+
raise ValueError(f"Cannot remove current schema with id {remove_schema_id}")
601+
602+
schemas = [schema for schema in base_metadata.schemas if schema.schema_id not in update.schema_ids]
603+
context.add_update(update)
604+
605+
return base_metadata.model_copy(update={"schemas": schemas})
606+
607+
585608
def update_table_metadata(
586609
base_metadata: TableMetadata,
587610
updates: Tuple[TableUpdate, ...],

tests/table/test_init.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
AssertRefSnapshotId,
7878
AssertTableUUID,
7979
RemovePropertiesUpdate,
80+
RemoveSchemasUpdate,
8081
RemoveSnapshotRefUpdate,
8182
RemoveSnapshotsUpdate,
8283
RemoveStatisticsUpdate,
@@ -1267,6 +1268,35 @@ def test_update_metadata_log_overflow(table_v2: Table) -> None:
12671268
assert len(new_metadata.metadata_log) == 1
12681269

12691270

1271+
def test_remove_schemas_update(table_v2: Table) -> None:
1272+
base_metadata = table_v2.metadata
1273+
assert len(base_metadata.schemas) == 2
1274+
1275+
update = RemoveSchemasUpdate(schema_ids=[0])
1276+
updated_metadata = update_table_metadata(
1277+
base_metadata,
1278+
(update,),
1279+
)
1280+
1281+
assert len(updated_metadata.schemas) == 1
1282+
1283+
1284+
def test_remove_schemas_update_schema_does_not_exist(table_v2: Table) -> None:
1285+
update = RemoveSchemasUpdate(
1286+
schema_ids=[123],
1287+
)
1288+
with pytest.raises(ValueError, match="Schema with schema id 123 does not exist"):
1289+
update_table_metadata(table_v2.metadata, (update,))
1290+
1291+
1292+
def test_remove_schemas_update_current_schema(table_v2: Table) -> None:
1293+
update = RemoveSchemasUpdate(
1294+
schema_ids=[1],
1295+
)
1296+
with pytest.raises(ValueError, match="Cannot remove current schema with id 1"):
1297+
update_table_metadata(table_v2.metadata, (update,))
1298+
1299+
12701300
def test_set_statistics_update(table_v2_with_statistics: Table) -> None:
12711301
snapshot_id = table_v2_with_statistics.metadata.current_snapshot_id
12721302

0 commit comments

Comments
 (0)