This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-4408-9955502c73758611d5ad69220b67aedb3d4a443e
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git

commit ba3ae084dcf6c7fd0897f924970d587fc33bd154
Author: i alarmed alien <[email protected]>
AuthorDate: Wed Nov 18 20:17:05 2026 +0000

    Two concurrent create_namespace requests should raise a 
NamespaceAlreadyExistsError (#4408)
    
    * Two concurrent create_namespace requests raise a 
NamespaceAlreadyExistsError
    
    * Missing import
---
 pyiceberg/catalog/sql.py  | 26 +++++++++++++++-----------
 tests/catalog/test_sql.py | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py
index 31f39c29a..15e32303b 100644
--- a/pyiceberg/catalog/sql.py
+++ b/pyiceberg/catalog/sql.py
@@ -617,20 +617,24 @@ class SqlCatalog(MetastoreCatalog):
         if self.namespace_exists(namespace):
             raise NamespaceAlreadyExistsError(f"Namespace {namespace} already 
exists")
 
-        if not properties:
-            properties = 
IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES
         create_properties = properties if properties else 
IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES
+        namespace_string = Catalog.namespace_to_string(namespace, 
NoSuchNamespaceError)
         with Session(self.engine) as session:
-            for key, value in create_properties.items():
-                session.add(
-                    IcebergNamespaceProperties(
-                        catalog_name=self.name,
-                        namespace=Catalog.namespace_to_string(namespace, 
NoSuchNamespaceError),
-                        property_key=key,
-                        property_value=value,
+            try:
+                for key, value in create_properties.items():
+                    session.add(
+                        IcebergNamespaceProperties(
+                            catalog_name=self.name,
+                            namespace=namespace_string,
+                            property_key=key,
+                            property_value=value,
+                        )
                     )
-                )
-            session.commit()
+                session.commit()
+            except IntegrityError as e:
+                if any(k is None or v is None for k, v in 
create_properties.items()):
+                    raise
+                raise NamespaceAlreadyExistsError(f"Namespace {namespace} 
already exists") from e
 
     @override
     def drop_namespace(self, namespace: str | Identifier) -> None:
diff --git a/tests/catalog/test_sql.py b/tests/catalog/test_sql.py
index 8e0348cba..3d40cad22 100644
--- a/tests/catalog/test_sql.py
+++ b/tests/catalog/test_sql.py
@@ -20,7 +20,7 @@ from pathlib import Path
 from typing import cast
 
 import pytest
-from sqlalchemy import Engine, create_engine, inspect, text
+from sqlalchemy import Engine, create_engine, event, inspect, text
 from sqlalchemy.exc import ArgumentError
 
 from pyiceberg.catalog import load_catalog
@@ -32,6 +32,7 @@ from pyiceberg.catalog.sql import (
     SqlCatalogBaseTable,
 )
 from pyiceberg.exceptions import (
+    NamespaceAlreadyExistsError,
     NoSuchPropertyException,
     NoSuchTableError,
     TableAlreadyExistsError,
@@ -559,3 +560,33 @@ def test_create_and_load_table_on_v0_schema(warehouse: 
Path) -> None:
 
     # iceberg_type column must still be absent from the DB (no migration 
occurred)
     assert "iceberg_type" not in get_columns(catalog.engine)
+
+
+def test_create_namespace_concurrent_integrity_error(warehouse: Path) -> None:
+    """Concurrent create_namespace requests should raise 
NamespaceAlreadyExistsError."""
+    uri = f"sqlite:///{warehouse.as_posix()}/test-concurrent-create-namespace"
+    warehouse_uri = f"file://{warehouse}"
+    catalog_1 = SqlCatalog("test", uri=uri, warehouse=warehouse_uri)
+    catalog_2 = SqlCatalog("test", uri=uri, warehouse=warehouse_uri)
+    try:
+        injected = False
+
+        @event.listens_for(catalog_1.engine, "before_cursor_execute")
+        def inject_concurrent_insert(
+            _conn: object,
+            _cursor: object,
+            statement: str,
+            _parameters: object,
+            _context: object,
+            _executemany: bool,
+        ) -> None:
+            nonlocal injected
+            if not injected and "INSERT INTO iceberg_namespace_properties" in 
statement:
+                injected = True
+                catalog_2.create_namespace("concurrent_ns")
+
+        with pytest.raises(NamespaceAlreadyExistsError):
+            catalog_1.create_namespace("concurrent_ns")
+    finally:
+        catalog_1.close()
+        catalog_2.close()

Reply via email to