This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] 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 b93982792 Default ascending sort fields to nulls-first (#4390)
b93982792 is described below

commit b9398279265592537f8d178514f91772151229b0
Author: Rahul Shivu Mahadev <[email protected]>
AuthorDate: Wed Nov 18 10:06:11 2026 +0000

    Default ascending sort fields to nulls-first (#4390)
    
    asc() defaulted to nulls-last, and SortField with a str direction did too: 
the
    validator compared the raw str with the enum. Java and SortField default 
ascending
    fields to nulls-first, so default asc() to nulls-first and normalize the 
direction.
    desc() and explicit null orders are unchanged.
    
    Co-authored-by: Isaac <[email protected]>
---
 pyiceberg/table/sorting.py              |  2 +-
 pyiceberg/table/update/sorting.py       |  2 +-
 tests/catalog/test_catalog_behaviors.py | 26 ++++++++++++++++++++++++++
 tests/table/test_sorting.py             |  8 ++++++++
 4 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/pyiceberg/table/sorting.py b/pyiceberg/table/sorting.py
index aed348a26..3a76ccf5f 100644
--- a/pyiceberg/table/sorting.py
+++ b/pyiceberg/table/sorting.py
@@ -92,7 +92,7 @@ class SortField(IcebergBaseModel):
 
     @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 e03d2264d..6c4d483d7 100644
--- a/pyiceberg/table/update/sorting.py
+++ b/pyiceberg/table/update/sorting.py
@@ -87,7 +87,7 @@ class UpdateSortOrder(UpdateTableMetadata["UpdateSortOrder"]):
         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 ffa67d91a..a5099e4e6 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 f8329091b..e2e9b5b4a 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"):

Reply via email to