geruh commented on code in PR #2918: URL: https://github.com/apache/iceberg-python/pull/2918#discussion_r2718382431
########## pyiceberg/table/delete_file_index.py: ########## @@ -0,0 +1,143 @@ +# 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 bisect import bisect_left +from typing import Any + +from pyiceberg.expressions import EqualTo +from pyiceberg.expressions.visitors import _InclusiveMetricsEvaluator +from pyiceberg.manifest import INITIAL_SEQUENCE_NUMBER, POSITIONAL_DELETE_SCHEMA, DataFile, ManifestEntry +from pyiceberg.typedef import Record + +PATH_FIELD_ID = 2147483546 + + +class PositionDeletes: + """Collects position delete files and indexes them by sequence number.""" + + __slots__ = ("_buffer", "_seqs", "_files") + + def __init__(self) -> None: + self._buffer: list[tuple[DataFile, int]] | None = [] + self._seqs: list[int] = [] + self._files: list[tuple[DataFile, int]] = [] + + def add(self, delete_file: DataFile, seq_num: int) -> None: + if self._buffer is None: + raise ValueError("Cannot add files after indexing") + self._buffer.append((delete_file, seq_num)) + + def _ensure_indexed(self) -> None: + if self._buffer is not None: + self._files = sorted(self._buffer, key=lambda file: file[1]) + self._seqs = [seq for _, seq in self._files] + self._buffer = None + + def filter_by_seq(self, seq: int) -> list[DataFile]: + self._ensure_indexed() + if not self._files: + return [] + start_idx = bisect_left(self._seqs, seq) Review Comment: Yess, The old `bisect_right` returned the the point to the right of the sorted sequences with the same key. This meant that any delete file with the same seq number was excluded. Per the Iceberg spec https://iceberg.apache.org/spec/#scan-planning: "The data file's data sequence number is _less than or equal to_ the delete file's data sequence number" This situation can occur when data files and position delete files are added in the same commit using the row delta logic i.e. a merge statement. Looking at a test on the java side like [this test](https://github.com/apache/iceberg/blob/main/core/src/test/java/org/apache/iceberg/DeleteFileIndexTestBase.java#L676). We can see Java is inclusive by returning the files with the same sequence number as part of the filter lookup. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
