Skip to content

sorting

NullOrder

Bases: Enum

Source code in pyiceberg/table/sorting.py
class NullOrder(Enum):
    NULLS_FIRST = "nulls-first"
    NULLS_LAST = "nulls-last"

    def __str__(self) -> str:
        """Return the string representation of the NullOrder class."""
        return self.name.replace("_", " ")

    def __repr__(self) -> str:
        """Return the string representation of the NullOrder class."""
        return f"NullOrder.{self.name}"

__repr__()

Return the string representation of the NullOrder class.

Source code in pyiceberg/table/sorting.py
def __repr__(self) -> str:
    """Return the string representation of the NullOrder class."""
    return f"NullOrder.{self.name}"

__str__()

Return the string representation of the NullOrder class.

Source code in pyiceberg/table/sorting.py
def __str__(self) -> str:
    """Return the string representation of the NullOrder class."""
    return self.name.replace("_", " ")

SortDirection

Bases: Enum

Source code in pyiceberg/table/sorting.py
class SortDirection(Enum):
    ASC = "asc"
    DESC = "desc"

    def __str__(self) -> str:
        """Return the string representation of the SortDirection class."""
        return self.name

    def __repr__(self) -> str:
        """Return the string representation of the SortDirection class."""
        return f"SortDirection.{self.name}"

__repr__()

Return the string representation of the SortDirection class.

Source code in pyiceberg/table/sorting.py
def __repr__(self) -> str:
    """Return the string representation of the SortDirection class."""
    return f"SortDirection.{self.name}"

__str__()

Return the string representation of the SortDirection class.

Source code in pyiceberg/table/sorting.py
def __str__(self) -> str:
    """Return the string representation of the SortDirection class."""
    return self.name

SortField

Bases: IcebergBaseModel

Sort order field.

Parameters:

Name Type Description Default
source_id int

Source column id from the table’s schema.

None
transform str

Transform that is used to produce values to be sorted on from the source column. This is the same transform as described in partition transforms.

None
direction SortDirection

Sort direction, that can only be either asc or desc.

None
null_order NullOrder

Null order that describes the order of null values when sorted. Can only be either nulls-first or nulls-last.

None
Source code in pyiceberg/table/sorting.py
class SortField(IcebergBaseModel):
    """Sort order field.

    Args:
      source_id (int): Source column id from the table’s schema.
      transform (str): Transform that is used to produce values to be sorted on from the source column.
                       This is the same transform as described in partition transforms.
      direction (SortDirection): Sort direction, that can only be either asc or desc.
      null_order (NullOrder): Null order that describes the order of null values when sorted. Can only be either nulls-first or nulls-last.
    """

    def __init__(
        self,
        source_id: Optional[int] = None,
        transform: Optional[Union[Transform[Any, Any], Callable[[IcebergType], Transform[Any, Any]]]] = None,
        direction: Optional[SortDirection] = None,
        null_order: Optional[NullOrder] = None,
        **data: Any,
    ):
        if source_id is not None:
            data["source-id"] = source_id
        if transform is not None:
            data["transform"] = transform
        if direction is not None:
            data["direction"] = direction
        if null_order is not None:
            data["null-order"] = null_order
        super().__init__(**data)

    @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
        if not values.get("null-order"):
            values["null-order"] = NullOrder.NULLS_FIRST if values["direction"] == SortDirection.ASC else NullOrder.NULLS_LAST
        return values

    source_id: int = Field(alias="source-id")
    transform: Annotated[  # type: ignore
        Transform,
        BeforeValidator(parse_transform),
        PlainSerializer(lambda c: str(c), return_type=str),  # pylint: disable=W0108
        WithJsonSchema({"type": "string"}, mode="serialization"),
    ] = Field()
    direction: SortDirection = Field()
    null_order: NullOrder = Field(alias="null-order")

    def __str__(self) -> str:
        """Return the string representation of the SortField class."""
        if isinstance(self.transform, IdentityTransform):
            # In the case of an identity transform, we can omit the transform
            return f"{self.source_id} {self.direction} {self.null_order}"
        else:
            return f"{self.transform}({self.source_id}) {self.direction} {self.null_order}"

__str__()

Return the string representation of the SortField class.

Source code in pyiceberg/table/sorting.py
def __str__(self) -> str:
    """Return the string representation of the SortField class."""
    if isinstance(self.transform, IdentityTransform):
        # In the case of an identity transform, we can omit the transform
        return f"{self.source_id} {self.direction} {self.null_order}"
    else:
        return f"{self.transform}({self.source_id}) {self.direction} {self.null_order}"

SortOrder

Bases: IcebergBaseModel

Describes how the data is sorted within the table.

Users can sort their data within partitions by columns to gain performance.

The order of the sort fields within the list defines the order in which the sort is applied to the data.

Parameters:

Name Type Description Default
fields List[SortField]

The fields how the table is sorted.

()

Other Parameters:

Name Type Description
order_id int

An unique id of the sort-order of a table.

Source code in pyiceberg/table/sorting.py
class SortOrder(IcebergBaseModel):
    """Describes how the data is sorted within the table.

    Users can sort their data within partitions by columns to gain performance.

    The order of the sort fields within the list defines the order in which the sort is applied to the data.

    Args:
      fields (List[SortField]): The fields how the table is sorted.

    Keyword Args:
      order_id (int): An unique id of the sort-order of a table.
    """

    order_id: int = Field(alias="order-id", default=INITIAL_SORT_ORDER_ID)
    fields: List[SortField] = Field(default_factory=list)

    def __init__(self, *fields: SortField, **data: Any):
        if fields:
            data["fields"] = fields
        super().__init__(**data)

    @property
    def is_unsorted(self) -> bool:
        return len(self.fields) == 0

    def __str__(self) -> str:
        """Return the string representation of the SortOrder class."""
        result_str = "["
        if self.fields:
            result_str += "\n  " + "\n  ".join([str(field) for field in self.fields]) + "\n"
        result_str += "]"
        return result_str

    def __repr__(self) -> str:
        """Return the string representation of the SortOrder class."""
        fields = f"{', '.join(repr(column) for column in self.fields)}, " if self.fields else ""
        return f"SortOrder({fields}order_id={self.order_id})"

__repr__()

Return the string representation of the SortOrder class.

Source code in pyiceberg/table/sorting.py
def __repr__(self) -> str:
    """Return the string representation of the SortOrder class."""
    fields = f"{', '.join(repr(column) for column in self.fields)}, " if self.fields else ""
    return f"SortOrder({fields}order_id={self.order_id})"

__str__()

Return the string representation of the SortOrder class.

Source code in pyiceberg/table/sorting.py
def __str__(self) -> str:
    """Return the string representation of the SortOrder class."""
    result_str = "["
    if self.fields:
        result_str += "\n  " + "\n  ".join([str(field) for field in self.fields]) + "\n"
    result_str += "]"
    return result_str