Skip to content

Commit cc8552d

Browse files
authored
Correctly convert partition_spec and sort_order (apache#544)
1 parent 6989b92 commit cc8552d

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
@@ -366,6 +366,11 @@ def construct_partition_specs(cls, data: Dict[str, Any]) -> Dict[str, Any]:
366366
fields = data[PARTITION_SPEC]
367367
data[PARTITION_SPECS] = [{SPEC_ID: INITIAL_SPEC_ID, FIELDS: fields}]
368368
data[DEFAULT_SPEC_ID] = INITIAL_SPEC_ID
369+
elif data.get("partition_spec") is not None:
370+
# Promote the spec from partition_spec to partition-specs
371+
fields = data["partition_spec"]
372+
data[PARTITION_SPECS] = [{SPEC_ID: INITIAL_SPEC_ID, FIELDS: fields}]
373+
data[DEFAULT_SPEC_ID] = INITIAL_SPEC_ID
369374
else:
370375
data[PARTITION_SPECS] = [{"field-id": 0, "fields": ()}]
371376

@@ -389,7 +394,7 @@ def set_sort_orders(cls, data: Dict[str, Any]) -> Dict[str, Any]:
389394
Returns:
390395
The TableMetadata with the sort_orders set, if not provided.
391396
"""
392-
if not data.get(SORT_ORDERS):
397+
if not data.get(SORT_ORDERS) and not data.get("sort_orders"):
393398
data[SORT_ORDERS] = [UNSORTED_SORT_ORDER]
394399
return data
395400

tests/table/test_metadata.py

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

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

236+
expected_sort_order = SortOrder(
237+
SortField(source_id=1, transform=IdentityTransform(), direction=SortDirection.ASC, null_order=NullOrder.NULLS_LAST),
238+
order_id=1,
239+
)
240+
236241
expected = TableMetadataV1(
237242
location="s3://some_v1_location/",
238243
table_uuid=actual.table_uuid,
@@ -250,20 +255,16 @@ def test_new_table_metadata_with_explicit_v1_format() -> None:
250255
snapshots=[],
251256
snapshot_log=[],
252257
metadata_log=[],
253-
sort_orders=[
254-
SortOrder(
255-
SortField(
256-
source_id=1, transform=IdentityTransform(), direction=SortDirection.ASC, null_order=NullOrder.NULLS_LAST
257-
),
258-
order_id=1,
259-
)
260-
],
258+
sort_orders=[expected_sort_order],
261259
default_sort_order_id=1,
262260
refs={},
263261
format_version=1,
264262
)
265263

266264
assert actual.model_dump() == expected.model_dump()
265+
assert actual.schemas == [expected_schema]
266+
assert actual.partition_specs == [expected_spec]
267+
assert actual.sort_orders == [expected_sort_order]
267268

268269

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

0 commit comments

Comments
 (0)