From 836bd2fabeada68a769d48ab369e4056f3210922 Mon Sep 17 00:00:00 2001 From: Gedalia Kliger <117381134+gedaliakliger@users.noreply.github.com> Date: Sat, 21 Nov 2026 08:30:39 +0000 Subject: [PATCH] Reject dropping a SqlCatalog namespace that has child namespaces drop_namespace only checked for tables, so dropping a parent namespace deleted its properties and reported success while its children kept it listed. Java's JdbcCatalog.dropNamespace throws NamespaceNotEmptyException in this case. --- pyiceberg/catalog/sql.py | 3 +++ tests/catalog/test_catalog_behaviors.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py index 15e32303be..a72266ace4 100644 --- a/pyiceberg/catalog/sql.py +++ b/pyiceberg/catalog/sql.py @@ -651,6 +651,9 @@ def drop_namespace(self, namespace: str | Identifier) -> None: raise NoSuchNamespaceError(f"Namespace does not exist: {namespace}") namespace_str = Catalog.namespace_to_string(namespace) + if child_namespaces := self.list_namespaces(namespace_str): + raise NamespaceNotEmptyError(f"Namespace {namespace_str} is not empty. {len(child_namespaces)} namespaces exist.") + if tables := self.list_tables(namespace): raise NamespaceNotEmptyError(f"Namespace {namespace_str} is not empty. {len(tables)} tables exist.") diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index e4f834b016..ad53ebc82e 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -1409,6 +1409,19 @@ def test_drop_namespace_raises_error_when_namespace_not_empty( catalog.drop_namespace(namespace) +def test_drop_namespace_raises_error_when_namespace_has_child_namespaces(catalog: Catalog, test_namespace: Identifier) -> None: + child_namespace = test_namespace + ("child",) + catalog.create_namespace(test_namespace) + catalog.create_namespace(child_namespace) + with pytest.raises(NamespaceNotEmptyError, match=f"Namespace {'.'.join(test_namespace)} is not empty. 1 namespaces exist."): + catalog.drop_namespace(test_namespace) + assert catalog.namespace_exists(test_namespace) + assert catalog.namespace_exists(child_namespace) + catalog.drop_namespace(child_namespace) + catalog.drop_namespace(test_namespace) + assert not catalog.namespace_exists(test_namespace) + + # RecordBatchReader streaming append/overwrite tests # # Streaming writes accept a pa.RecordBatchReader and microbatch it into target-sized