Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error on nonexistent namespace on sql.py #865

Merged
merged 3 commits into from
Jul 4, 2024
Merged
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
26 changes: 14 additions & 12 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,19 +543,21 @@ def drop_namespace(self, namespace: Union[str, Identifier]) -> None:
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.
Expand Down
12 changes: 12 additions & 0 deletions tests/catalog/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,18 @@ def test_drop_namespace(catalog: SqlCatalog, table_schema_nested: Schema, table_
assert namespace not in catalog.list_namespaces()


@pytest.mark.parametrize(
"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",
[
Expand Down