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

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
16 changes: 16 additions & 0 deletions tests/integration/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 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 Down
Loading