Skip to content
Open
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
36 changes: 29 additions & 7 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,12 @@ def namespace_exists(self, identifier: str | Identifier) -> bool:
return True
return False

@staticmethod
def _check_for_reserved_properties(properties: Properties) -> None:
for reserved_key in IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES:
if reserved_key in properties:
raise ValueError(f"Cannot set reserved property: {reserved_key}")

@override
def create_namespace(self, namespace: str | Identifier, properties: Properties = EMPTY_DICT) -> None:
"""Create a namespace in the catalog.
Expand All @@ -613,11 +619,13 @@ def create_namespace(self, namespace: str | Identifier, properties: Properties =

Raises:
NamespaceAlreadyExistsError: If a namespace with the given name already exists.
ValueError: If properties set a reserved property.
"""
if self.namespace_exists(namespace):
raise NamespaceAlreadyExistsError(f"Namespace {namespace} already exists")

create_properties = properties if properties else IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES
self._check_for_reserved_properties(properties)
create_properties = {**properties, **IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES}
namespace_string = Catalog.namespace_to_string(namespace, NoSuchNamespaceError)
with Session(self.engine) as session:
try:
Expand Down Expand Up @@ -759,7 +767,11 @@ def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
)
with Session(self.engine) as session:
result = session.scalars(stmt)
return {props.property_key: props.property_value for props in result}
return {
props.property_key: props.property_value
for props in result
if props.property_key not in IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES
}

@override
def update_namespace_properties(
Expand All @@ -774,23 +786,25 @@ def update_namespace_properties(

Raises:
NoSuchNamespaceError: If a namespace with the given name does not exist.
ValueError: If removals and updates have overlapping keys.
ValueError: If removals and updates have overlapping keys, or updates set a reserved property.
"""
namespace_str = Catalog.namespace_to_string(namespace)
if not self.namespace_exists(namespace):
raise NoSuchNamespaceError(f"Namespace {namespace_str} does not exists")

self._check_for_reserved_properties(updates)

current_properties = self.load_namespace_properties(namespace=namespace)
properties_update_summary, updated_properties = self._get_updated_props_and_update_summary(
properties_update_summary, _ = self._get_updated_props_and_update_summary(
current_properties=current_properties, removals=removals, updates=updates
)

with Session(self.engine) as session:
if removals:
if properties_update_summary.removed:
delete_stmt = delete(IcebergNamespaceProperties).where(
IcebergNamespaceProperties.catalog_name == self.name,
IcebergNamespaceProperties.namespace == namespace_str,
IcebergNamespaceProperties.property_key.in_(removals),
IcebergNamespaceProperties.property_key.in_(properties_update_summary.removed),
)
session.execute(delete_stmt)

Expand All @@ -816,7 +830,15 @@ def update_namespace_properties(
insert_stmt = insert(IcebergNamespaceProperties).values(insert_stmt_values)
session.execute(insert_stmt)

if properties_update_summary.removed and not updated_properties:
remaining_stmt = (
select(IcebergNamespaceProperties)
.where(
IcebergNamespaceProperties.catalog_name == self.name,
IcebergNamespaceProperties.namespace == namespace_str,
)
.limit(1)
)
if properties_update_summary.removed and not session.execute(remaining_stmt).all():
# A namespace without tables only exists through its properties
for key, value in IcebergNamespaceProperties.NAMESPACE_MINIMAL_PROPERTIES.items():
session.add(
Expand Down
20 changes: 18 additions & 2 deletions tests/catalog/test_catalog_behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ def test_load_namespace_properties_non_existing_namespace(catalog: Catalog) -> N
def test_load_empty_namespace_properties(catalog: Catalog, test_namespace: Identifier) -> None:
catalog.create_namespace(test_namespace)
listed_properties = catalog.load_namespace_properties(test_namespace)
assert listed_properties == {"exists": "true"}
assert listed_properties == {}


# List namespaces tests
Expand Down Expand Up @@ -1365,7 +1365,23 @@ def test_update_namespace_properties_removes_all_properties(catalog: Catalog, te
assert update_report.removed == ["comment"]
assert catalog.namespace_exists(test_namespace)
assert test_namespace in catalog.list_namespaces(test_namespace[:-1])
assert catalog.load_namespace_properties(test_namespace) == {"exists": "true"}
assert catalog.load_namespace_properties(test_namespace) == {}


def test_namespace_rejects_reserved_exists_property(catalog: Catalog, test_namespace: Identifier) -> None:
with pytest.raises(ValueError, match="Cannot set reserved property: exists"):
catalog.create_namespace(test_namespace, {"exists": "false"})
assert not catalog.namespace_exists(test_namespace)

catalog.create_namespace(test_namespace, {"comment": "this is a test description"})
with pytest.raises(ValueError, match="Cannot set reserved property: exists"):
catalog.update_namespace_properties(test_namespace, updates={"exists": "false"})

update_report = catalog.update_namespace_properties(test_namespace, removals={"exists", "comment"})
assert update_report.removed == ["comment"]
assert update_report.missing == ["exists"]
assert catalog.namespace_exists(test_namespace)
assert catalog.load_namespace_properties(test_namespace) == {}


def test_update_namespace_metadata_raises_error_when_namespace_does_not_exist(
Expand Down
19 changes: 19 additions & 0 deletions tests/catalog/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,25 @@ def test_create_and_load_table_on_v0_schema(warehouse: Path) -> None:
assert "iceberg_type" not in get_columns(catalog.engine)


def test_namespace_created_with_properties_survives_external_property_removal(warehouse: Path) -> None:
catalog = SqlCatalog("test", uri="sqlite:///:memory:", warehouse=f"file://{warehouse}")
catalog.create_namespace("ns", {"comment": "this is a test description"})
assert catalog.load_namespace_properties("ns") == {"comment": "this is a test description"}

# Java's JdbcCatalog.removeProperties deletes the rows and relies on the exists marker
with catalog.engine.connect() as conn:
conn.execute(
text(
"DELETE FROM iceberg_namespace_properties "
"WHERE catalog_name = 'test' AND namespace = 'ns' AND property_key = 'comment'"
)
)
conn.commit()

assert catalog.namespace_exists("ns")
assert catalog.load_namespace_properties("ns") == {}


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"
Expand Down