JasperHG90 commented on code in PR #1500: URL: https://github.com/apache/iceberg-python/pull/1500#discussion_r2009213055
########## pyiceberg/table/update/sorting.py: ########## @@ -0,0 +1,117 @@ +# 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. +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, List, Tuple + +from pyiceberg.table.sorting import NullOrder, SortDirection, SortField, SortOrder +from pyiceberg.table.update import ( + AddSortOrderUpdate, + AssertDefaultSortOrderId, + SetDefaultSortOrderUpdate, + TableRequirement, + TableUpdate, + UpdatesAndRequirements, + UpdateTableMetadata, +) +from pyiceberg.transforms import Transform + +if TYPE_CHECKING: + from pyiceberg.table import Transaction + + +class UpdateSortOrder(UpdateTableMetadata["UpdateSortOrder"]): + _transaction: Transaction + _last_assigned_order_id: int + _case_sensitive: bool + _fields: List[SortField] + _last_sort_order_id: int + + def __init__(self, transaction: Transaction, case_sensitive: bool = True) -> None: + super().__init__(transaction) + self._fields: List[SortField] = [] + self._case_sensitive: bool = case_sensitive + self._last_sort_order_id: int = transaction.table_metadata.default_sort_order_id + + def _column_name_to_id(self, column_name: str) -> int: + """Map the column name to the column field id.""" + return ( + self._transaction.table_metadata.schema() + .find_field( + name_or_id=column_name, + case_sensitive=self._case_sensitive, + ) + .field_id + ) + + def _add_sort_field( + self, + source_id: int, + transform: Transform[Any, Any], + direction: SortDirection, + null_order: NullOrder, + ) -> UpdateSortOrder: + """Add a sort field to the sort order list.""" + self._fields.append( + SortField( + source_id=source_id, + transform=transform, + direction=direction, + null_order=null_order, + ) + ) + return self + + def asc(self, source_column_name: str, transform: Transform[Any, Any], null_order: NullOrder = NullOrder.NULLS_LAST) -> UpdateSortOrder: + """Add a sort field with ascending order.""" + return self._add_sort_field( + source_id=self._column_name_to_id(source_column_name), + transform=transform, + direction=SortDirection.ASC, + null_order=null_order, + ) + + def desc( + self, source_column_name: str, transform: Transform[Any, Any], null_order: NullOrder = NullOrder.NULLS_LAST + ) -> UpdateSortOrder: + """Add a sort field with descending order.""" + return self._add_sort_field( + source_id=self._column_name_to_id(source_column_name), + transform=transform, + direction=SortDirection.DESC, + null_order=null_order, + ) + + def _apply(self) -> SortOrder: + """Return the sort order.""" + return SortOrder(*self._fields, order_id=self._last_sort_order_id + 1) + + def _commit(self) -> UpdatesAndRequirements: Review Comment: Hi @kevinjqliu . I'm not following you: - `In particular, the java implementation tries to retry sort order whenever possible.` : I looked at the Java code, and with my limited ability to parse it I'm not sure what you mean here. If no sort order has been added, it throws an exception. Else it appears to do an incrementation based on all possible sort orders (see [here](https://github.com/apache/iceberg/blob/71493b92dc2e0b953c184f76ad76e7f8794da8b1/core/src/main/java/org/apache/iceberg/TableMetadata.java#L1763-L1779)). When would a user add a new sort order that matches an existing sort order exactly? - ` is correct since default_sort_order_id might be always be the highest sort order`: The 'highest' meaning the latest? Why is it a problem to take the value of the default one as the value to be incremented? This would be a problem if the default sort order _wasn't_ the highest one, right? -- 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