Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,20 +617,24 @@ def create_namespace(self, namespace: str | Identifier, properties: Properties =
if self.namespace_exists(namespace):
raise NamespaceAlreadyExistsError(f"Namespace {namespace} already exists")

if not properties:
properties = IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES
Comment on lines -620 to -621

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.

redundant with the next line

create_properties = properties if properties else IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES
namespace_string = Catalog.namespace_to_string(namespace, NoSuchNamespaceError)

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.

avoid generating the namespace string in the for loop

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:
Expand Down
33 changes: 32 additions & 1 deletion tests/catalog/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
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
Expand All @@ -32,6 +32,7 @@
SqlCatalogBaseTable,
)
from pyiceberg.exceptions import (
NamespaceAlreadyExistsError,
NoSuchPropertyException,
NoSuchTableError,
TableAlreadyExistsError,
Expand Down Expand Up @@ -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()
Loading