Skip to content

Commit 1b92851

Browse files
authored
Raise error on nonexistent namespace on sql.py (#865)
1 parent 473d28d commit 1b92851

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

pyiceberg/catalog/sql.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -543,19 +543,21 @@ def drop_namespace(self, namespace: Union[str, Identifier]) -> None:
543543
NoSuchNamespaceError: If a namespace with the given name does not exist.
544544
NamespaceNotEmptyError: If the namespace is not empty.
545545
"""
546-
if self._namespace_exists(namespace):
547-
namespace_str = Catalog.namespace_to_string(namespace)
548-
if tables := self.list_tables(namespace):
549-
raise NamespaceNotEmptyError(f"Namespace {namespace_str} is not empty. {len(tables)} tables exist.")
550-
551-
with Session(self.engine) as session:
552-
session.execute(
553-
delete(IcebergNamespaceProperties).where(
554-
IcebergNamespaceProperties.catalog_name == self.name,
555-
IcebergNamespaceProperties.namespace == namespace_str,
556-
)
546+
if not self._namespace_exists(namespace):
547+
raise NoSuchNamespaceError(f"Namespace does not exist: {namespace}")
548+
549+
namespace_str = Catalog.namespace_to_string(namespace)
550+
if tables := self.list_tables(namespace):
551+
raise NamespaceNotEmptyError(f"Namespace {namespace_str} is not empty. {len(tables)} tables exist.")
552+
553+
with Session(self.engine) as session:
554+
session.execute(
555+
delete(IcebergNamespaceProperties).where(
556+
IcebergNamespaceProperties.catalog_name == self.name,
557+
IcebergNamespaceProperties.namespace == namespace_str,
557558
)
558-
session.commit()
559+
)
560+
session.commit()
559561

560562
def list_tables(self, namespace: Union[str, Identifier]) -> List[Identifier]:
561563
"""List tables under the given namespace in the catalog.

tests/catalog/test_sql.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,18 @@ def test_drop_namespace(catalog: SqlCatalog, table_schema_nested: Schema, table_
10931093
assert namespace not in catalog.list_namespaces()
10941094

10951095

1096+
@pytest.mark.parametrize(
1097+
"catalog",
1098+
[
1099+
lazy_fixture("catalog_memory"),
1100+
lazy_fixture("catalog_sqlite"),
1101+
],
1102+
)
1103+
def test_drop_non_existing_namespaces(catalog: SqlCatalog) -> None:
1104+
with pytest.raises(NoSuchNamespaceError):
1105+
catalog.drop_namespace("does_not_exist")
1106+
1107+
10961108
@pytest.mark.parametrize(
10971109
"catalog",
10981110
[

0 commit comments

Comments
 (0)