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
14 changes: 12 additions & 2 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
String,
create_engine,
delete,
func,
insert,
select,
text,
Expand Down Expand Up @@ -576,13 +577,19 @@ 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)
.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)
),
)
.limit(1)
)
Expand All @@ -594,7 +601,10 @@ def namespace_exists(self, identifier: str | Identifier) -> bool:
.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)
),
)
.limit(1)
)
Expand Down
15 changes: 15 additions & 0 deletions tests/catalog/test_catalog_behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,21 @@ 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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add the repro from #4163? With only sales.eu, create_table("SALES.orders") should raise NoSuchNamespaceError and create_namespace("SALES") should succeed. Note that all three catalog fixtures (memory, sql, sql_without_rowcount) are SQLite-backed, so this doesn't cover a case-insensitive backend.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 2e7b100 as test_parent_namespace_match_is_case_sensitive: with only sales.eu, create_table("SALES.orders") raises NoSuchNamespaceError and create_namespace("SALES") succeeds. It fails on main for all three fixtures. As you say, they're all SQLite, so nothing here exercises a case-insensitive collation.



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
Expand Down
Loading