DG47 commented on code in PR #4164:
URL: https://github.com/apache/iceberg-python/pull/4164#discussion_r4744266726


##########
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:
   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.
   



##########
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:
   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`.
   



##########
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:
   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.
   



-- 
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]

Reply via email to