Skip to content
Open
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
50 changes: 19 additions & 31 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
String,
create_engine,
delete,
func,
insert,
select,
text,
Expand Down Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions tests/catalog/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading