Skip to content

Commit d174760

Browse files
committed
Correctly convert partition_spec and sort_order (#544)
1 parent a371077 commit d174760

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

pyiceberg/table/metadata.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ def construct_partition_specs(cls, data: Dict[str, Any]) -> Dict[str, Any]:
306306
fields = data[PARTITION_SPEC]
307307
data[PARTITION_SPECS] = [{SPEC_ID: INITIAL_SPEC_ID, FIELDS: fields}]
308308
data[DEFAULT_SPEC_ID] = INITIAL_SPEC_ID
309+
elif data.get("partition_spec") is not None:
310+
# Promote the spec from partition_spec to partition-specs
311+
fields = data["partition_spec"]
312+
data[PARTITION_SPECS] = [{SPEC_ID: INITIAL_SPEC_ID, FIELDS: fields}]
313+
data[DEFAULT_SPEC_ID] = INITIAL_SPEC_ID
309314
else:
310315
data[PARTITION_SPECS] = [{"field-id": 0, "fields": ()}]
311316

@@ -328,7 +333,7 @@ def set_sort_orders(cls, data: Dict[str, Any]) -> Dict[str, Any]:
328333
Returns:
329334
The TableMetadata with the sort_orders set, if not provided.
330335
"""
331-
if not data.get(SORT_ORDERS):
336+
if not data.get(SORT_ORDERS) and not data.get("sort_orders"):
332337
data[SORT_ORDERS] = [UNSORTED_SORT_ORDER]
333338
return data
334339

tests/table/test_metadata.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ def test_new_table_metadata_with_explicit_v1_format() -> None:
235235

236236
expected_spec = PartitionSpec(PartitionField(source_id=2, field_id=1000, transform=IdentityTransform(), name="bar"))
237237

238+
expected_sort_order = SortOrder(
239+
SortField(source_id=1, transform=IdentityTransform(), direction=SortDirection.ASC, null_order=NullOrder.NULLS_LAST),
240+
order_id=1,
241+
)
242+
238243
expected = TableMetadataV1(
239244
location="s3://some_v1_location/",
240245
table_uuid=actual.table_uuid,
@@ -252,20 +257,16 @@ def test_new_table_metadata_with_explicit_v1_format() -> None:
252257
snapshots=[],
253258
snapshot_log=[],
254259
metadata_log=[],
255-
sort_orders=[
256-
SortOrder(
257-
SortField(
258-
source_id=1, transform=IdentityTransform(), direction=SortDirection.ASC, null_order=NullOrder.NULLS_LAST
259-
),
260-
order_id=1,
261-
)
262-
],
260+
sort_orders=[expected_sort_order],
263261
default_sort_order_id=1,
264262
refs={},
265263
format_version=1,
266264
)
267265

268266
assert actual.model_dump() == expected.model_dump()
267+
assert actual.schemas == [expected_schema]
268+
assert actual.partition_specs == [expected_spec]
269+
assert actual.sort_orders == [expected_sort_order]
269270

270271

271272
def test_invalid_format_version(example_table_metadata_v1: Dict[str, Any]) -> None:

0 commit comments

Comments
 (0)