This is an automated email from the ASF dual-hosted git repository.

honahx pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git


The following commit(s) were added to refs/heads/main by this push:
     new 1b92851e Raise error on nonexistent namespace on sql.py (#865)
1b92851e is described below

commit 1b92851ed7bf7d765592937ea696d92bba3a1713
Author: edson duarte <[email protected]>
AuthorDate: Thu Jul 4 03:04:25 2024 -0300

    Raise error on nonexistent namespace on sql.py (#865)
---
 pyiceberg/catalog/sql.py  | 26 ++++++++++++++------------
 tests/catalog/test_sql.py | 12 ++++++++++++
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py
index d3cd8a94..039c7ddb 100644
--- a/pyiceberg/catalog/sql.py
+++ b/pyiceberg/catalog/sql.py
@@ -543,19 +543,21 @@ class SqlCatalog(MetastoreCatalog):
             NoSuchNamespaceError: If a namespace with the given name does not 
exist.
             NamespaceNotEmptyError: If the namespace is not empty.
         """
-        if self._namespace_exists(namespace):
-            namespace_str = Catalog.namespace_to_string(namespace)
-            if tables := self.list_tables(namespace):
-                raise NamespaceNotEmptyError(f"Namespace {namespace_str} is 
not empty. {len(tables)} tables exist.")
-
-            with Session(self.engine) as session:
-                session.execute(
-                    delete(IcebergNamespaceProperties).where(
-                        IcebergNamespaceProperties.catalog_name == self.name,
-                        IcebergNamespaceProperties.namespace == namespace_str,
-                    )
+        if not self._namespace_exists(namespace):
+            raise NoSuchNamespaceError(f"Namespace does not exist: 
{namespace}")
+
+        namespace_str = Catalog.namespace_to_string(namespace)
+        if tables := self.list_tables(namespace):
+            raise NamespaceNotEmptyError(f"Namespace {namespace_str} is not 
empty. {len(tables)} tables exist.")
+
+        with Session(self.engine) as session:
+            session.execute(
+                delete(IcebergNamespaceProperties).where(
+                    IcebergNamespaceProperties.catalog_name == self.name,
+                    IcebergNamespaceProperties.namespace == namespace_str,
                 )
-                session.commit()
+            )
+            session.commit()
 
     def list_tables(self, namespace: Union[str, Identifier]) -> 
List[Identifier]:
         """List tables under the given namespace in the catalog.
diff --git a/tests/catalog/test_sql.py b/tests/catalog/test_sql.py
index eae0c5ea..a32cd047 100644
--- a/tests/catalog/test_sql.py
+++ b/tests/catalog/test_sql.py
@@ -1093,6 +1093,18 @@ def test_drop_namespace(catalog: SqlCatalog, 
table_schema_nested: Schema, table_
     assert namespace not in catalog.list_namespaces()
 
 
[email protected](
+    "catalog",
+    [
+        lazy_fixture("catalog_memory"),
+        lazy_fixture("catalog_sqlite"),
+    ],
+)
+def test_drop_non_existing_namespaces(catalog: SqlCatalog) -> None:
+    with pytest.raises(NoSuchNamespaceError):
+        catalog.drop_namespace("does_not_exist")
+
+
 @pytest.mark.parametrize(
     "catalog",
     [

Reply via email to