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
3 changes: 2 additions & 1 deletion mkdocs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
14 changes: 14 additions & 0 deletions mkdocs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,20 @@ catalog:

<!-- prettier-ignore-end -->

<!-- prettier-ignore-start -->

!!! 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")
```

<!-- prettier-ignore-end -->

### 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
Expand Down
62 changes: 57 additions & 5 deletions pyiceberg/catalog/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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))

Expand Down
10 changes: 5 additions & 5 deletions tests/catalog/integration_test_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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",
Expand All @@ -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(
Expand Down
Loading
Loading