Skip to content

Commit 47613d3

Browse files
authored
fix: glue drop_namespace to check non-iceberg tables (apache#2083)
# Rationale for this change This PR is adding a check for all tables in the GlueCatalog instead of just iceberg tables. GlueCatalog allows users to store other table formats under the same database. Using the catalog's `list_tables()` call filters out non-Iceberg table types, which avoids the proper Iceberg `NamespaceNotEmptyError` and instead returns a generic Glue SDK error. This change aligns with the behavior in the java implementation: - https://github.com/apache/iceberg/blob/main/aws/src/main/java/org/apache/iceberg/aws/glue/GlueCatalog.java#L538-L571 # Are these changes tested? Tested locally with Glue Catalog # Are there any user-facing changes? Yes new exception message when a `drop_namespace()` call is made against a database with non-iceberg tables.
1 parent 1fe9697 commit 47613d3

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

pyiceberg/catalog/glue.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,19 @@ def drop_namespace(self, namespace: Union[str, Identifier]) -> None:
687687
"""
688688
database_name = self.identifier_to_database(namespace, NoSuchNamespaceError)
689689
try:
690-
table_list = self.list_tables(namespace=database_name)
691-
except NoSuchNamespaceError as e:
690+
table_list_response = self.glue.get_tables(DatabaseName=database_name)
691+
table_list = table_list_response["TableList"]
692+
except self.glue.exceptions.EntityNotFoundException as e:
692693
raise NoSuchNamespaceError(f"Database does not exist: {database_name}") from e
693694

694695
if len(table_list) > 0:
695-
raise NamespaceNotEmptyError(f"Database {database_name} is not empty")
696-
696+
first_table = table_list[0]
697+
if self.__is_iceberg_table(first_table):
698+
raise NamespaceNotEmptyError(f"Cannot drop namespace {database_name} because it still contains Iceberg tables")
699+
else:
700+
raise NamespaceNotEmptyError(
701+
f"Cannot drop namespace {database_name} because it still contains non-Iceberg tables"
702+
)
697703
self.glue.delete_database(Name=database_name)
698704

699705
def list_tables(self, namespace: Union[str, Identifier]) -> List[Identifier]:

tests/catalog/test_glue.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,18 @@ def test_drop_non_empty_namespace(
548548
test_catalog.drop_namespace(database_name)
549549

550550

551+
@mock_aws
552+
def test_drop_namespace_that_contains_non_iceberg_tables(
553+
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
554+
) -> None:
555+
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
556+
test_catalog.create_namespace(namespace=database_name)
557+
test_catalog.glue.create_table(DatabaseName=database_name, TableInput={"Name": "hive_table"})
558+
559+
with pytest.raises(NamespaceNotEmptyError):
560+
test_catalog.drop_namespace(database_name)
561+
562+
551563
@mock_aws
552564
def test_drop_non_exist_namespace(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
553565
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})

0 commit comments

Comments
 (0)