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, and `comment` wins when both are set. Loading namespace properties returns the description under both keys, and removing either key clears it.
```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
37 changes: 32 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,18 @@ 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 _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 +325,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 +995,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 @@ -1008,6 +1022,19 @@ def update_namespace_properties(
current_properties=current_properties, removals=removals, updates=updates
)

# "comment" and "Description" both name the Glue description: updating either sets it, removing either clears it.
description_keys = {GLUE_DESCRIPTION, GLUE_DESCRIPTION_LEGACY}
if description_keys & updates.keys():
description = _description_from_properties(updates)
elif description_keys & (removals or set()):
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
113 changes: 107 additions & 6 deletions tests/catalog/test_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -580,6 +582,60 @@ 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"


@mock_aws
def test_create_namespace_description_from_properties(_glue: boto3.client, moto_endpoint_url: str) -> None:
test_catalog = GlueCatalog("glue", client=None, **{"s3.endpoint": moto_endpoint_url})
cases = [
("comment_only", {"comment": "from comment"}, "from comment"),
("description_only", {"Description": "from Description"}, "from Description"),
("both", {"comment": "from comment", "Description": "from Description"}, "from comment"),
]
for database_name, properties, expected in cases:
test_catalog.create_namespace(database_name, properties)
database = _glue.get_database(Name=database_name)["Database"]
assert database["Description"] == expected, database_name
assert database["Parameters"] == {}, database_name
assert test_catalog.load_namespace_properties(database_name) == {"comment": expected, "Description": expected}


@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
def test_update_namespace_description(_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": "created"})
cases = [
({"comment": "updated comment"}, set(), "updated comment"),
({"Description": "updated Description"}, set(), "updated Description"),
({"key": "value"}, set(), "updated Description"),
({}, {"comment"}, None),
({"comment": "again"}, set(), "again"),
({}, {"Description"}, None),
]
for updates, removals, expected in cases:
test_catalog.update_namespace_properties(database_name, removals=removals, updates=updates)
database = _glue.get_database(Name=database_name)["Database"]
assert database.get("Description") == expected, (updates, removals)
assert "comment" not in database["Parameters"]
assert "Description" not in database["Parameters"]


@mock_aws
Expand Down Expand Up @@ -650,9 +706,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
Expand All @@ -663,7 +717,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",
Expand All @@ -684,6 +740,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)


Expand Down Expand Up @@ -722,7 +779,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
Expand Down Expand Up @@ -998,6 +1058,47 @@ def test_commit_table_properties(
assert table_info["Table"]["Parameters"]["test_c"] == "test_c"


@mock_aws
def test_create_table_description_from_properties(
_glue: boto3.client,
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"})
test_catalog.create_namespace(namespace=database_name)
cases = [
("comment_only", {"comment": "from comment"}, "from comment"),
("description_only", {"Description": "from Description"}, "from Description"),
("both", {"comment": "from comment", "Description": "from Description"}, "from comment"),
]
for table_name, properties, expected in cases:
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, table_name


@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
def test_commit_table_prunes_removed_properties(
_glue: boto3.client,
Expand Down
Loading