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
36 changes: 35 additions & 1 deletion 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 @@ -883,6 +883,40 @@ def fetch_manifest_entry(self, io: FileIO, discard_deleted: bool = True) -> list
if not discard_deleted or entry.status != ManifestEntryStatus.DELETED
]

def prune_manifest_entry(
self, io: FileIO, partition_filter: Callable[[DataFile], bool], metrics_evaluator: Callable[[DataFile], bool]
) -> list[ManifestEntry]:
"""
Read manifest entries, applying partition and metrics evaluator during deserialization.

Unlike fetch_manifest_entry followed by a separate filer pass, this fuses filtering
into the deserialization loop, avoiding the intermediate list allocation for
non-matching entries.

Args:
io: The FileIO to fetch the file.
partition_filter: Evaluates the entry's partition data.
metrics_evaluator: Evaluates the entry's column-level metrics.

Returns:
An Iterator of manifest entries matching both filters.
"""
input_file = io.new_input(self.manifest_path)
with AvroFile[ManifestEntry](
input_file,
MANIFEST_ENTRY_SCHEMAS[DEFAULT_READ_VERSION],
read_types={-1: ManifestEntry, 2: DataFile},
read_enums={0: ManifestEntryStatus, 101: FileFormat, 134: DataFileContent},
) as reader:
result = []
for entry in reader:
if entry.status != ManifestEntryStatus.DELETED:
_inherit_from_manifest(entry, self)
if partition_filter(entry.data_file) and metrics_evaluator(entry.data_file):
result.append(entry)

return result

def __eq__(self, other: Any) -> bool:
"""Return the equality of two instances of the ManifestFile class."""
return self.manifest_path == other.manifest_path if isinstance(other, ManifestFile) else False
Expand Down
6 changes: 1 addition & 5 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2151,11 +2151,7 @@ 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.

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.prune_manifest_entry(io, partition_filter, metrics_evaluator)


def _min_sequence_number(manifests: list[ManifestFile]) -> int:
Expand Down