Skip to content

Fix case-insensitive parent namespace match in SqlCatalog.namespace_exists - #4164

Open
DG47 wants to merge 5 commits into
apache:mainfrom
DG47:fix/sql-namespace-exists-case-sensitive
Open

DG47 wants to merge 5 commits into
apache:mainfrom
DG47:fix/sql-namespace-exists-case-sensitive

Conversation

@DG47

@DG47 DG47 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Closes #4163

Rationale for this change

SqlCatalog.namespace_exists also reports a namespace as existing when a child namespace exists, using LIKE '<namespace>.%'. SQLite evaluates LIKE case-insensitively for ASCII characters, so with only sales.eu in the catalog, namespace_exists("SALES") returned True. As a result create_table("SALES.orders") succeeded in a namespace that does not exist, and create_namespace("SALES") failed with NamespaceAlreadyExistsError. On SQLite the exact-match comparisons elsewhere in the catalog are case-sensitive, and PostgreSQL's LIKE is case-sensitive too.

The query now also compares the prefix with substr(table_namespace, 1, len(namespace) + 1) = '<namespace>.', next to the LIKE that keeps the lookup index-friendly, and keeps LIMIT 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 with load_table, create_namespace and the others on a case-insensitive collation such as MySQL's.

Deviation from the Java JdbcCatalog

JdbcUtil.namespaceExists in Java runs the same = ? OR LIKE ? ESCAPE '!' query with LIMIT 1 and 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:

Both fail on main for the memory, sql and sql_without_rowcount catalogs 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_exists and the operations that depend on it no longer treat a namespace as existing just because a child namespace with a different case exists.

DG47 added 2 commits October 27, 2026 06:30
…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.
@DG47

DG47 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

@Fokko @kevinjqliu, this one is ready for a review. SqlCatalog.namespace_exists uses LIKE '<namespace>.%' to detect child namespaces, and SQLite evaluates LIKE case-insensitively, so with only sales.eu in the catalog namespace_exists("SALES") returned True and create_table("SALES.orders") succeeded in a namespace that does not exist (closes #4163). The fix keeps the LIKE filter and re-checks the matched names with a case-sensitive prefix comparison in Python, the same way list_namespaces already does.

@rambleraptor rambleraptor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems reasonable. I'm a little concerned about performance on the .distinct() calls, but some of this seems unavoidable.

@DG47

DG47 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Hi @Fokko and @kevinjqliu, a follow-up on the fix for #4163. On SQLite, namespace_exists("SALES") returned True when only sales.eu existed because LIKE is case-insensitive there, and SqlCatalog.namespace_exists now re-checks the matched names with a case-sensitive prefix comparison. Could one of you review it?

@Fokko Fokko left a comment

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.

Thanks for the fix! I compared this against the Java JdbcCatalog, a few notes inline.

Comment thread pyiceberg/catalog/sql.py Outdated

@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

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.

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.

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.

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.

Comment thread pyiceberg/catalog/sql.py Outdated
| (IcebergTables.table_namespace.like(namespace_starts_with, escape="!")),
)
.limit(1)
.distinct()

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.

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.

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.

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")

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.

DG47 added 2 commits November 30, 2026 14:19
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.

@Fokko Fokko left a comment

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.

LGTM, thanks @DG47! Nice catch on the SQLite LIKE case-insensitivity, and the in-SQL prefix check keeps LIMIT 1 in place. 🚀

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqlCatalog: namespace_exists matches parent namespaces case-insensitively on SQLite

3 participants