Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions pyiceberg/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import math
import threading
from abc import ABC, abstractmethod
from collections.abc import Iterator
from collections.abc import Callable, Iterator
from copy import copy
from enum import Enum
from types import TracebackType
Expand Down Expand Up @@ -859,13 +859,19 @@ def has_added_files(self) -> bool:
def has_existing_files(self) -> bool:
return self.existing_files_count is None or self.existing_files_count > 0

def fetch_manifest_entry(self, io: FileIO, discard_deleted: bool = True) -> list[ManifestEntry]:
def fetch_manifest_entry(
self,
io: FileIO,
discard_deleted: bool = True,
entry_filter: Callable[[ManifestEntry], bool] | None = None,
) -> list[ManifestEntry]:
"""
Read the manifest entries from the manifest file.

Args:
io: The FileIO to fetch the file.
discard_deleted: Filter on live entries.
entry_filter: Optional predicate to filter manifest entries.

Returns:
An Iterator of manifest entries.
Expand All @@ -877,11 +883,17 @@ def fetch_manifest_entry(self, io: FileIO, discard_deleted: bool = True) -> list
read_types={-1: ManifestEntry, 2: DataFile},
read_enums={0: ManifestEntryStatus, 101: FileFormat, 134: DataFileContent},
) as reader:
return [
result = []

for entry in reader:
if discard_deleted and entry.status == ManifestEntryStatus.DELETED:
continue
_inherit_from_manifest(entry, self)
for entry in reader
if not discard_deleted or entry.status != ManifestEntryStatus.DELETED
]

if entry_filter is None or entry_filter(entry):
result.append(entry)

return result

def __eq__(self, other: Any) -> bool:
"""Return the equality of two instances of the ManifestFile class."""
Expand Down
8 changes: 3 additions & 5 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2151,11 +2151,9 @@ def _open_manifest(
Returns:
A list of ManifestEntry that matches the provided filters.
"""
return [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great find! Would it be better to incorporate this fused behavior directly into fetch_manifest_entry by allowing it to accept an entry_filter: Callable[[ManifestEntry], bool] | None arg? That way the optimization is generic and available to all callers that currently materialize the full list just to discard entries immediately.

The call site in _open_manifest would become:

return manifest.fetch_manifest_entry(
    io,
    discard_deleted=True,
    entry_filter=lambda e: partition_filter(e.data_file) and metrics_evaluator(e.data_file),
)

This avoids duplicating the Avro reader setup and _inherit_from_manifest call ordering between fetch_manifest_entry and prune_manifest_entry, and there are 6 other call sites that filter after materializing that could benefit from the same interface:

  1. table/__init__.py - _open_manifest (partition + metrics)
  2. table/inspect.py - _get_files_from_manifest (content type)
  3. table/update/snapshot.py - _OverwriteFiles._build_... (strict + inclusive metrics)
  4. table/update/validate.py - _deleted_data_files (snapshot_id + data_filter + partition_set)
  5. table/update/validate.py - _added_data_files (same)
  6. table/update/validate.py - _added_delete_files (same)

Using Callable[[ManifestEntry], bool] rather than Callable[[DataFile], bool] covers all of these since the validate callers filter on entry-level fields like snapshot_id and status, not just DataFile.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! I agree on both points.

My initial approach was a separate method to avoid any risk of side effects to existing callers but the optional entry_filter defaulting to None achieves the same safety without duplicating the Avro reader setup. I have updated the PR to include this but kept the other call site migration for a follow-up.

manifest_entry
for manifest_entry in manifest.fetch_manifest_entry(io, discard_deleted=True)
if partition_filter(manifest_entry.data_file) and metrics_evaluator(manifest_entry.data_file)
]
return manifest.fetch_manifest_entry(
io, discard_deleted=True, entry_filter=lambda e: partition_filter(e.data_file) and metrics_evaluator(e.data_file)
)


def _min_sequence_number(manifests: list[ManifestFile]) -> int:
Expand Down
28 changes: 28 additions & 0 deletions tests/utils/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,34 @@ def test_read_manifest_entry(generated_manifest_entry_file: str) -> None:
assert data_file.sort_order_id == 0


def test_fetch_manifest_entry_with_filter(generated_manifest_entry_file: str) -> None:
manifest = ManifestFile.from_args(
manifest_path=generated_manifest_entry_file,
manifest_length=0,
partition_spec_id=0,
added_snapshot_id=0,
sequence_number=0,
partitions=[],
)

all_entries = manifest.fetch_manifest_entry(PyArrowFileIO())
assert len(all_entries) == 2

# Entry 1 has tpep_pickup_day=1925 & entry 2 has tpep_pickup_day=None
matched = manifest.fetch_manifest_entry(
PyArrowFileIO(),
entry_filter=lambda e: e.data_file.partition[1] == 1925,
)
assert len(matched) == 1
assert matched[0].data_file.record_count == 19513

no_match = manifest.fetch_manifest_entry(
PyArrowFileIO(),
entry_filter=lambda e: e.data_file.partition[1] == 9999,
)
assert len(no_match) == 0


def test_read_manifest_list(generated_manifest_file_file_v1: str) -> None:
input_file = PyArrowFileIO().new_input(generated_manifest_file_file_v1)
manifest_list = list(read_manifest_list(input_file))[0]
Expand Down