Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Fix] Fix TableMetadataV1 Validators #544

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pyiceberg/table/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": ()}]

Expand All @@ -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

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 @@ -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,
Expand All @@ -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:
Expand Down