Skip to content

Fix Table.to_polars() after the manifest cache change - #4610

Open
rahulsmahadev wants to merge 2 commits into
apache:mainfrom
rahulsmahadev:rahul/to-polars-manifest-cache
Open

rahulsmahadev wants to merge 2 commits into
apache:mainfrom
rahulsmahadev:rahul/to-polars-manifest-cache

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: since #4527, Table.to_polars().collect() fails on any table with a snapshot when polars_iceberg is 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 a CachingFileIO that forwards only public attributes. _open_metadata_file now reads the private io._read_cached, so collect() raises while reading the manifest list:

table.append(pa.table({"x": [1, 2, 3]}))
table.to_polars().collect()
# AttributeError: _read_cached

Before #4527 it read the public io.content_cache property, which the wrapper forwards.

Fix: keep the cached reader from #4527 (Fokko's cachetools design) as the transient _read_cached, and read it in _open_metadata_file through a new read-only FileIO.read_cached property. With the cache disabled it still calls new_input() on the FileIO it is given, so Polars' own metadata cache keeps working. With io.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's ContentCache.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 public newInputFile() and properties(), 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_wrapper in tests/io/test_content_cache.py plans a scan through a wrapper that forwards only public attributes, with io.manifest.cache-enabled off and on. It fails on main with AttributeError: _read_cached and 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_cached property.

This pull request and its description were written by Isaac.

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 Fokko left a comment

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.

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 thread tests/io/test_content_cache.py Outdated
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

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.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Fokko, done in 27ce8d3.

Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants