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
5 changes: 5 additions & 0 deletions pyiceberg/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ def _init_transient(self) -> None:
# Unpickled FileIOs start with an empty cache
self._read_cached = _cached_reader(self)

@property
def read_cached(self) -> Callable[[str], bytes] | None:
"""Return the function that reads files through the manifest cache, or None when the cache is disabled."""
return self._read_cached

@abstractmethod
def new_input(self, location: str) -> InputFile:
"""Get an InputFile instance to read bytes from the file at the given location.
Expand Down
4 changes: 2 additions & 2 deletions pyiceberg/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ def open(self, seekable: bool = True) -> InputStream:

def _open_metadata_file(io: FileIO, location: str) -> InputFile:
"""Open a metadata file, reading its bytes through the content cache when the FileIO has one."""
if io._read_cached is None:
if (read_cached := io.read_cached) is None:
return io.new_input(location)

return _MemoryInputFile(location, io._read_cached(location))
return _MemoryInputFile(location, read_cached(location))


class DataFileContent(int, Enum):
Expand Down
36 changes: 33 additions & 3 deletions tests/io/test_content_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Any, cast
from unittest.mock import patch

import pyarrow as pa
Expand Down Expand Up @@ -54,7 +55,7 @@ def metadata_file(tmp_path: Path) -> str:


def test_cache_disabled_by_default() -> None:
assert PyArrowFileIO()._read_cached is None
assert PyArrowFileIO().read_cached is None


def test_cache_reads_file_once(metadata_file: str) -> None:
Expand Down Expand Up @@ -173,6 +174,35 @@ def test_reads_are_not_cached_when_disabled(catalog: Catalog) -> None:
assert new_input.call_count == 2


class _PublicAttributesFileIO:
"""Forwards only the public attributes of a FileIO, like the wrapper Polars puts on the FileIO of a scan."""

def __init__(self, io: FileIO) -> None:
self._io = io

def __getattr__(self, name: str) -> Any:
if name.startswith("_"):
raise AttributeError(name)
return getattr(self._io, name)


@pytest.mark.parametrize("enabled, reads", [("false", 4), ("true", 2)])
def test_scan_is_planned_through_a_file_io_wrapper(catalog: Catalog, enabled: str, reads: int) -> None:
catalog.create_namespace("default")
table = catalog.create_table("default.wrapped_file_io", schema=pa.schema([pa.field("x", pa.int64())]))
table.append(pa.table({"x": [1, 2, 3]}))

# Planning twice reads the manifest list and the manifest twice each, or once each through the cache
io = PyArrowFileIO({IO_MANIFEST_CACHE_ENABLED: enabled})
scan = table.scan()
scan.io = cast(FileIO, _PublicAttributesFileIO(io))
with patch.object(io, "new_input", side_effect=io.new_input) as new_input:
for _ in range(2):
assert len(list(scan.plan_files())) == 1

assert new_input.call_count == reads


def test_cache_properties_are_read_from_table_properties(catalog: Catalog) -> None:
catalog.create_namespace("default")
catalog.create_table(
Expand All @@ -185,7 +215,7 @@ def test_cache_properties_are_read_from_table_properties(catalog: Catalog) -> No
table = catalog.load_table("default.cache_properties")
snapshot = table.current_snapshot()
assert snapshot is not None
assert table.io._read_cached is not None
assert table.io.read_cached is not None

with patch.object(table.io, "new_input", side_effect=table.io.new_input) as new_input:
snapshot.manifests(table.io)
Expand All @@ -196,4 +226,4 @@ def test_cache_properties_are_read_from_table_properties(catalog: Catalog) -> No

@pytest.mark.parametrize("enabled", ["false", "0", ""])
def test_cache_stays_disabled_when_property_is_not_truthy(enabled: str) -> None:
assert PyArrowFileIO({IO_MANIFEST_CACHE_ENABLED: enabled})._read_cached is None
assert PyArrowFileIO({IO_MANIFEST_CACHE_ENABLED: enabled}).read_cached is None
Loading