Repository navigation
Fix Table.to_polars() after the manifest cache change - #4610
Open
rahulsmahadev wants to merge 2 commits into
Open
rahulsmahadev wants to merge 2 commits into
rahulsmahadev wants to merge 2 commits into
Conversation
Since apache#4527, Table.to_polars().collect() raised AttributeError: _read_cached, because Polars plans the scan through a FileIO wrapper that forwards only public attributes. Read the cached reader through a new read-only FileIO.read_cached property. Co-authored-by: Isaac <no-reply@databricks.com>
Fokko
reviewed
Dec 4, 2026
Fokko
left a comment
Contributor
There was a problem hiding this comment.
Thanks @rahulsmahadev! Great find and a nice reproduction. Verified locally that the new test fails on main (AttributeError: _read_cached) and passes with this change, and the suite is green. Making read_cached public LGTM, it restores a public accessor like the old content_cache property. One nit inline :)
Comment on lines
+189
to
+203
| def test_scan_is_planned_through_a_file_io_wrapper(catalog: Catalog) -> 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 | ||
| for enabled, reads in (("false", 4), ("true", 2)): | ||
| 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, enabled |
Contributor
There was a problem hiding this comment.
Nit: could you use @pytest.mark.parametrize here, like test_cache_stays_disabled_when_property_is_not_truthy below?
Suggested change
| def test_scan_is_planned_through_a_file_io_wrapper(catalog: Catalog) -> 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 | |
| for enabled, reads in (("false", 4), ("true", 2)): | |
| 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, enabled | |
| @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 |
Contributor
Author
Co-authored-by: Isaac <no-reply@databricks.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
Bug: since #4527,
Table.to_polars().collect()fails on any table with a snapshot whenpolars_icebergis not installed (pyiceberg[polars]does not install it). Polars (2.1.0 and 2.2.0) then plans the scan with PyIceberg, and its metadata cache, on by default, wraps the scan's FileIO in aCachingFileIOthat forwards only public attributes._open_metadata_filenow reads the privateio._read_cached, socollect()raises while reading the manifest list:Before #4527 it read the public
io.content_cacheproperty, which the wrapper forwards.Fix: keep the cached reader from #4527 (Fokko's
cachetoolsdesign) as the transient_read_cached, and read it in_open_metadata_filethrough a new read-onlyFileIO.read_cachedproperty. With the cache disabled it still callsnew_input()on the FileIO it is given, so Polars' own metadata cache keeps working. Withio.manifest.cache-enabled=true, PyIceberg's cache reads its misses through the wrapped FileIO, so Polars' cache stays empty and a reloaded table reads the files again. Before #4527 misses went through the wrapper, as Java'sContentCache.tryCache()reads through the caller's FileIO.This makes the cached reader public API. Is that OK, or would you rather keep it internal with
getattr(io, "_read_cached", None)in_open_metadata_file? That leaves PyIceberg's cache off behind such wrappers, and Polars' wrapper then caches the reads itself.Java keeps the content cache in
ManifestFiles, keyed by the FileIO, and uses only FileIO's publicnewInputFile()andproperties(), so it has no such failure. PyIceberg keeps the cache on the FileIO (since #4335), hence the accessor.Are these changes tested?
Yes,
test_scan_is_planned_through_a_file_io_wrapperintests/io/test_content_cache.pyplans a scan through a wrapper that forwards only public attributes, withio.manifest.cache-enabledoff and on. It fails on main withAttributeError: _read_cachedand passes with this change. The existing cache tests now read the property instead of_read_cached.Table.to_polars().collect()was also checked by hand with Polars 2.2.0: it fails on main and returns the rows with this change.Are there any user-facing changes?
No change for released versions, since #4527 is not released yet. New public API: a read-only
FileIO.read_cachedproperty.This pull request and its description were written by Isaac.