Fokko commented on code in PR #4164:
URL: https://github.com/apache/iceberg-python/pull/4164#discussion_r4742760268
##########
pyiceberg/catalog/sql.py:
##########
@@ -578,31 +579,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
Review Comment:
This deviates from Java: `JdbcUtil.namespaceExists` runs the same `= ? OR
LIKE ? ESCAPE '!'` query and returns on the first row without any
post-filtering, so Java's `JdbcCatalog` has the same behavior on SQLite. Fixing
it here is reasonable, but could you call out the deviation in the PR
description and open a matching issue on apache/iceberg so both implementations
stay aligned?
Also, this check is stricter than the database's own `=`. On MySQL/MariaDB
(and MSSQL) with their default case-insensitive collations, `table_namespace =
'SALES'` matches `sales`, and so do the exact lookups in `load_table`,
`list_tables`, `drop_namespace`, etc. With this change
`namespace_exists("SALES")` returns False there while the rest of the catalog
still treats it as the same namespace, e.g. `create_namespace("SALES")` passes
the existence check and then fails on the primary key.
##########
tests/catalog/test_catalog_behaviors.py:
##########
@@ -1077,6 +1077,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")
Review Comment:
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.
##########
pyiceberg/catalog/sql.py:
##########
@@ -578,31 +579,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()
Review Comment:
Java keeps `LIMIT 1` here (`GET_NAMESPACE_SQL`). Without it, the database
has to compute the `DISTINCT` over the whole namespace subtree before `any()`
can short-circuit, and `namespace_exists` is on the path of almost every
catalog call. What about doing the prefix check in SQL so `LIMIT 1` can stay?
For example, keep the `LIKE` (index-friendly) and add:
```python
func.substr(IcebergTables.table_namespace, 1, len(namespace) + 1) ==
f"{namespace}."
```
This uses the same comparison semantics as the existing `==` lookups:
case-sensitive on SQLite/PostgreSQL, and consistent with the rest of the
catalog on case-insensitive collations. It would also make
`_contains_namespace` unnecessary.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]