diff --git a/pyiceberg/table/update/__init__.py b/pyiceberg/table/update/__init__.py index a57d74d6d8..a48c15c6ef 100644 --- a/pyiceberg/table/update/__init__.py +++ b/pyiceberg/table/update/__init__.py @@ -378,7 +378,8 @@ def _(update: RemovePropertiesUpdate, base_metadata: TableMetadata, context: _Ta 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) 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..a6794c095a 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 + # Removing the HMS-native property via Iceberg leaves it in place, # 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 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 b26b4db4f6..4c3a8775e9 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -1226,6 +1226,21 @@ 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 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"]),)) + + 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()