diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md index 57d80d66e2..27c3219230 100644 --- a/mkdocs/docs/api.md +++ b/mkdocs/docs/api.md @@ -150,10 +150,11 @@ catalog.create_table( schema=schema, partition_spec=partition_spec, sort_order=sort_order, + properties={"comment": "Bid prices per symbol"}, ) ``` -When the table is created, all IDs in the schema are re-assigned to ensure uniqueness. +When the table is created, all IDs in the schema are re-assigned to ensure uniqueness. The `comment` property sets the table's description, which engines such as Spark and Trino read as the table comment, and the same property describes a namespace. To create a table using a pyarrow schema: diff --git a/mkdocs/docs/configuration.md b/mkdocs/docs/configuration.md index d72a4ead27..042a137629 100644 --- a/mkdocs/docs/configuration.md +++ b/mkdocs/docs/configuration.md @@ -874,6 +874,20 @@ catalog: + + +!!! note "Table and namespace descriptions" + Use the `comment` property to set the Glue description of a table or a namespace (Glue database), as in the Hive catalog. The `Description` property still works. On create, `comment` wins when both are set. Loading namespace properties returns the description under both keys. On a namespace update, the key that changed wins, changing both keys to different values raises an error, and removing either key clears the description. + ```python + catalog.create_namespace("sales", properties={"comment": "Sales data"}) + table = catalog.create_table("sales.orders", schema=schema, properties={"comment": "One row per order"}) + catalog.update_namespace_properties("sales", updates={"comment": "Sales and refunds"}) + with table.transaction() as tx: + tx.set_properties(comment="One row per order line") + ``` + + + ### DynamoDB Catalog If you want to use AWS DynamoDB as the catalog, you can use the last two ways to configure the pyiceberg and refer diff --git a/pyiceberg/catalog/glue.py b/pyiceberg/catalog/glue.py index 9ba8775747..3fe346420e 100644 --- a/pyiceberg/catalog/glue.py +++ b/pyiceberg/catalog/glue.py @@ -132,6 +132,10 @@ # This could be used to use GlueCatalog with any glue-compatible metastore service that has a different endpoint GLUE_CATALOG_ENDPOINT = "glue.endpoint" +# Table and namespace property used as the Glue description, as in Java's GlueCatalog. "Description" is still read. +GLUE_DESCRIPTION = "comment" +GLUE_DESCRIPTION_LEGACY = "Description" + ICEBERG_FIELD_ID = "iceberg.field.id" ICEBERG_FIELD_OPTIONAL = "iceberg.field.optional" ICEBERG_FIELD_CURRENT = "iceberg.field.current" @@ -282,12 +286,33 @@ def _construct_table_input( }, } - if "Description" in properties: - table_input["Description"] = properties["Description"] + if (description := _description_from_properties(properties)) is not None: + table_input["Description"] = description return table_input +def _description_from_properties(properties: Properties) -> str | None: + if GLUE_DESCRIPTION in properties: + return properties[GLUE_DESCRIPTION] + return properties.get(GLUE_DESCRIPTION_LEGACY) + + +def _description_from_updates(updates: Properties, current: str | None) -> str | None: + if GLUE_DESCRIPTION not in updates or GLUE_DESCRIPTION_LEGACY not in updates: + return _description_from_properties(updates) + comment, legacy = updates[GLUE_DESCRIPTION], updates[GLUE_DESCRIPTION_LEGACY] + # Both keys sent, e.g. loaded properties sent back after an edit: the key that changed wins, + # and changing both to different values is an error. + if comment == legacy or legacy == current: + return comment + if comment == current: + return legacy + raise ValueError( + f"Updates set {GLUE_DESCRIPTION} and {GLUE_DESCRIPTION_LEGACY} to different values: {comment!r} and {legacy!r}" + ) + + def _construct_rename_table_input(to_table_name: str, glue_table: "TableTypeDef") -> "TableInputTypeDef": rename_table_input: TableInputTypeDef = {"Name": to_table_name} # use the same Glue info to create the new table, pointing to the old metadata @@ -315,10 +340,12 @@ def _construct_rename_table_input(to_table_name: str, glue_table: "TableTypeDef" def _construct_database_input(database_name: str, properties: Properties) -> "DatabaseInputTypeDef": database_input: DatabaseInputTypeDef = {"Name": database_name} + if (description := _description_from_properties(properties)) is not None: + database_input["Description"] = description parameters = {} for k, v in properties.items(): - if k == "Description": - database_input["Description"] = v + if k in (GLUE_DESCRIPTION, GLUE_DESCRIPTION_LEGACY): + continue elif k == LOCATION: database_input["LocationUri"] = v else: @@ -983,8 +1010,10 @@ def load_namespace_properties(self, namespace: str | Identifier) -> Properties: properties = dict(database.get("Parameters", {})) if "LocationUri" in database: properties["location"] = database["LocationUri"] + # The Glue description takes precedence over a "comment" parameter written by older versions. if "Description" in database: - properties["Description"] = database["Description"] + properties[GLUE_DESCRIPTION] = database["Description"] + properties[GLUE_DESCRIPTION_LEGACY] = database["Description"] return properties @@ -1004,10 +1033,33 @@ def update_namespace_properties( ValueError: If removals and updates have overlapping keys. """ current_properties = self.load_namespace_properties(namespace=namespace) + + # "comment" and "Description" both name the Glue description: updating either sets it, removing either clears it. + description_keys = {GLUE_DESCRIPTION, GLUE_DESCRIPTION_LEGACY} + # Adjust removals so the summary matches what the next load returns + removals = set(removals or set()) + if description_keys & updates.keys(): + # e.g. removals={"comment"} with updates={"Description": "x"}: "comment" still loads as "x" + removals -= description_keys - updates.keys() + elif description_keys & removals: + # Removing either key clears the description, so both keys are removed + removals |= description_keys & current_properties.keys() + properties_update_summary, updated_properties = self._get_updated_props_and_update_summary( current_properties=current_properties, removals=removals, updates=updates ) + if description_keys & updates.keys(): + description = _description_from_updates(updates, _description_from_properties(current_properties)) + elif description_keys & removals: + description = None + else: + description = _description_from_properties(current_properties) + for key in description_keys: + updated_properties.pop(key, None) + if description is not None: + updated_properties[GLUE_DESCRIPTION] = description + database_name = self.identifier_to_database(namespace, NoSuchNamespaceError) self.glue.update_database(Name=database_name, DatabaseInput=_construct_database_input(database_name, updated_properties)) diff --git a/tests/catalog/integration_test_glue.py b/tests/catalog/integration_test_glue.py index d8bc7f95ff..1bf5823af6 100644 --- a/tests/catalog/integration_test_glue.py +++ b/tests/catalog/integration_test_glue.py @@ -299,7 +299,7 @@ def test_create_namespace_if_not_exists(test_catalog: Catalog, database_name: st assert (database_name,) in test_catalog.list_namespaces() -def test_create_namespace_with_comment_and_location(test_catalog: Catalog, database_name: str) -> None: +def test_create_namespace_with_comment_and_location(test_catalog: Catalog, glue: boto3.client, database_name: str) -> None: test_location = get_s3_path(get_bucket_name(), database_name) test_properties = { "comment": "this is a test description", @@ -311,6 +311,7 @@ def test_create_namespace_with_comment_and_location(test_catalog: Catalog, datab properties = test_catalog.load_namespace_properties(database_name) assert properties["comment"] == "this is a test description" assert properties["location"] == test_location + assert glue.get_database(Name=database_name)["Database"]["Description"] == "this is a test description" def test_list_namespaces(test_catalog: Catalog, database_list: list[str]) -> None: @@ -345,9 +346,7 @@ def test_load_namespace_properties(test_catalog: Catalog, database_name: str) -> test_catalog.create_namespace(database_name, test_properties) listed_properties = test_catalog.load_namespace_properties(database_name) - for k, v in listed_properties.items(): - assert k in test_properties - assert v == test_properties[k] + assert listed_properties == {**test_properties, "Description": "this is a test description"} def test_load_empty_namespace_properties(test_catalog: Catalog, database_name: str) -> None: @@ -363,7 +362,7 @@ def test_load_default_namespace_properties(test_catalog: Catalog, glue: boto3.cl assert listed_properties == {} -def test_update_namespace_properties(test_catalog: Catalog, database_name: str) -> None: +def test_update_namespace_properties(test_catalog: Catalog, glue: boto3.client, database_name: str) -> None: warehouse_location = get_s3_path(get_bucket_name()) test_properties = { "comment": "this is a test description", @@ -384,6 +383,7 @@ def test_update_namespace_properties(test_catalog: Catalog, database_name: str) else: assert k in update_report.removed assert "updated test description" == test_catalog.load_namespace_properties(database_name)["comment"] + assert glue.get_database(Name=database_name)["Database"]["Description"] == "updated test description" def test_commit_table_update_schema( diff --git a/tests/catalog/test_glue.py b/tests/catalog/test_glue.py index 5f1ecab2c7..9156a20b08 100644 --- a/tests/catalog/test_glue.py +++ b/tests/catalog/test_glue.py @@ -566,7 +566,9 @@ def test_create_namespace_no_properties(_bucket_initialize: None, moto_endpoint_ @mock_aws -def test_create_namespace_with_comment_and_location(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None: +def test_create_namespace_with_comment_and_location( + _glue: boto3.client, _bucket_initialize: None, moto_endpoint_url: str, database_name: str +) -> None: test_location = f"s3://{BUCKET_NAME}/{database_name}.db" test_properties = { "comment": "this is a test description", @@ -580,6 +582,152 @@ def test_create_namespace_with_comment_and_location(_bucket_initialize: None, mo properties = test_catalog.load_namespace_properties(database_name) assert properties["comment"] == "this is a test description" assert properties["location"] == test_location + assert _glue.get_database(Name=database_name)["Database"]["Description"] == "this is a test description" + + +def _assert_namespace_description(glue: boto3.client, catalog: GlueCatalog, database_name: str, expected: str | None) -> None: + database = glue.get_database(Name=database_name)["Database"] + assert database.get("Description") == expected + assert "comment" not in database["Parameters"] + assert "Description" not in database["Parameters"] + properties = catalog.load_namespace_properties(database_name) + assert properties.get("comment") == expected + assert properties.get("Description") == expected + + +@mock_aws +@pytest.mark.parametrize( + "properties, expected", + [ + ({"comment": "from comment"}, "from comment"), + ({"Description": "from Description"}, "from Description"), + ({"comment": "from comment", "Description": "from Description"}, "from comment"), + ], +) +def test_create_namespace_description_from_properties( + _glue: boto3.client, moto_endpoint_url: str, database_name: str, properties: Properties, expected: str +) -> None: + test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url}) + test_catalog.create_namespace(database_name, properties) + _assert_namespace_description(_glue, test_catalog, database_name, expected) + assert _glue.get_database(Name=database_name)["Database"]["Parameters"] == {} + + +@mock_aws +def test_load_namespace_properties_with_legacy_comment_parameter(_glue: boto3.client, moto_endpoint_url: str) -> None: + # Older versions stored "comment" as a Glue database parameter + _glue.create_database(DatabaseInput={"Name": "only_parameter", "Parameters": {"comment": "parameter"}}) + _glue.create_database(DatabaseInput={"Name": "both", "Description": "description", "Parameters": {"comment": "parameter"}}) + test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url}) + + assert test_catalog.load_namespace_properties("only_parameter") == {"comment": "parameter"} + assert test_catalog.load_namespace_properties("both") == {"comment": "description", "Description": "description"} + + # Any update moves the description out of the parameters + test_catalog.update_namespace_properties("only_parameter", updates={"key": "value"}) + database = _glue.get_database(Name="only_parameter")["Database"] + assert database["Description"] == "parameter" + assert database["Parameters"] == {"key": "value"} + + +@mock_aws +@pytest.mark.parametrize( + "updates, removals, expected", + [ + ({"key": "value"}, set(), "current"), + ({"comment": "new"}, set(), "new"), + ({"Description": "new"}, set(), "new"), + # Both keys set: the one that changed wins + ({"comment": "new", "Description": "new"}, set(), "new"), + ({"comment": "new", "Description": "current"}, set(), "new"), + ({"comment": "current", "Description": "new"}, set(), "new"), + ({}, {"comment"}, None), + ({}, {"Description"}, None), + ], +) +def test_update_namespace_description( + _glue: boto3.client, + moto_endpoint_url: str, + database_name: str, + updates: Properties, + removals: set[str], + expected: str | None, +) -> None: + test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url}) + test_catalog.create_namespace(database_name, {"comment": "current"}) + test_catalog.update_namespace_properties(database_name, removals=removals, updates=updates) + _assert_namespace_description(_glue, test_catalog, database_name, expected) + + +@mock_aws +@pytest.mark.parametrize( + "edit, expected", + [ + ({"key": "value"}, "current"), + ({"comment": "new"}, "new"), + ({"Description": "new"}, "new"), + ], +) +def test_update_namespace_description_after_load( + _glue: boto3.client, moto_endpoint_url: str, database_name: str, edit: Properties, expected: str +) -> None: + test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url}) + test_catalog.create_namespace(database_name, {"comment": "current"}) + # Send the loaded properties back with one key edited + properties = {**test_catalog.load_namespace_properties(database_name), **edit} + test_catalog.update_namespace_properties(database_name, updates=properties) + _assert_namespace_description(_glue, test_catalog, database_name, expected) + + +@mock_aws +@pytest.mark.parametrize( + "properties, removals, updates, removed, updated, missing, expected", + [ + # Removing one key removes both + ({"comment": "current"}, {"comment"}, {}, {"comment", "Description"}, set(), set(), None), + ({"comment": "current"}, {"Description"}, {}, {"comment", "Description"}, set(), set(), None), + # The update sets the description, so its alias isn't removed + ({"comment": "current"}, {"comment"}, {"Description": "x"}, set(), {"Description"}, set(), "x"), + ({}, {"comment"}, {}, set(), set(), {"comment"}, None), + ], +) +def test_update_namespace_description_summary( + _glue: boto3.client, + moto_endpoint_url: str, + database_name: str, + properties: Properties, + removals: set[str], + updates: Properties, + removed: set[str], + updated: set[str], + missing: set[str], + expected: str | None, +) -> None: + test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url}) + test_catalog.create_namespace(database_name, properties) + summary = test_catalog.update_namespace_properties(database_name, removals=removals, updates=updates) + assert set(summary.removed) == removed + assert set(summary.updated) == updated + assert set(summary.missing) == missing + _assert_namespace_description(_glue, test_catalog, database_name, expected) + + +@mock_aws +def test_update_namespace_description_overlap(_glue: boto3.client, moto_endpoint_url: str, database_name: str) -> None: + test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url}) + test_catalog.create_namespace(database_name, {"comment": "current"}) + with pytest.raises(ValueError, match="Updates and deletes have an overlap"): + test_catalog.update_namespace_properties(database_name, removals={"comment"}, updates={"comment": "x"}) + _assert_namespace_description(_glue, test_catalog, database_name, "current") + + +@mock_aws +def test_update_namespace_description_conflict(_glue: boto3.client, moto_endpoint_url: str, database_name: str) -> None: + test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url}) + test_catalog.create_namespace(database_name, {"comment": "current"}) + with pytest.raises(ValueError, match="different values"): + test_catalog.update_namespace_properties(database_name, updates={"comment": "one", "Description": "two"}) + _assert_namespace_description(_glue, test_catalog, database_name, "current") @mock_aws @@ -650,9 +798,7 @@ def test_load_namespace_properties(_bucket_initialize: None, moto_endpoint_url: test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url}) test_catalog.create_namespace(database_name, test_properties) listed_properties = test_catalog.load_namespace_properties(database_name) - for k, v in listed_properties.items(): - assert k in test_properties - assert v == test_properties[k] + assert listed_properties == {**test_properties, "Description": "this is a test description"} @mock_aws @@ -663,7 +809,9 @@ def test_load_non_exist_namespace_properties(_bucket_initialize: None, moto_endp @mock_aws -def test_update_namespace_properties(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None: +def test_update_namespace_properties( + _glue: boto3.client, _bucket_initialize: None, moto_endpoint_url: str, database_name: str +) -> None: test_properties = { "comment": "this is a test description", "location": f"s3://{BUCKET_NAME}/{database_name}.db", @@ -684,6 +832,7 @@ def test_update_namespace_properties(_bucket_initialize: None, moto_endpoint_url else: assert k in update_report.removed assert "updated test description" == test_catalog.load_namespace_properties(database_name)["comment"] + assert _glue.get_database(Name=database_name)["Database"]["Description"] == "updated test description" test_catalog.drop_namespace(database_name) @@ -722,7 +871,10 @@ def test_update_namespace_properties_overlap_update_removal( with pytest.raises(ValueError): test_catalog.update_namespace_properties(database_name, removals, updates) # should not modify the properties - assert test_catalog.load_namespace_properties(database_name) == test_properties + assert test_catalog.load_namespace_properties(database_name) == { + **test_properties, + "Description": "this is a test description", + } @mock_aws @@ -998,6 +1150,74 @@ def test_commit_table_properties( assert table_info["Table"]["Parameters"]["test_c"] == "test_c" +@mock_aws +@pytest.mark.parametrize( + "properties, expected", + [ + ({"comment": "from comment"}, "from comment"), + ({"Description": "from Description"}, "from Description"), + ({"comment": "from comment", "Description": "from Description"}, "from comment"), + ], +) +def test_create_table_description_from_properties( + _glue: boto3.client, + _bucket_initialize: None, + moto_endpoint_url: str, + table_schema_nested: Schema, + database_name: str, + table_name: str, + properties: Properties, + expected: str, +) -> None: + test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"}) + test_catalog.create_namespace(namespace=database_name) + test_catalog.create_table(identifier=(database_name, table_name), schema=table_schema_nested, properties=properties) + table_info = _glue.get_table(DatabaseName=database_name, Name=table_name) + assert table_info["Table"]["Description"] == expected + + +@mock_aws +def test_commit_table_comment_updates_description( + _glue: boto3.client, + _bucket_initialize: None, + moto_endpoint_url: str, + table_schema_nested: Schema, + database_name: str, + table_name: str, +) -> None: + identifier = (database_name, table_name) + test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"}) + test_catalog.create_namespace(namespace=database_name) + table = test_catalog.create_table(identifier=identifier, schema=table_schema_nested, properties={"comment": "before"}) + + table.transaction().set_properties(comment="after").commit_transaction() + + table_info = _glue.get_table(DatabaseName=database_name, Name=table_name) + assert table_info["Table"]["Description"] == "after" + + +@mock_aws +@pytest.mark.parametrize("key", ["comment", "Description"]) +def test_commit_table_removed_description_clears_glue_description( + _glue: boto3.client, + _bucket_initialize: None, + moto_endpoint_url: str, + table_schema_nested: Schema, + database_name: str, + table_name: str, + key: str, +) -> None: + identifier = (database_name, table_name) + test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"}) + test_catalog.create_namespace(namespace=database_name) + table = test_catalog.create_table(identifier=identifier, schema=table_schema_nested, properties={key: "before"}) + assert _glue.get_table(DatabaseName=database_name, Name=table_name)["Table"]["Description"] == "before" + + table.transaction().remove_properties(key).commit_transaction() + + assert "Description" not in _glue.get_table(DatabaseName=database_name, Name=table_name)["Table"] + + @mock_aws def test_commit_table_prunes_removed_properties( _glue: boto3.client,