Skip to content

Commit ba3ae08

Browse files
Two concurrent create_namespace requests should raise a NamespaceAlreadyExistsError (#4408)
* Two concurrent create_namespace requests raise a NamespaceAlreadyExistsError * Missing import
1 parent 9955502 commit ba3ae08

2 files changed

Lines changed: 47 additions & 12 deletions

File tree

‎pyiceberg/catalog/sql.py‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -617,20 +617,24 @@ def create_namespace(self, namespace: str | Identifier, properties: Properties =
617617
if self.namespace_exists(namespace):
618618
raise NamespaceAlreadyExistsError(f"Namespace {namespace} already exists")
619619

620-
if not properties:
621-
properties = IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES
622620
create_properties = properties if properties else IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES
621+
namespace_string = Catalog.namespace_to_string(namespace, NoSuchNamespaceError)
623622
with Session(self.engine) as session:
624-
for key, value in create_properties.items():
625-
session.add(
626-
IcebergNamespaceProperties(
627-
catalog_name=self.name,
628-
namespace=Catalog.namespace_to_string(namespace, NoSuchNamespaceError),
629-
property_key=key,
630-
property_value=value,
623+
try:
624+
for key, value in create_properties.items():
625+
session.add(
626+
IcebergNamespaceProperties(
627+
catalog_name=self.name,
628+
namespace=namespace_string,
629+
property_key=key,
630+
property_value=value,
631+
)
631632
)
632-
)
633-
session.commit()
633+
session.commit()
634+
except IntegrityError as e:
635+
if any(k is None or v is None for k, v in create_properties.items()):
636+
raise
637+
raise NamespaceAlreadyExistsError(f"Namespace {namespace} already exists") from e
634638

635639
@override
636640
def drop_namespace(self, namespace: str | Identifier) -> None:

‎tests/catalog/test_sql.py‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from typing import cast
2121

2222
import pytest
23-
from sqlalchemy import Engine, create_engine, inspect, text
23+
from sqlalchemy import Engine, create_engine, event, inspect, text
2424
from sqlalchemy.exc import ArgumentError
2525

2626
from pyiceberg.catalog import load_catalog
@@ -32,6 +32,7 @@
3232
SqlCatalogBaseTable,
3333
)
3434
from pyiceberg.exceptions import (
35+
NamespaceAlreadyExistsError,
3536
NoSuchPropertyException,
3637
NoSuchTableError,
3738
TableAlreadyExistsError,
@@ -559,3 +560,33 @@ def test_create_and_load_table_on_v0_schema(warehouse: Path) -> None:
559560

560561
# iceberg_type column must still be absent from the DB (no migration occurred)
561562
assert "iceberg_type" not in get_columns(catalog.engine)
563+
564+
565+
def test_create_namespace_concurrent_integrity_error(warehouse: Path) -> None:
566+
"""Concurrent create_namespace requests should raise NamespaceAlreadyExistsError."""
567+
uri = f"sqlite:///{warehouse.as_posix()}/test-concurrent-create-namespace"
568+
warehouse_uri = f"file://{warehouse}"
569+
catalog_1 = SqlCatalog("test", uri=uri, warehouse=warehouse_uri)
570+
catalog_2 = SqlCatalog("test", uri=uri, warehouse=warehouse_uri)
571+
try:
572+
injected = False
573+
574+
@event.listens_for(catalog_1.engine, "before_cursor_execute")
575+
def inject_concurrent_insert(
576+
_conn: object,
577+
_cursor: object,
578+
statement: str,
579+
_parameters: object,
580+
_context: object,
581+
_executemany: bool,
582+
) -> None:
583+
nonlocal injected
584+
if not injected and "INSERT INTO iceberg_namespace_properties" in statement:
585+
injected = True
586+
catalog_2.create_namespace("concurrent_ns")
587+
588+
with pytest.raises(NamespaceAlreadyExistsError):
589+
catalog_1.create_namespace("concurrent_ns")
590+
finally:
591+
catalog_1.close()
592+
catalog_2.close()

0 commit comments

Comments
 (0)