diff --git a/pyiceberg/table/update/__init__.py b/pyiceberg/table/update/__init__.py index 67a4e1fba6..11299000c0 100644 --- a/pyiceberg/table/update/__init__.py +++ b/pyiceberg/table/update/__init__.py @@ -367,6 +367,11 @@ def _(update: SetPropertiesUpdate, base_metadata: TableMetadata, context: _Table properties = dict(base_metadata.properties) properties.update(update.updates) + if properties == base_metadata.properties: + # Every key already has the requested value, so there is no change to record. Recording the + # update anyway would bump last_updated_ms and defeat the catalogs' skip-commit check. + 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 6a6deffcfc..cc1291fbdb 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_set_unchanged_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().set_properties(a="1").commit_transaction() + + loaded = catalog.load_table(test_table_identifier) + assert loaded.properties["a"] == "1" + # The value is already set, 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/table/test_init.py b/tests/table/test_init.py index f4e0abc412..53c7c81465 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -1304,6 +1304,27 @@ def test_apply_set_properties_update(table_v2: Table) -> None: assert new_metadata_add_only.last_updated_ms > base_metadata.last_updated_ms +def test_apply_set_properties_update_skips_unchanged_values(table_v2: Table) -> None: + base_metadata = table_v2.metadata + + # The property already has this value, so nothing changes and nothing is recorded + unchanged = update_table_metadata(base_metadata, (SetPropertiesUpdate(updates={"read.split.target.size": "134217728"}),)) + assert unchanged == base_metadata + + # A new key, or a new value for an existing key, is a real change + added = update_table_metadata(base_metadata, (SetPropertiesUpdate(updates={"test_a": "test_a"}),)) + assert added.properties == {"read.split.target.size": "134217728", "test_a": "test_a"} + + changed = update_table_metadata(base_metadata, (SetPropertiesUpdate(updates={"read.split.target.size": "1"}),)) + assert changed.properties == {"read.split.target.size": "1"} + + # Mixed: one unchanged key and one new key still commits both + mixed = update_table_metadata( + base_metadata, (SetPropertiesUpdate(updates={"read.split.target.size": "134217728", "test_b": "test_b"}),) + ) + assert mixed.properties == {"read.split.target.size": "134217728", "test_b": "test_b"} + + @pytest.mark.parametrize("table_fixture", ["table_v1", "table_v2"]) @pytest.mark.parametrize("property_name", ["encryption.key-id", "encryption.data-key-length"]) def test_apply_set_properties_update_rejects_encryption_before_v3(