vinjai commented on code in PR #871: URL: https://github.com/apache/iceberg-python/pull/871#discussion_r1667347220
########## pyiceberg/utils/arrow_sorting.py: ########## @@ -0,0 +1,110 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# pylint:disable=redefined-outer-name +from typing import List, Tuple + +import pyarrow as pa + +from pyiceberg.table.sorting import NullOrder, SortDirection, SortField + + +class PyArrowSortOptions: + sort_direction: str + null_order: str + + def __init__(self, sort_direction: str = "ascending", null_order: str = "at_end"): + if sort_direction not in ("ascending", "descending"): + raise ValueError('Sort Direction should be one of ["ascending","descending"]') + if null_order not in ("at_start", "at_end"): + raise ValueError('Sort Null Order should be one of ["at_start","at_end"]') + + self.sort_direction = sort_direction + self.null_order = null_order + + +def convert_sort_field_to_pyarrow_sort_options(sort_field: SortField) -> PyArrowSortOptions: + """ + Convert an Iceberg Table Sort Field to Arrow Sort Options. + + Args: + sort_field (SortField): Source Iceberg Sort Field to be converted + + Returns: + PyArrowOptions: PyArrowOptions format for the input Sort Field + """ + pyarrow_sort_direction = {SortDirection.ASC: "ascending", SortDirection.DESC: "descending"} + pyarrow_null_ordering = {NullOrder.NULLS_LAST: "at_end", NullOrder.NULLS_FIRST: "at_start"} + return PyArrowSortOptions( + pyarrow_sort_direction.get(sort_field.direction, "ascending"), + pyarrow_null_ordering.get(sort_field.null_order, "at_end"), + ) + + +def get_sort_indices_arrow_table(arrow_table: pa.Table, sort_seq: List[Tuple[str, PyArrowSortOptions]]) -> List[int]: Review Comment: Just wanted to clarify on the separate implementation for sort_indices other than the one provided by pyarrow. This is because pyarrow sort_indices or Sort Options only supports one order for null placement across keys. More details here: - https://arrow.apache.org/docs/python/generated/pyarrow.compute.sort_indices.html - https://arrow.apache.org/docs/python/generated/pyarrow.compute.SortOptions.html While, the iceberg spec doesn't discriminate of having multiple null ordering across keys: https://iceberg.apache.org/spec/#sort-orders This function specifically helps to implement the above functionality by sorting across keys and utilizing the stable nature of the sort_indices algo from pyarrow. ---- We can raise another issue to improve the performance of this function. ----- In future, if pyarrow sort_indices does support different null ordering across, we can mark this function as obsolete and keep the implementation clean in the iceberg table append and overwrite methods. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org