This is an automated email from the ASF dual-hosted git repository.
fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git
The following commit(s) were added to refs/heads/main by this push:
new cc8552de Correctly convert `partition_spec` and `sort_order` (#544)
cc8552de is described below
commit cc8552dec490b342d0ebb8159deeaf0c8f35ab40
Author: Honah J <[email protected]>
AuthorDate: Sun Mar 24 22:19:38 2024 -0700
Correctly convert `partition_spec` and `sort_order` (#544)
---
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 323f6d85..3e1acf95 100644
--- a/pyiceberg/table/metadata.py
+++ b/pyiceberg/table/metadata.py
@@ -366,6 +366,11 @@ class TableMetadataV1(TableMetadataCommonFields,
IcebergBaseModel):
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 @@ class TableMetadataV1(TableMetadataCommonFields,
IcebergBaseModel):
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 0cf17b11..b4e30a6b 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: