Skip to content
Open
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
2 changes: 1 addition & 1 deletion pyiceberg/table/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyiceberg/table/update/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 26 additions & 0 deletions tests/catalog/test_catalog_behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/table/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
Loading