diff --git a/pyiceberg/table/snapshots.py b/pyiceberg/table/snapshots.py index b21a0f5613..b8d4073bd4 100644 --- a/pyiceberg/table/snapshots.py +++ b/pyiceberg/table/snapshots.py @@ -24,9 +24,10 @@ from pydantic import Field, PrivateAttr, model_serializer from pyiceberg.io import FileIO -from pyiceberg.manifest import DataFile, DataFileContent, ManifestFile, read_manifest_list +from pyiceberg.manifest import DataFile, DataFileContent, ManifestFile from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec from pyiceberg.schema import Schema +from pyiceberg.utils.manifest_util import fetch_manifests if TYPE_CHECKING: from pyiceberg.table.metadata import TableMetadata @@ -248,10 +249,8 @@ def __str__(self) -> str: return result_str def manifests(self, io: FileIO) -> List[ManifestFile]: - if self.manifest_list is not None: - file = io.new_input(self.manifest_list) - return list(read_manifest_list(file)) - return [] + """Return the manifests for the given snapshot.""" + return fetch_manifests(io, self.manifest_list) class MetadataLogEntry(IcebergBaseModel): diff --git a/pyiceberg/utils/manifest_util.py b/pyiceberg/utils/manifest_util.py new file mode 100644 index 0000000000..c4dbc243ef --- /dev/null +++ b/pyiceberg/utils/manifest_util.py @@ -0,0 +1,14 @@ +from functools import lru_cache +from typing import List, Optional + +from pyiceberg.io import FileIO +from pyiceberg.manifest import ManifestFile, read_manifest_list + + +@lru_cache +def fetch_manifests(io: FileIO, manifest_list: Optional[str]) -> List[ManifestFile]: + """Return the manifests for the given snapshot.""" + if manifest_list not in (None, ""): + file = io.new_input(manifest_list) # type: ignore + return list(read_manifest_list(file)) + return []