diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py index a89caff2ed..e140cf7524 100644 --- a/pyiceberg/catalog/sql.py +++ b/pyiceberg/catalog/sql.py @@ -29,7 +29,6 @@ String, create_engine, delete, - func, insert, select, text, @@ -572,44 +571,33 @@ def commit_table( ) @override - def namespace_exists(self, identifier: str | Identifier) -> bool: + def namespace_exists(self, identifier: str | Identifier) -> bool: namespace_tuple = Catalog.identifier_to_tuple(identifier) 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}." + + # The LIKE filter keeps the lookup index-friendly, but LIKE is + # case-insensitive on some backends (SQLite) and follows the column + # collation on others (MySQL _ci), so it cannot decide the match on + # its own. The exact, case-sensitive prefix check therefore happens + # in Python on the returned rows, like list_namespaces already does. + # This also avoids the SQL substr function, which SQL Server does + # not support. with Session(self.engine) as session: - stmt = ( - select(IcebergTables) - .where( - IcebergTables.catalog_name == self.name, - (IcebergTables.table_namespace == namespace) - | ( - IcebergTables.table_namespace.like(namespace_starts_with, escape="!") - & (func.substr(IcebergTables.table_namespace, 1, len(prefix)) == prefix) - ), + tables_stmt = select(IcebergTables.table_namespace).where( + IcebergTables.catalog_name == self.name, + (IcebergTables.table_namespace == namespace) + | (IcebergTables.table_namespace.like(namespace_starts_with, escape="!")), ) - .limit(1) - ) - result = session.execute(stmt).all() - if result: + if any(ns == namespace or ns.startswith(prefix) for ns in session.scalars(tables_stmt)): return True - stmt = ( - select(IcebergNamespaceProperties) - .where( - IcebergNamespaceProperties.catalog_name == self.name, - (IcebergNamespaceProperties.namespace == namespace) - | ( - IcebergNamespaceProperties.namespace.like(namespace_starts_with, escape="!") - & (func.substr(IcebergNamespaceProperties.namespace, 1, len(prefix)) == prefix) - ), + properties_stmt = select(IcebergNamespaceProperties.namespace).where( + IcebergNamespaceProperties.catalog_name == self.name, + (IcebergNamespaceProperties.namespace == namespace) + | (IcebergNamespaceProperties.namespace.like(namespace_starts_with, escape="!")), ) - .limit(1) - ) - result = session.execute(stmt).all() - if result: + if any(ns == namespace or ns.startswith(prefix) for ns in session.scalars(properties_stmt)): return True return False diff --git a/tests/catalog/test_sql.py b/tests/catalog/test_sql.py index 3d40cad22b..2f6a8e0da9 100644 --- a/tests/catalog/test_sql.py +++ b/tests/catalog/test_sql.py @@ -546,6 +546,24 @@ def test_namespace_exists_on_v0_schema(warehouse: Path) -> None: assert not catalog.namespace_exists("missing") +def test_namespace_exists_is_case_sensitive(warehouse: Path) -> None: + """namespace_exists must not match a namespace that differs only in case.""" + catalog = SqlCatalog( + "test", + uri=f"sqlite:///{warehouse.as_posix()}/test-ns-exists-case", + warehouse=f"file://{warehouse}", + ) + catalog.create_tables() + catalog.create_namespace("sales.eu") + assert catalog.namespace_exists("sales") + assert catalog.namespace_exists("sales.eu") + assert not catalog.namespace_exists("SALES") + assert not catalog.namespace_exists("SALES.eu") + catalog.create_namespace("SALES") + assert catalog.namespace_exists("SALES") + catalog.destroy_tables() + + def test_create_and_load_table_on_v0_schema(warehouse: Path) -> None: """create_table and load_table should work on V0 schema without iceberg_type column.""" catalog = _make_v0_catalog(f"sqlite:///{warehouse.as_posix()}/test-v0-create", warehouse)