Skip to content

Commit 4842d91

Browse files
committed
fix schema_id during AddSchemaUpdate
1 parent 0b19e4d commit 4842d91

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

pyiceberg/table/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,11 @@ def _(update: AddSchemaUpdate, base_metadata: TableMetadata, context: _TableMeta
504504
if update.last_column_id < base_metadata.last_column_id:
505505
raise ValueError(f"Invalid last column id {update.last_column_id}, must be >= {base_metadata.last_column_id}")
506506

507-
# PROBLEM: `update.schema_`'s `schema_id` starts with 0 but should be 1
508-
# fresh_schema = assign_fresh_schema_ids(update.schema_)
507+
# `update.schema_.schema_id` should be the last_schema_id + 1
508+
last_schema_id = max(schema.schema_id for schema in base_metadata.schemas)
509+
next_schema_id = last_schema_id + 1
510+
new_schema = update.schema_.model_copy(update={"schema_id": next_schema_id})
511+
update = update.model_copy(update={"schema_": new_schema})
509512

510513
context.add_update(update)
511514
return base_metadata.model_copy(

tests/catalog/test_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,10 @@ def test_commit_table(catalog: InMemoryCatalog) -> None:
397397

398398
# Then
399399
assert response.metadata.table_uuid == given_table.metadata.table_uuid
400-
# assert len(response.metadata.schemas) == 1
401-
# assert response.metadata.schemas[0] == new_schema
400+
assert given_table.metadata.current_schema_id == 1
401+
assert len(response.metadata.schemas) == 2
402+
assert response.metadata.schemas[1] == new_schema
403+
assert given_table.metadata.last_column_id == new_schema.highest_field_id
402404

403405

404406
def test_add_column(catalog: InMemoryCatalog) -> None:

0 commit comments

Comments
 (0)