Skip to content

Commit 70342ac

Browse files
authored
Allow setting non-string typed values in set_properties (#504)
* pass dict properties to set_properties * whitespace * set non-string property values * test error for none * add comment * rewrite validator * properties validator
1 parent afdfa35 commit 70342ac

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

pyiceberg/catalog/rest.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ class CreateTableRequest(IcebergBaseModel):
149149
partition_spec: Optional[PartitionSpec] = Field(alias="partition-spec")
150150
write_order: Optional[SortOrder] = Field(alias="write-order")
151151
stage_create: bool = Field(alias="stage-create", default=False)
152-
properties: Properties = Field(default_factory=dict)
152+
properties: Dict[str, str] = Field(default_factory=dict)
153+
153154
# validators
154-
transform_properties_dict_value_to_str = field_validator('properties', mode='before')(transform_dict_value_to_str)
155+
@field_validator('properties', mode='before')
156+
def transform_properties_dict_value_to_str(cls, properties: Properties) -> Dict[str, str]:
157+
return transform_dict_value_to_str(properties)
155158

156159

157160
class RegisterTableRequest(IcebergBaseModel):

pyiceberg/table/__init__.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
Union,
4343
)
4444

45-
from pydantic import Field, SerializeAsAny
45+
from pydantic import Field, SerializeAsAny, field_validator
4646
from sortedcontainers import SortedList
4747
from typing_extensions import Annotated
4848

@@ -124,6 +124,7 @@
124124
NestedField,
125125
PrimitiveType,
126126
StructType,
127+
transform_dict_value_to_str,
127128
)
128129
from pyiceberg.utils.concurrent import ExecutorFactory
129130
from pyiceberg.utils.datetime import datetime_to_millis
@@ -293,7 +294,7 @@ def upgrade_table_version(self, format_version: Literal[1, 2]) -> Transaction:
293294

294295
return self
295296

296-
def set_properties(self, properties: Properties = EMPTY_DICT, **kwargs: str) -> Transaction:
297+
def set_properties(self, properties: Properties = EMPTY_DICT, **kwargs: Any) -> Transaction:
297298
"""Set properties.
298299
299300
When a property is already set, it will be overwritten.
@@ -474,6 +475,10 @@ class SetPropertiesUpdate(TableUpdate):
474475
action: TableUpdateAction = TableUpdateAction.set_properties
475476
updates: Dict[str, str]
476477

478+
@field_validator('updates', mode='before')
479+
def transform_properties_dict_value_to_str(cls, properties: Properties) -> Dict[str, str]:
480+
return transform_dict_value_to_str(properties)
481+
477482

478483
class RemovePropertiesUpdate(TableUpdate):
479484
action: TableUpdateAction = TableUpdateAction.remove_properties

pyiceberg/table/metadata.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ class TableMetadataCommonFields(IcebergBaseModel):
221221
current-snapshot-id even if the refs map is null."""
222222

223223
# validators
224-
transform_properties_dict_value_to_str = field_validator('properties', mode='before')(transform_dict_value_to_str)
224+
@field_validator('properties', mode='before')
225+
def transform_properties_dict_value_to_str(cls, properties: Properties) -> Dict[str, str]:
226+
return transform_dict_value_to_str(properties)
225227

226228
def snapshot_by_id(self, snapshot_id: int) -> Optional[Snapshot]:
227229
"""Get the snapshot by snapshot_id."""

tests/integration/test_reads.py

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import pytest
2525
from hive_metastore.ttypes import LockRequest, LockResponse, LockState, UnlockRequest
2626
from pyarrow.fs import S3FileSystem
27+
from pydantic_core import ValidationError
2728

2829
from pyiceberg.catalog import Catalog, load_catalog
2930
from pyiceberg.catalog.hive import HiveCatalog, _HiveClient
@@ -119,6 +120,14 @@ def test_table_properties(catalog: Catalog) -> None:
119120
table = table.transaction().remove_properties("abc").commit_transaction()
120121
assert table.properties == DEFAULT_PROPERTIES
121122

123+
table = table.transaction().set_properties(abc=123).commit_transaction()
124+
# properties are stored as strings in the iceberg spec
125+
assert table.properties == dict(abc="123", **DEFAULT_PROPERTIES)
126+
127+
with pytest.raises(ValidationError) as exc_info:
128+
table.transaction().set_properties(property_name=None).commit_transaction()
129+
assert "None type is not a supported value in properties: property_name" in str(exc_info.value)
130+
122131

123132
@pytest.mark.integration
124133
@pytest.mark.parametrize('catalog', [pytest.lazy_fixture('catalog_hive'), pytest.lazy_fixture('catalog_rest')])
@@ -141,6 +150,14 @@ def test_table_properties_dict(catalog: Catalog) -> None:
141150
table = table.transaction().remove_properties("abc").commit_transaction()
142151
assert table.properties == DEFAULT_PROPERTIES
143152

153+
table = table.transaction().set_properties({"abc": 123}).commit_transaction()
154+
# properties are stored as strings in the iceberg spec
155+
assert table.properties == dict({"abc": "123"}, **DEFAULT_PROPERTIES)
156+
157+
with pytest.raises(ValidationError) as exc_info:
158+
table.transaction().set_properties({"property_name": None}).commit_transaction()
159+
assert "None type is not a supported value in properties: property_name" in str(exc_info.value)
160+
144161

145162
@pytest.mark.integration
146163
@pytest.mark.parametrize('catalog', [pytest.lazy_fixture('catalog_hive'), pytest.lazy_fixture('catalog_rest')])

0 commit comments

Comments
 (0)