From 9e15ea0baad14695ef8222fe10bb70274944d75e Mon Sep 17 00:00:00 2001 From: Dhruv Gupta Date: Tue, 27 Oct 2026 06:30:38 -0400 Subject: [PATCH 1/2] Fix case-insensitive parent namespace match in SqlCatalog.namespace_exists The parent-namespace check uses LIKE, which SQLite evaluates case-insensitively for ASCII. With only `sales.eu` present, `namespace_exists("SALES")` returned True, so `create_table("SALES.t")` succeeded in a namespace that does not exist and `create_namespace("SALES")` failed with NamespaceAlreadyExistsError. Re-check the LIKE matches with a case-sensitive prefix comparison so the result matches the exact-equality lookups used elsewhere in the catalog. --- pyiceberg/catalog/sql.py | 20 ++++++++++++-------- tests/catalog/test_catalog_behaviors.py | 3 +++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py index 94c7965a88..9526771a7b 100644 --- a/pyiceberg/catalog/sql.py +++ b/pyiceberg/catalog/sql.py @@ -18,6 +18,7 @@ from __future__ import annotations import logging +from collections.abc import Iterable from typing import ( TYPE_CHECKING, Any, @@ -573,31 +574,34 @@ def namespace_exists(self, identifier: str | Identifier) -> bool: with Session(self.engine) as session: stmt = ( - select(IcebergTables) + select(IcebergTables.table_namespace) .where( IcebergTables.catalog_name == self.name, (IcebergTables.table_namespace == namespace) | (IcebergTables.table_namespace.like(namespace_starts_with, escape="!")), ) - .limit(1) + .distinct() ) - result = session.execute(stmt).all() - if result: + if self._contains_namespace(session.scalars(stmt), namespace): return True stmt = ( - select(IcebergNamespaceProperties) + select(IcebergNamespaceProperties.namespace) .where( IcebergNamespaceProperties.catalog_name == self.name, (IcebergNamespaceProperties.namespace == namespace) | (IcebergNamespaceProperties.namespace.like(namespace_starts_with, escape="!")), ) - .limit(1) + .distinct() ) - result = session.execute(stmt).all() - if result: + if self._contains_namespace(session.scalars(stmt), namespace): return True return False + @staticmethod + def _contains_namespace(candidates: Iterable[str], namespace: str) -> bool: + # LIKE is case-insensitive on some backends (e.g. SQLite), so re-check the prefix match + return any(candidate == namespace or candidate.startswith(f"{namespace}.") for candidate in candidates) + @override def create_namespace(self, namespace: str | Identifier, properties: Properties = EMPTY_DICT) -> None: """Create a namespace in the catalog. diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index 1148ca8527..5a7bf67bb0 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -1046,6 +1046,9 @@ def test_namespace_exists(catalog: Catalog) -> None: assert not catalog.namespace_exists("db_") # make sure '%' is escaped in the query assert not catalog.namespace_exists("db%") + # namespaces are case-sensitive, also when matching a parent namespace + assert not catalog.namespace_exists("DB2") + assert not catalog.namespace_exists("db3.NS1") # Namespace properties From 2e7b10047263d4747e665231cf640443885889f8 Mon Sep 17 00:00:00 2001 From: Dhruv Gupta Date: Mon, 30 Nov 2026 14:19:49 -0500 Subject: [PATCH 2/2] Compare the parent namespace prefix in SQL and keep LIMIT 1 namespace_exists filtered the LIKE matches in Python, which needed a DISTINCT over the whole namespace subtree and compared names case-sensitively even on backends whose collation makes "=" case- insensitive. Compare the prefix with substr() = '.' in the query instead, next to the index-friendly LIKE, so the lookup stops at the first row again and follows the same rules as the exact namespace lookups elsewhere in the catalog. Add the reproduction from #4163: with only sales.eu, creating a table in SALES raises NoSuchNamespaceError and creating the SALES namespace works. --- pyiceberg/catalog/sql.py | 34 +++++++++++++++---------- tests/catalog/test_catalog_behaviors.py | 12 +++++++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py index 4044787313..97a7db644d 100644 --- a/pyiceberg/catalog/sql.py +++ b/pyiceberg/catalog/sql.py @@ -18,7 +18,6 @@ from __future__ import annotations import logging -from collections.abc import Iterable from typing import ( TYPE_CHECKING, Any, @@ -30,6 +29,7 @@ String, create_engine, delete, + func, insert, select, text, @@ -577,36 +577,42 @@ def namespace_exists(self, identifier: str | Identifier) -> bool: namespace = Catalog.namespace_to_string(namespace_tuple, NoSuchNamespaceError) namespace_starts_with = namespace.replace("!", "!!").replace("_", "!_").replace("%", "!%") + ".%" + # LIKE keeps the lookup index-friendly but is case-insensitive on some backends (e.g. SQLite), + # so the prefix is also compared with "=", which follows the same rules as the exact lookups + prefix = f"{namespace}." with Session(self.engine) as session: stmt = ( - select(IcebergTables.table_namespace) + select(IcebergTables) .where( IcebergTables.catalog_name == self.name, (IcebergTables.table_namespace == namespace) - | (IcebergTables.table_namespace.like(namespace_starts_with, escape="!")), + | ( + IcebergTables.table_namespace.like(namespace_starts_with, escape="!") + & (func.substr(IcebergTables.table_namespace, 1, len(prefix)) == prefix) + ), ) - .distinct() + .limit(1) ) - if self._contains_namespace(session.scalars(stmt), namespace): + result = session.execute(stmt).all() + if result: return True stmt = ( - select(IcebergNamespaceProperties.namespace) + select(IcebergNamespaceProperties) .where( IcebergNamespaceProperties.catalog_name == self.name, (IcebergNamespaceProperties.namespace == namespace) - | (IcebergNamespaceProperties.namespace.like(namespace_starts_with, escape="!")), + | ( + IcebergNamespaceProperties.namespace.like(namespace_starts_with, escape="!") + & (func.substr(IcebergNamespaceProperties.namespace, 1, len(prefix)) == prefix) + ), ) - .distinct() + .limit(1) ) - if self._contains_namespace(session.scalars(stmt), namespace): + result = session.execute(stmt).all() + if result: return True return False - @staticmethod - def _contains_namespace(candidates: Iterable[str], namespace: str) -> bool: - # LIKE is case-insensitive on some backends (e.g. SQLite), so re-check the prefix match - return any(candidate == namespace or candidate.startswith(f"{namespace}.") for candidate in candidates) - @override def create_namespace(self, namespace: str | Identifier, properties: Properties = EMPTY_DICT) -> None: """Create a namespace in the catalog. diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index 66782e3de3..6e4551fc3a 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -1082,6 +1082,18 @@ def test_namespace_exists(catalog: Catalog) -> None: assert not catalog.namespace_exists("db3.NS1") +def test_parent_namespace_match_is_case_sensitive(catalog: Catalog, table_schema_simple: Schema) -> None: + # https://github.com/apache/iceberg-python/issues/4163: with only `sales.eu`, `SALES` must not be treated as existing + catalog.create_namespace(("sales", "eu")) + + with pytest.raises(NoSuchNamespaceError): + catalog.create_table(("SALES", "orders"), table_schema_simple) + + catalog.create_namespace("SALES") + assert catalog.namespace_exists("SALES") + assert catalog.namespace_exists("sales") + + # Namespace properties