You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "geruh (via GitHub)" <gi...@apache.org> on 2026/01/16 03:21:16 UTC
[PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
geruh opened a new pull request, #2918:
URL: https://github.com/apache/iceberg-python/pull/2918
Related to #2255.
# Rationale for this change
This PR is a piece of the existing DFI PR in #2255. However, this rips out the existing delete->data matching behavior for deletes and indexes them for efficient lookup.
The previous implementation:
1. Scanned all delete files with sequence number >= data file's sequence number
2. Created a new `_InclusiveMetricsEvaluator` instance for each data file
3. Evaluated every candidate delete file against the data file's path
Now we extend this workflow with a `DeleteFileIndex` that:
- INdexes path specific DVs
- Indexes partition-scoped deletes by (spec_id, partition record)
- Uses bisect_left for sequence number filtering
This aligns with the Java implementation of the [DeleteFileIndex](https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/DeleteFileIndex.java), following the python infra.
## Are these changes tested?
New tests added and existing tests continue to pass
## Are there any user-facing changes?
No
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "kevinjqliu (via GitHub)" <gi...@apache.org>.
kevinjqliu commented on code in PR #2918:
URL: https://github.com/apache/iceberg-python/pull/2918#discussion_r2722180353
##########
pyiceberg/table/__init__.py:
##########
@@ -33,7 +33,6 @@
)
from pydantic import Field
-from sortedcontainers import SortedList
Review Comment:
https://github.com/apache/iceberg-python/issues/2945
##########
pyiceberg/table/__init__.py:
##########
@@ -33,7 +33,6 @@
)
from pydantic import Field
-from sortedcontainers import SortedList
Review Comment:
tracking in https://github.com/apache/iceberg-python/issues/2945
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "kevinjqliu (via GitHub)" <gi...@apache.org>.
kevinjqliu commented on PR #2918:
URL: https://github.com/apache/iceberg-python/pull/2918#issuecomment-3787073960
Thanks @geruh and thank you @Fokko @jayceslesar @.copilot for the review
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2918:
URL: https://github.com/apache/iceberg-python/pull/2918#discussion_r2717330847
##########
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)
+ return [delete_file for delete_file, _ in self._files[start_idx:]]
+
+
+def _has_path_bounds(delete_file: DataFile) -> bool:
+ lower = delete_file.lower_bounds
+ upper = delete_file.upper_bounds
+ if not lower or not upper:
+ return False
+
+ return PATH_FIELD_ID in lower and PATH_FIELD_ID in upper
+
+
+def _applies_to_data_file(delete_file: DataFile, data_file: DataFile) -> bool:
+ if not _has_path_bounds(delete_file):
+ return True
+
+ evaluator = _InclusiveMetricsEvaluator(POSITIONAL_DELETE_SCHEMA, EqualTo("file_path", data_file.file_path))
+ return evaluator.eval(delete_file)
+
+
+def _referenced_data_file_path(delete_file: DataFile) -> str | None:
+ """Return the path, if the path bounds evaluate to the same location."""
+ lower_bounds = delete_file.lower_bounds
+ upper_bounds = delete_file.upper_bounds
+
+ if not lower_bounds or not upper_bounds:
+ return None
+
+ lower = lower_bounds.get(PATH_FIELD_ID)
+ upper = upper_bounds.get(PATH_FIELD_ID)
+
+ if lower and upper and lower == upper:
+ try:
+ return lower.decode("utf-8")
+ except (UnicodeDecodeError, AttributeError):
+ pass
+
+ return None
+
+
+def _partition_key(spec_id: int, partition: Record | None) -> tuple[int, tuple[Any, ...]]:
+ if partition:
+ return spec_id, tuple(partition._data)
Review Comment:
Ideally we want to keep it a `Record`, instead of converting (and copying) this to Tuple.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2918:
URL: https://github.com/apache/iceberg-python/pull/2918#discussion_r2717372566
##########
pyiceberg/table/__init__.py:
##########
@@ -33,7 +33,6 @@
)
from pydantic import Field
-from sortedcontainers import SortedList
Review Comment:
Unrelated to this PR, but I noticed that there is just one more occurrence of `sortedcontainers` outside of the tests. Might be interesting to see if we can get rid of it.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "kevinjqliu (via GitHub)" <gi...@apache.org>.
kevinjqliu merged PR #2918:
URL: https://github.com/apache/iceberg-python/pull/2918
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "jayceslesar (via GitHub)" <gi...@apache.org>.
jayceslesar commented on code in PR #2918:
URL: https://github.com/apache/iceberg-python/pull/2918#discussion_r2701217045
##########
pyiceberg/table/__init__.py:
##########
@@ -1857,29 +1856,19 @@ def _min_sequence_number(manifests: list[ManifestFile]) -> int:
return INITIAL_SEQUENCE_NUMBER
-def _match_deletes_to_data_file(data_entry: ManifestEntry, positional_delete_entries: SortedList[ManifestEntry]) -> set[DataFile]:
- """Check if the delete file is relevant for the data file.
-
- Using the column metrics to see if the filename is in the lower and upper bound.
+def _match_deletes_to_data_file(data_entry: ManifestEntry, delete_file_index: DeleteFileIndex) -> set[DataFile]:
+ """Check if delete files are relevant for the data file.
Review Comment:
Do we even need this function anymore?
##########
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)
+ return [delete_file for delete_file, _ in self._files[start_idx:]]
+
+
+def _has_path_bounds(delete_file: DataFile) -> bool:
+ lower = delete_file.lower_bounds
+ upper = delete_file.upper_bounds
+ if not lower or not upper:
+ return False
+
+ return PATH_FIELD_ID in lower and PATH_FIELD_ID in upper
+
+
+def _applies_to_data_file(delete_file: DataFile, data_file: DataFile) -> bool:
+ if not _has_path_bounds(delete_file):
+ return True
+
+ evaluator = _InclusiveMetricsEvaluator(POSITIONAL_DELETE_SCHEMA, EqualTo("file_path", data_file.file_path))
+ return evaluator.eval(delete_file)
+
+
+def _referenced_data_file_path(delete_file: DataFile) -> str | None:
+ """Return the path, if the path bounds evaluate to the same location."""
+ lower_bounds = delete_file.lower_bounds
+ upper_bounds = delete_file.upper_bounds
+
+ if not lower_bounds or not upper_bounds:
+ return None
+
+ lower = lower_bounds.get(PATH_FIELD_ID)
+ upper = upper_bounds.get(PATH_FIELD_ID)
+
+ if lower and upper and lower == upper:
+ try:
+ return lower.decode("utf-8")
+ except (UnicodeDecodeError, AttributeError):
+ pass
Review Comment:
consider using `contextlib.suppress` here instead of the except pass
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2918:
URL: https://github.com/apache/iceberg-python/pull/2918#discussion_r2717374795
##########
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:
Nice!
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "kevinjqliu (via GitHub)" <gi...@apache.org>.
kevinjqliu commented on code in PR #2918:
URL: https://github.com/apache/iceberg-python/pull/2918#discussion_r2717947622
##########
pyiceberg/table/__init__.py:
##########
@@ -33,7 +33,6 @@
)
from pydantic import Field
-from sortedcontainers import SortedList
Review Comment:
https://github.com/apache/iceberg-python/blob/26ecfe77ffc9525e43a180c52b4ab49e76e66cb4/pyiceberg/table/update/snapshot.py#L798
last occurrence
##########
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:
note the old solution uses `bisect_right`, which might have been a bug
##########
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:
might be worth it to add an integration test scenario for this
##########
tests/table/test_delete_file_index.py:
##########
@@ -0,0 +1,192 @@
+# 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.
+import pytest
+
+from pyiceberg.manifest import DataFile, DataFileContent, FileFormat, ManifestEntry, ManifestEntryStatus
+from pyiceberg.table.delete_file_index import PATH_FIELD_ID, DeleteFileIndex, PositionDeletes
+from pyiceberg.typedef import Record
+
+
+def _create_data_file(file_path: str = "s3://bucket/data.parquet", spec_id: int = 0) -> DataFile:
+ data_file = DataFile.from_args(
+ content=DataFileContent.DATA,
+ file_path=file_path,
+ file_format=FileFormat.PARQUET,
+ partition=Record(),
+ record_count=100,
+ file_size_in_bytes=1000,
+ )
+ data_file._spec_id = spec_id
+ return data_file
+
+
+def _create_positional_delete(
+ sequence_number: int = 1, file_path: str = "s3://bucket/data.parquet", spec_id: int = 0
+) -> ManifestEntry:
+ delete_file = DataFile.from_args(
+ content=DataFileContent.POSITION_DELETES,
+ file_path=f"s3://bucket/pos-delete-{sequence_number}.parquet",
+ file_format=FileFormat.PARQUET,
+ partition=Record(),
+ record_count=10,
+ file_size_in_bytes=100,
+ lower_bounds={PATH_FIELD_ID: file_path.encode()},
+ upper_bounds={PATH_FIELD_ID: file_path.encode()},
+ )
+ delete_file._spec_id = spec_id
+ return ManifestEntry.from_args(status=ManifestEntryStatus.ADDED, sequence_number=sequence_number, data_file=delete_file)
+
+
+def _create_partition_delete(sequence_number: int = 1, spec_id: int = 0, partition: Record | None = None) -> ManifestEntry:
+ delete_file = DataFile.from_args(
+ content=DataFileContent.POSITION_DELETES,
+ file_path=f"s3://bucket/pos-delete-{sequence_number}.parquet",
+ file_format=FileFormat.PARQUET,
+ partition=partition or Record(),
+ record_count=10,
+ file_size_in_bytes=100,
+ )
+ delete_file._spec_id = spec_id
+ return ManifestEntry.from_args(status=ManifestEntryStatus.ADDED, sequence_number=sequence_number, data_file=delete_file)
+
+
+def _create_deletion_vector(
+ sequence_number: int = 1, file_path: str = "s3://bucket/data.parquet", spec_id: int = 0
+) -> ManifestEntry:
+ delete_file = DataFile.from_args(
+ content=DataFileContent.POSITION_DELETES,
+ file_path=f"s3://bucket/deletion-vector-{sequence_number}.puffin",
+ file_format=FileFormat.PUFFIN,
+ partition=Record(),
+ record_count=10,
+ file_size_in_bytes=100,
+ lower_bounds={PATH_FIELD_ID: file_path.encode()},
+ upper_bounds={PATH_FIELD_ID: file_path.encode()},
+ )
+ delete_file._spec_id = spec_id
+ return ManifestEntry.from_args(status=ManifestEntryStatus.ADDED, sequence_number=sequence_number, data_file=delete_file)
Review Comment:
ha, might as well add it to the source code 😄 we can follow up with this
##########
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)
+ return [delete_file for delete_file, _ in self._files[start_idx:]]
+
+
+def _has_path_bounds(delete_file: DataFile) -> bool:
+ lower = delete_file.lower_bounds
+ upper = delete_file.upper_bounds
+ if not lower or not upper:
+ return False
+
+ return PATH_FIELD_ID in lower and PATH_FIELD_ID in upper
+
+
+def _applies_to_data_file(delete_file: DataFile, data_file: DataFile) -> bool:
+ if not _has_path_bounds(delete_file):
+ return True
+
+ evaluator = _InclusiveMetricsEvaluator(POSITIONAL_DELETE_SCHEMA, EqualTo("file_path", data_file.file_path))
+ return evaluator.eval(delete_file)
+
+
+def _referenced_data_file_path(delete_file: DataFile) -> str | None:
Review Comment:
a little awkward that `delete_file` is of type `DataFile`, we can refactor this later perhaps
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Add DeleteFileIndex to improve position delete lookup [iceberg-python]
Posted by "geruh (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org