Skip to content

fix(sql): make namespace_exists prefix check case-sensitive - #4168

Open
Shahzeb-DataEngineer wants to merge 3 commits into
apache:mainfrom
Shahzeb-DataEngineer:fix/namespace-exists-case-sensitive
Open

Shahzeb-DataEngineer wants to merge 3 commits into
apache:mainfrom
Shahzeb-DataEngineer:fix/namespace-exists-case-sensitive

Conversation

@Shahzeb-DataEngineer

Copy link
Copy Markdown
Contributor

Description

SqlCatalog.namespace_exists used a LIKE prefix check to detect parent namespaces. SQLite evaluates LIKE case-insensitively for ASCII, so with only sales.eu stored, namespace_exists("SALES") returned True, while the exact-match lookups in the same catalog and PostgreSQL (where LIKE is case-sensitive) said otherwise. That inconsistency leaked into create_table, create_namespace, and load_namespace_properties, which all go through namespace_exists.

This PR replaces the LIKE prefix check with a substr prefix comparison, which is case-sensitive on both SQLite and PostgreSQL, in both the tables and the namespace-properties queries. It also removes the need for the LIKE pattern escaping. list_namespaces is unchanged: it already filters fuzzy matches in Python with exact tuple equality.

Related Issue

Closes #4163

Testing

  • Reproduced the reported case on SQLite before the fix (namespace_exists("SALES") was True, create_namespace("SALES") raised NamespaceAlreadyExistsError) and verified all of it is fixed after.
  • New regression test test_namespace_exists_is_case_sensitive in tests/catalog/test_sql.py, plus edge cases verified manually (namespaces containing %/_ no longer risk pattern-matching surprises).
  • tests/catalog/test_sql.py: 25 passed. ruff check and ruff format --check clean on both touched files.

SQLite evaluates LIKE case-insensitively for ASCII, so namespace_exists
reported e.g. "SALES" as existing when only "sales.eu" was stored, while
the exact-match lookups and PostgreSQL disagreed. Replace the LIKE
prefix check with a substr prefix comparison, which is case-sensitive
on both backends, and add a regression test.

Signed-off-by: Shahzeb Ali <126076773+Shahzeb-DataEngineer@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 28, 2026 04:48

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions

Copy link
Copy Markdown

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Nov 28, 2026
@Shahzeb-DataEngineer

Copy link
Copy Markdown
Contributor Author

Hi @FokkoDriesprong, just checking in on this one. It is still ready for review whenever you have a moment, and I am happy to rebase if needed. Thanks!

@github-actions github-actions Bot removed the stale label Dec 2, 2026
@Fokko

Fokko commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

just checking in on this one. It is still ready for review whenever you have a moment

Thanks for the patience, @Shahzeb-DataEngineer. I took a look, and I think one thing needs to change before this can go in.

func.substr(...) breaks namespace_exists on SQL Server. SQLAlchemy emits it literally as substr(...) for every dialect, and you can see this by compiling the filter with sqlalchemy.dialects.mssql:

substr(iceberg_tables.table_namespace, :substr_1, :substr_2) = :substr_3

SQL Server only has SUBSTRING/LEFT, so the query fails with 'substr' is not a recognized built-in function name instead of returning a bool. Most catalog operations call namespace_exists (create_namespace, create_table, drop_namespace, list_tables, rename_table, and the namespace-properties calls), so a SQL Server-backed SqlCatalog stops working. The previous LIKE query worked there.

One more thing: on MySQL/MariaDB (and SQL Server) with their default _ci collations, = ignores case just like LIKE. So substr(...) = ... doesn't make the check case-sensitive on those backends.

Could we keep a filter that works on every backend in SQL, either the escaped LIKE or .startswith(prefix, autoescape=True), and then check the exact case-sensitive prefix in Python on the returned rows, like list_namespaces already does? That works on every dialect, gives the same case-sensitive result regardless of collation, and keeps the prefix match able to use the primary-key index.

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