Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pyiceberg/table/update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good that this uses is not None, so a property set to "" is still removed. Nothing pins it, though: with bool(properties.pop(key, None)) every test still passes, and remove_properties("a") on {"a": ""} silently keeps the key. A case in test_apply_remove_properties_update_ignores_unset_keys would cover it.


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})
Expand Down
17 changes: 17 additions & 0 deletions tests/catalog/test_catalog_behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
25 changes: 25 additions & 0 deletions tests/catalog/test_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,28 @@ 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", "b").commit_transaction()
metadata_location = test_catalog.load_table(identifier).metadata_location
transaction.commit_transaction()

loaded = test_catalog.load_table(identifier)
assert "a" not in loaded.properties and "b" not in loaded.properties
if not isinstance(test_catalog, RestCatalog):
# Every key was already removed, so the staged metadata equals the current metadata and the
# catalog must skip the commit. A REST server decides this itself and may still write a version.
assert loaded.metadata_location == metadata_location


@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:
Expand Down
16 changes: 14 additions & 2 deletions tests/table/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),))

Expand All @@ -1297,6 +1298,17 @@ def test_apply_remove_properties_update_ignores_unset_keys(table_v2: Table) -> N
assert new_metadata.last_updated_ms > base_metadata.last_updated_ms


def test_apply_remove_properties_update_removes_empty_string_value(table_v2: Table) -> None:
base_metadata = update_table_metadata(table_v2.metadata, (SetPropertiesUpdate(updates={"empty": ""}),))
assert base_metadata.properties["empty"] == ""

new_metadata = update_table_metadata(base_metadata, (RemovePropertiesUpdate(removals=["empty"]),))

# A property set to an empty string is still a set property, so removing it is a real change
assert "empty" not in new_metadata.properties
assert new_metadata != base_metadata


def test_apply_add_schema_update(table_v2: Table) -> None:
transaction = table_v2.transaction()
update = transaction.update_schema()
Expand Down
Loading