diff --git a/pyiceberg/io/__init__.py b/pyiceberg/io/__init__.py index c5c1ca932a..c3838d6584 100644 --- a/pyiceberg/io/__init__.py +++ b/pyiceberg/io/__init__.py @@ -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. diff --git a/pyiceberg/manifest.py b/pyiceberg/manifest.py index 3a2ed4f59b..75ab8fffa9 100644 --- a/pyiceberg/manifest.py +++ b/pyiceberg/manifest.py @@ -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): diff --git a/tests/io/test_content_cache.py b/tests/io/test_content_cache.py index 816fd23358..93df0bb8b1 100644 --- a/tests/io/test_content_cache.py +++ b/tests/io/test_content_cache.py @@ -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 @@ -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: @@ -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( @@ -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) @@ -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