Repository navigation
Conversation
…xists
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.
…xists-case-sensitive
|
@Fokko @kevinjqliu, this one is ready for a review. |
…sts-case-sensitive
rambleraptor
left a comment
There was a problem hiding this comment.
This seems reasonable. I'm a little concerned about performance on the .distinct() calls, but some of this seems unavoidable.
|
Hi @Fokko and @kevinjqliu, a follow-up on the fix for #4163. On SQLite, |
Fokko
left a comment
There was a problem hiding this comment.
Thanks for the fix! I compared this against the Java JdbcCatalog, a few notes inline.
|
|
||
| @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 |
There was a problem hiding this 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.
There was a problem hiding this comment.
Good point on the collations. With the comparison in SQL, namespace_exists follows the same = semantics as load_table, create_namespace and the rest, so on MySQL or MariaDB namespace_exists("SALES") agrees with them again. The PR description now has a section on the difference from JdbcUtil.namespaceExists, and apache/iceberg#19219 tracks the same change for the Java catalog.
| | (IcebergTables.table_namespace.like(namespace_starts_with, escape="!")), | ||
| ) | ||
| .limit(1) | ||
| .distinct() |
There was a problem hiding this 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:
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.
There was a problem hiding this comment.
Done in 2e7b100. The prefix is compared in the query now, with func.substr(table_namespace, 1, len(namespace) + 1) == f"{namespace}." next to the LIKE, LIMIT 1 is back on both queries, and _contains_namespace is gone. SQLite and PostgreSQL, the two backends the docs list for SqlCatalog, both have substr.
| 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") |
There was a problem hiding this 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.
There was a problem hiding this comment.
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.
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() = '<namespace>.' 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 apache#4163: with only sales.eu, creating a table in SALES raises NoSuchNamespaceError and creating the SALES namespace works.
…sts-case-sensitive
Closes #4163
Rationale for this change
SqlCatalog.namespace_existsalso reports a namespace as existing when a child namespace exists, usingLIKE '<namespace>.%'. SQLite evaluatesLIKEcase-insensitively for ASCII characters, so with onlysales.euin the catalog,namespace_exists("SALES")returned True. As a resultcreate_table("SALES.orders")succeeded in a namespace that does not exist, andcreate_namespace("SALES")failed withNamespaceAlreadyExistsError. On SQLite the exact-match comparisons elsewhere in the catalog are case-sensitive, and PostgreSQL'sLIKEis case-sensitive too.The query now also compares the prefix with
substr(table_namespace, 1, len(namespace) + 1) = '<namespace>.', next to theLIKEthat keeps the lookup index-friendly, and keepsLIMIT 1. Because the comparison uses=, it follows the same rules as the exact namespace lookups in the rest of the catalog: case-sensitive on SQLite and PostgreSQL, and consistent withload_table,create_namespaceand the others on a case-insensitive collation such as MySQL's.Deviation from the Java
JdbcCatalogJdbcUtil.namespaceExistsin Java runs the same= ? OR LIKE ? ESCAPE '!'query withLIMIT 1and no extra prefix check, so the Java catalog has the same behavior on SQLite. This PR fixes it only in PyIceberg. apache/iceberg#19219 tracks the same change for Java so the two stay aligned.Are these changes tested?
Yes, in
tests/catalog/test_catalog_behaviors.py:test_namespace_existsalso checks thatDB2anddb3.NS1are not reported as existing whendb2.ns1anddb3.ns1.ns2exist.test_parent_namespace_match_is_case_sensitiveis the reproduction from SqlCatalog: namespace_exists matches parent namespaces case-insensitively on SQLite #4163: with onlysales.eu,create_table("SALES.orders")raisesNoSuchNamespaceErrorandcreate_namespace("SALES")succeeds.Both fail on main for the
memory,sqlandsql_without_rowcountcatalogs and pass with this change. All three fixtures are SQLite-backed, so no test covers a case-insensitive backend. The pre-commit hooks (ruff, mypy, codespell) pass.Are there any user-facing changes?
On SQLite,
namespace_existsand the operations that depend on it no longer treat a namespace as existing just because a child namespace with a different case exists.