From a56838dc5d9acc5f0e0d70919bfc433c7d0756f1 Mon Sep 17 00:00:00 2001 From: Kevin Liu Date: Sun, 21 Jan 2024 19:51:08 -0800 Subject: [PATCH] fix schema_id during AddSchemaUpdate --- pyiceberg/table/__init__.py | 7 +++++-- tests/catalog/test_base.py | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index 8953b69866..4759ec7add 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -504,8 +504,11 @@ def _(update: AddSchemaUpdate, base_metadata: TableMetadata, context: _TableMeta if update.last_column_id < base_metadata.last_column_id: raise ValueError(f"Invalid last column id {update.last_column_id}, must be >= {base_metadata.last_column_id}") - # PROBLEM: `update.schema_`'s `schema_id` starts with 0 but should be 1 - # fresh_schema = assign_fresh_schema_ids(update.schema_) + # `update.schema_.schema_id` should be the last_schema_id + 1 + last_schema_id = max(schema.schema_id for schema in base_metadata.schemas) + next_schema_id = last_schema_id + 1 + new_schema = update.schema_.model_copy(update={"schema_id": next_schema_id}) + update = update.model_copy(update={"schema_": new_schema}) context.add_update(update) return base_metadata.model_copy( diff --git a/tests/catalog/test_base.py b/tests/catalog/test_base.py index d99c637841..c3090cd975 100644 --- a/tests/catalog/test_base.py +++ b/tests/catalog/test_base.py @@ -397,8 +397,10 @@ def test_commit_table(catalog: InMemoryCatalog) -> None: # Then assert response.metadata.table_uuid == given_table.metadata.table_uuid - # assert len(response.metadata.schemas) == 1 - # assert response.metadata.schemas[0] == new_schema + assert given_table.metadata.current_schema_id == 1 + assert len(response.metadata.schemas) == 2 + assert response.metadata.schemas[1] == new_schema + assert given_table.metadata.last_column_id == new_schema.highest_field_id def test_add_column(catalog: InMemoryCatalog) -> None: