diff --git a/pyiceberg/table/sorting.py b/pyiceberg/table/sorting.py index aed348a265..3a76ccf5fd 100644 --- a/pyiceberg/table/sorting.py +++ b/pyiceberg/table/sorting.py @@ -92,7 +92,7 @@ def __init__( @model_validator(mode="before") def set_null_order(cls, values: dict[str, Any]) -> dict[str, Any]: - values["direction"] = values["direction"] if values.get("direction") else SortDirection.ASC + values["direction"] = SortDirection(values["direction"]) if values.get("direction") else SortDirection.ASC if not values.get("null-order"): values["null-order"] = NullOrder.NULLS_FIRST if values["direction"] == SortDirection.ASC else NullOrder.NULLS_LAST return values diff --git a/pyiceberg/table/update/sorting.py b/pyiceberg/table/update/sorting.py index e03d2264df..6c4d483d72 100644 --- a/pyiceberg/table/update/sorting.py +++ b/pyiceberg/table/update/sorting.py @@ -87,7 +87,7 @@ def _reuse_or_create_sort_order_id(self) -> int: return new_sort_order_id def asc( - self, source_column_name: str, transform: Transform[Any, Any], null_order: NullOrder = NullOrder.NULLS_LAST + self, source_column_name: str, transform: Transform[Any, Any], null_order: NullOrder = NullOrder.NULLS_FIRST ) -> UpdateSortOrder: """Add a sort field with ascending order.""" return self._add_sort_field( diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index cd817512b4..463913d9da 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -240,6 +240,32 @@ def test_create_table_custom_sort_order(catalog: Catalog, table_schema_nested: S catalog.drop_table(test_table_identifier) +def test_update_sort_order_default_null_order( + catalog: Catalog, table_schema_simple: Schema, test_table_identifier: Identifier +) -> None: + catalog.create_namespace(Catalog.namespace_from(test_table_identifier)) + table = catalog.create_table(test_table_identifier, table_schema_simple) + + # Without a null order, ascending fields are nulls-first and descending fields are nulls-last + table.update_sort_order().asc("foo", IdentityTransform()).desc("bar", IdentityTransform()).commit() + assert table.sort_order() == SortOrder( + SortField(source_id=1, transform=IdentityTransform(), direction=SortDirection.ASC, null_order=NullOrder.NULLS_FIRST), + SortField(source_id=2, transform=IdentityTransform(), direction=SortDirection.DESC, null_order=NullOrder.NULLS_LAST), + order_id=1, + ) + + # An explicit null order is kept + table.update_sort_order().asc("foo", IdentityTransform(), NullOrder.NULLS_LAST).desc( + "bar", IdentityTransform(), NullOrder.NULLS_FIRST + ).commit() + assert table.sort_order() == SortOrder( + SortField(source_id=1, transform=IdentityTransform(), direction=SortDirection.ASC, null_order=NullOrder.NULLS_LAST), + SortField(source_id=2, transform=IdentityTransform(), direction=SortDirection.DESC, null_order=NullOrder.NULLS_FIRST), + order_id=2, + ) + catalog.drop_table(test_table_identifier) + + def test_create_table_with_default_warehouse_location( warehouse: Path, catalog_with_warehouse: Catalog, table_schema_nested: Schema, test_table_identifier: Identifier ) -> None: diff --git a/tests/table/test_sorting.py b/tests/table/test_sorting.py index f8329091b4..e2e9b5b4a9 100644 --- a/tests/table/test_sorting.py +++ b/tests/table/test_sorting.py @@ -138,6 +138,14 @@ def test_serialize_sort_field_v3() -> None: assert SortField.model_validate_json(payload) == expected +def test_deserialize_sort_field_str_direction() -> None: + # A str direction gets the same default null order as the SortDirection member + for direction, null_order in [(SortDirection.ASC, NullOrder.NULLS_FIRST), (SortDirection.DESC, NullOrder.NULLS_LAST)]: + payload = {"source-id": 19, "transform": "identity", "direction": direction.value} + expected = SortField(source_id=19, transform=IdentityTransform(), direction=direction, null_order=null_order) + assert SortField.model_validate(payload) == expected + + def test_deserialize_sort_field_empty_source_ids_rejected() -> None: payload = '{"source-ids":[],"transform":"identity","direction":"asc","null-order":"nulls-first"}' with pytest.raises(Exception, match="Empty source-ids is not allowed"):