From a0352771d859cd5c591a95341b0189190d151788 Mon Sep 17 00:00:00 2001 From: HonahX Date: Sun, 24 Mar 2024 11:52:17 -0700 Subject: [PATCH] fix v1 validators --- pyiceberg/table/metadata.py | 7 ++++++- tests/table/test_metadata.py | 17 +++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pyiceberg/table/metadata.py b/pyiceberg/table/metadata.py index 323f6d85a8..3e1acf95f1 100644 --- a/pyiceberg/table/metadata.py +++ b/pyiceberg/table/metadata.py @@ -366,6 +366,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": ()}] @@ -389,7 +394,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 diff --git a/tests/table/test_metadata.py b/tests/table/test_metadata.py index 0cf17b11a2..b4e30a6b84 100644 --- a/tests/table/test_metadata.py +++ b/tests/table/test_metadata.py @@ -233,6 +233,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, @@ -250,20 +255,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: