From 2463861eb7df25a62d89a1b8b6603d0fa9d39b7d Mon Sep 17 00:00:00 2001 From: Tom Ron Date: Mon, 30 Nov 2026 21:16:06 +0200 Subject: [PATCH] Skip remove-properties commits that change nothing Removing property keys that aren't set still recorded the update, so update_table_metadata bumped last_updated_ms and defeated the catalogs' skip-commit check: a repeated remove_properties, or one that loses a race to a concurrent removal, wrote a new metadata JSON, added a metadata-log entry and swapped the catalog pointer for a commit that changes nothing. Record the update only when at least one key was removed, the same way the set-properties and upgrade-format-version handlers return early. Closes #4538 --- pyiceberg/table/update/__init__.py | 9 ++++++++- tests/catalog/test_catalog_behaviors.py | 17 +++++++++++++++++ tests/catalog/test_glue.py | 25 +++++++++++++++++++++++++ tests/integration/test_catalog.py | 16 ++++++++++++++++ tests/table/test_init.py | 5 +++-- 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/pyiceberg/table/update/__init__.py b/pyiceberg/table/update/__init__.py index 67a4e1fba6..89da2ff0b4 100644 --- a/pyiceberg/table/update/__init__.py +++ b/pyiceberg/table/update/__init__.py @@ -377,9 +377,16 @@ def _(update: RemovePropertiesUpdate, base_metadata: TableMetadata, context: _Ta return base_metadata properties = dict(base_metadata.properties) + removed = False for key in update.removals: # Ignore keys that aren't set, since a concurrent commit may have removed them - properties.pop(key, None) + removed |= properties.pop(key, None) is not None + + if not removed: + # Nothing was removed, so there is no change to record. Recording the update anyway + # would bump last_updated_ms and defeat the catalogs' skip-commit check, writing a + # new metadata version that changes nothing. + return base_metadata context.add_update(update) return base_metadata.model_copy(update={"properties": properties}) diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index 4e51a4dbda..80790fe1f2 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -778,6 +778,23 @@ def test_remove_properties_already_removed_by_concurrent_commit( assert catalog.load_table(test_table_identifier).properties == {} +def test_remove_unset_properties_writes_no_new_metadata( + catalog: Catalog, table_schema_simple: Schema, test_table_identifier: Identifier +) -> None: + namespace = Catalog.namespace_from(test_table_identifier) + catalog.create_namespace(namespace) + table = catalog.create_table(test_table_identifier, table_schema_simple, properties={"a": "1"}) + + metadata_location = table.metadata_location + table.transaction().remove_properties("missing", "also-missing").commit_transaction() + + loaded = catalog.load_table(test_table_identifier) + assert loaded.properties == {"a": "1"} + # Nothing was removed, so the staged metadata equals the current metadata and the + # catalog must skip the commit instead of writing a metadata version that changes nothing + assert loaded.metadata_location == metadata_location + + # Append table diff --git a/tests/catalog/test_glue.py b/tests/catalog/test_glue.py index 5f1ecab2c7..1980f2e028 100644 --- a/tests/catalog/test_glue.py +++ b/tests/catalog/test_glue.py @@ -133,6 +133,31 @@ def test_create_table_with_database_location( assert storage_descriptor["Location"] == f"s3://{BUCKET_NAME}/{database_name}.db/{table_name}" +@mock_aws +def test_remove_unset_properties_writes_no_new_metadata( + _glue: boto3.client, + _bucket_initialize: None, + moto_endpoint_url: str, + table_schema_nested: Schema, + database_name: str, + table_name: str, +) -> None: + catalog_name = "glue" + identifier = (database_name, table_name) + test_catalog = GlueCatalog(catalog_name, client=None, **{"s3.endpoint": moto_endpoint_url}) + test_catalog.create_namespace(namespace=database_name, properties={"location": f"s3://{BUCKET_NAME}/{database_name}.db"}) + table = test_catalog.create_table(identifier, table_schema_nested, properties={"a": "1"}) + + metadata_location = table.metadata_location + table.transaction().remove_properties("missing").commit_transaction() + + loaded = test_catalog.load_table(identifier) + assert loaded.properties["a"] == "1" + # Nothing was removed, so the staged metadata equals the current metadata and the + # catalog must skip the commit instead of writing a metadata version that changes nothing + assert loaded.metadata_location == metadata_location + + @mock_aws def test_create_v1_table( _bucket_initialize: None, diff --git a/tests/integration/test_catalog.py b/tests/integration/test_catalog.py index ab9589cb85..4e7f82fb16 100644 --- a/tests/integration/test_catalog.py +++ b/tests/integration/test_catalog.py @@ -583,6 +583,22 @@ def test_update_table_spec_then_revert(test_catalog: Catalog, test_schema: Schem assert table.spec() == initial_spec +@pytest.mark.integration +@pytest.mark.parametrize("test_catalog", CATALOGS) +def test_remove_properties_already_removed_by_concurrent_commit( + test_catalog: Catalog, test_schema: Schema, table_name: str, database_name: str +) -> None: + identifier = (database_name, table_name) + test_catalog.create_namespace(database_name) + table = test_catalog.create_table(identifier, test_schema, properties={"a": "1", "b": "2"}) + + transaction = table.transaction().remove_properties("a", "b") + test_catalog.load_table(identifier).transaction().remove_properties("a").commit_transaction() + transaction.commit_transaction() + + assert test_catalog.load_table(identifier).properties == {} + + @pytest.mark.integration @pytest.mark.parametrize("test_catalog", CATALOGS) def test_register_table(test_catalog: Catalog, table_schema_nested: Schema, table_name: str, database_name: str) -> None: diff --git a/tests/table/test_init.py b/tests/table/test_init.py index f6e0bab93a..30421cc365 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -1287,8 +1287,9 @@ def test_apply_remove_properties_update_ignores_unset_keys(table_v2: Table) -> N new_metadata_no_removal = update_table_metadata(base_metadata, (RemovePropertiesUpdate(removals=["test_a"]),)) - assert new_metadata_no_removal.properties == base_metadata.properties - assert new_metadata_no_removal.last_updated_ms > base_metadata.last_updated_ms + # Removing only unset keys is a no-op: nothing is recorded, so last_updated_ms is not bumped + # and the catalogs' skip-commit check still recognizes the metadata as unchanged + assert new_metadata_no_removal == base_metadata new_metadata = update_table_metadata(base_metadata, (RemovePropertiesUpdate(removals=["read.split.target.size", "test_a"]),))