Skip to content

Commit

Permalink
Correctly convert partition_spec and sort_order (apache#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
HonahX committed Mar 28, 2024
1 parent a371077 commit d174760
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
7 changes: 6 additions & 1 deletion pyiceberg/table/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ def construct_partition_specs(cls, data: Dict[str, Any]) -> Dict[str, Any]:
fields = data[PARTITION_SPEC]
data[PARTITION_SPECS] = [{SPEC_ID: INITIAL_SPEC_ID, FIELDS: fields}]
data[DEFAULT_SPEC_ID] = INITIAL_SPEC_ID
elif data.get("partition_spec") is not None:
# Promote the spec from partition_spec to partition-specs
fields = data["partition_spec"]
data[PARTITION_SPECS] = [{SPEC_ID: INITIAL_SPEC_ID, FIELDS: fields}]
data[DEFAULT_SPEC_ID] = INITIAL_SPEC_ID
else:
data[PARTITION_SPECS] = [{"field-id": 0, "fields": ()}]

Expand All @@ -328,7 +333,7 @@ def set_sort_orders(cls, data: Dict[str, Any]) -> Dict[str, Any]:
Returns:
The TableMetadata with the sort_orders set, if not provided.
"""
if not data.get(SORT_ORDERS):
if not data.get(SORT_ORDERS) and not data.get("sort_orders"):
data[SORT_ORDERS] = [UNSORTED_SORT_ORDER]
return data

Expand Down
17 changes: 9 additions & 8 deletions tests/table/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ def test_new_table_metadata_with_explicit_v1_format() -> None:

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

expected_sort_order = SortOrder(
SortField(source_id=1, transform=IdentityTransform(), direction=SortDirection.ASC, null_order=NullOrder.NULLS_LAST),
order_id=1,
)

expected = TableMetadataV1(
location="s3://some_v1_location/",
table_uuid=actual.table_uuid,
Expand All @@ -252,20 +257,16 @@ def test_new_table_metadata_with_explicit_v1_format() -> None:
snapshots=[],
snapshot_log=[],
metadata_log=[],
sort_orders=[
SortOrder(
SortField(
source_id=1, transform=IdentityTransform(), direction=SortDirection.ASC, null_order=NullOrder.NULLS_LAST
),
order_id=1,
)
],
sort_orders=[expected_sort_order],
default_sort_order_id=1,
refs={},
format_version=1,
)

assert actual.model_dump() == expected.model_dump()
assert actual.schemas == [expected_schema]
assert actual.partition_specs == [expected_spec]
assert actual.sort_orders == [expected_sort_order]


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

0 comments on commit d174760

Please sign in to comment.