From d4c3699da1ae93fece5266596d99fff32afa2384 Mon Sep 17 00:00:00 2001 From: Sreesh Maheshwar Date: Sun, 18 Oct 2026 20:44:26 +0100 Subject: [PATCH 1/2] Ignore unset keys when removing table properties Applying a property removal popped each key, so the commit failed with a KeyError if another writer had already removed the property. Skip keys that aren't set, as the REST catalog does, and treat a removal of only unset keys as a no-op. Co-Authored-By: Claude Opus 5.5 --- pyiceberg/table/update/__init__.py | 9 +++++---- tests/catalog/test_catalog_behaviors.py | 14 ++++++++++++++ tests/integration/test_reads.py | 9 ++++----- tests/table/test_init.py | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/pyiceberg/table/update/__init__.py b/pyiceberg/table/update/__init__.py index a57d74d6d8..0760965109 100644 --- a/pyiceberg/table/update/__init__.py +++ b/pyiceberg/table/update/__init__.py @@ -373,12 +373,13 @@ def _(update: SetPropertiesUpdate, base_metadata: TableMetadata, context: _Table @_apply_table_update.register(RemovePropertiesUpdate) def _(update: RemovePropertiesUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata: - if len(update.removals) == 0: - return base_metadata - properties = dict(base_metadata.properties) for key in update.removals: - properties.pop(key) + # Ignore keys that aren't set, since a concurrent commit may have removed them + properties.pop(key, None) + + if len(properties) == len(base_metadata.properties): + 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 82990c1033..832fc35d1b 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -755,6 +755,20 @@ def test_table_properties_raise_for_none_value( assert "None type is not a supported value in properties: property_name" in str(exc_info.value) +def test_remove_properties_already_removed_by_concurrent_commit( + 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", "b": "2"}) + + transaction = table.transaction().remove_properties("a", "b") + catalog.load_table(test_table_identifier).transaction().remove_properties("a").commit_transaction() + transaction.commit_transaction() + + assert catalog.load_table(test_table_identifier).properties == {} + + # Append table diff --git a/tests/integration/test_reads.py b/tests/integration/test_reads.py index 2c5dd26b73..27f136736b 100644 --- a/tests/integration/test_reads.py +++ b/tests/integration/test_reads.py @@ -302,7 +302,7 @@ def test_hive_critical_properties_always_from_iceberg(catalog: Catalog) -> None: def test_hive_native_properties_cannot_be_deleted_via_iceberg(catalog: Catalog) -> None: """Test that HMS-native properties (set outside Iceberg) cannot be deleted via Iceberg. - HMS-native properties are not visible to Iceberg, so remove_properties fails with KeyError. + HMS-native properties are not visible to Iceberg, so remove_properties leaves them in place. However, if you first SET an HMS property via Iceberg (making it tracked in Iceberg metadata), it can then be deleted via Iceberg. """ @@ -326,16 +326,15 @@ def test_hive_native_properties_cannot_be_deleted_via_iceberg(catalog: Catalog) # Verify the HMS-native property is NOT visible in Iceberg assert "hms_native_prop" not in table.properties - # Attempt to remove the HMS-native property via Iceberg - this should fail + # Attempt to remove the HMS-native property via Iceberg - this is a no-op # because the property is not tracked in Iceberg metadata (not visible to Iceberg) - with pytest.raises(KeyError): - table.transaction().remove_properties("hms_native_prop").commit_transaction() + table.transaction().remove_properties("hms_native_prop").commit_transaction() # HMS-native property should still exist (cannot be deleted via Iceberg) with hive_client as open_client: hive_table = _get_hive_table(open_client) assert hive_table.parameters.get("hms_native_prop") == "native_value", ( - "HMS-native property should still exist since Iceberg removal failed!" + "HMS-native property should still exist since Iceberg removal is a no-op!" ) # Now SET the same property via Iceberg (this makes it tracked in Iceberg metadata) diff --git a/tests/table/test_init.py b/tests/table/test_init.py index b26b4db4f6..f04b585fb7 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -1226,6 +1226,20 @@ def test_apply_remove_properties_update(table_v2: Table) -> None: assert new_metadata.properties == {"read.split.target.size": "134217728", "test_b": "test_b", "test_d": "test_d"} +def test_apply_remove_properties_update_ignores_unset_keys(table_v2: Table) -> None: + base_metadata = table_v2.metadata + + new_metadata_no_removal = update_table_metadata(base_metadata, (RemovePropertiesUpdate(removals=["test_a"]),)) + + assert base_metadata == new_metadata_no_removal + + new_metadata = update_table_metadata(base_metadata, (RemovePropertiesUpdate(removals=["read.split.target.size", "test_a"]),)) + + assert base_metadata.properties == {"read.split.target.size": "134217728"} + assert new_metadata.properties == {} + assert new_metadata.last_updated_ms > base_metadata.last_updated_ms + + def test_apply_add_schema_update(table_v2: Table) -> None: transaction = table_v2.transaction() update = transaction.update_schema() From e76329cc68455d52800af668525db1297e900756 Mon Sep 17 00:00:00 2001 From: Sreesh Maheshwar Date: Mon, 30 Nov 2026 14:26:17 +0000 Subject: [PATCH 2/2] Record property removals even when none of the keys are set As Java's TableMetadata.Builder.removeProperties does, only an empty removal is skipped. Co-Authored-By: Claude Opus 5.5 --- pyiceberg/table/update/__init__.py | 6 +++--- tests/integration/test_reads.py | 4 ++-- tests/table/test_init.py | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pyiceberg/table/update/__init__.py b/pyiceberg/table/update/__init__.py index 0760965109..a48c15c6ef 100644 --- a/pyiceberg/table/update/__init__.py +++ b/pyiceberg/table/update/__init__.py @@ -373,14 +373,14 @@ def _(update: SetPropertiesUpdate, base_metadata: TableMetadata, context: _Table @_apply_table_update.register(RemovePropertiesUpdate) def _(update: RemovePropertiesUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata: + if len(update.removals) == 0: + return base_metadata + properties = dict(base_metadata.properties) for key in update.removals: # Ignore keys that aren't set, since a concurrent commit may have removed them properties.pop(key, None) - if len(properties) == len(base_metadata.properties): - return base_metadata - context.add_update(update) return base_metadata.model_copy(update={"properties": properties}) diff --git a/tests/integration/test_reads.py b/tests/integration/test_reads.py index 27f136736b..a6794c095a 100644 --- a/tests/integration/test_reads.py +++ b/tests/integration/test_reads.py @@ -326,7 +326,7 @@ def test_hive_native_properties_cannot_be_deleted_via_iceberg(catalog: Catalog) # Verify the HMS-native property is NOT visible in Iceberg assert "hms_native_prop" not in table.properties - # Attempt to remove the HMS-native property via Iceberg - this is a no-op + # Removing the HMS-native property via Iceberg leaves it in place, # because the property is not tracked in Iceberg metadata (not visible to Iceberg) table.transaction().remove_properties("hms_native_prop").commit_transaction() @@ -334,7 +334,7 @@ def test_hive_native_properties_cannot_be_deleted_via_iceberg(catalog: Catalog) with hive_client as open_client: hive_table = _get_hive_table(open_client) assert hive_table.parameters.get("hms_native_prop") == "native_value", ( - "HMS-native property should still exist since Iceberg removal is a no-op!" + "HMS-native property should still exist since Iceberg doesn't track it!" ) # Now SET the same property via Iceberg (this makes it tracked in Iceberg metadata) diff --git a/tests/table/test_init.py b/tests/table/test_init.py index f04b585fb7..4c3a8775e9 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -1231,7 +1231,8 @@ 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 base_metadata == new_metadata_no_removal + assert new_metadata_no_removal.properties == base_metadata.properties + assert new_metadata_no_removal.last_updated_ms > base_metadata.last_updated_ms new_metadata = update_table_metadata(base_metadata, (RemovePropertiesUpdate(removals=["read.split.target.size", "test_a"]),))