Skip to content

ENH: use native filesystem (if available) for read_parquet with pyarrow engine #51609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Mar 14, 2023

Conversation

mroeschke
Copy link
Member

@mroeschke mroeschke commented Feb 24, 2023

@mroeschke mroeschke added IO Parquet parquet, feather Arrow pyarrow functionality labels Feb 24, 2023
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks for reviving this!

try:
fs_arrow = import_optional_dependency("fsspec.implementations.arrow")
fs, path_or_handle = pa_fs.FileSystem.from_uri(path)
fs = fs_arrow.ArrowFSWrapper(fs)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason we need to wrap it here? Are we using some fsspec APIs? From looking at the code, it seems we just pass this object through to pyarrow as filesystem keyword? In that case, we can (and should) pass the actual pyarrow filesystem .

(in pyarrow, we should probably unwrap such an fsspec filesystem that wraps a pyarrow filesystem, but at the moment we don't do that, we just see it as a generic fsspec filesystem)

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking if there is a future fix/enhancement that expects the filesystem to be fsspec-like that this would be convenient, but it isn't needed particularly now. We can add that if needed in the future

(I was under the impression that ArrowFSWrapper would be interpreted as an pyarrow filesystem since that's the backend from https://arrow.apache.org/docs/python/filesystems.html#using-arrow-filesystems-with-fsspec)

Copy link
Member

Choose a reason for hiding this comment

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

(I was under the impression that ArrowFSWrapper would be interpreted as an pyarrow filesystem since that's the backend from https://arrow.apache.org/docs/python/filesystems.html#using-arrow-filesystems-with-fsspec)

That section is for if you want to use a pyarrow filesystem in an fsspec context (for example for passing it to a function that expects an fsspec filesystem, or to use the richer API it provives). But in this case we are not in such a context, since we are passing the filesystem to a pyarrow function.

It doesn't really say anything about then using that wrapper in a pyarrow context, but as mentioned above, we can certainly unwrap this inside pyarrow, avoiding the confusion / wrong expectation you just had (but so currently we don't do that yet).

Copy link
Member

Choose a reason for hiding this comment

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

@@ -417,6 +456,12 @@ def to_parquet(

.. versionadded:: 1.2.0

filesystem : fsspec or pyarrow filesystem, default None
Filesystem object to use when reading the parquet file. Only implemented
for ``engine-"pyarrow"``.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for ``engine-"pyarrow"``.
for ``engine="pyarrow"``.

@@ -504,6 +557,12 @@ def read_parquet(

.. versionadded:: 2.0.0

filesystem : fsspec or pyarrow filesystem, default None
Filesystem object to use when reading the parquet file. Only implemented
for ``engine-"pyarrow"``.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for ``engine-"pyarrow"``.
for ``engine="pyarrow"``.

@staticmethod
def from_uri(path):
to_local = path.replace("gs", "file")
return pa_fs.LocalFileSystem.from_uri(to_local)[0], to_local
Copy link
Member

Choose a reason for hiding this comment

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

Could we somehow also assert that this is being called? To actually ensure we get here, because if not (and it's still using fsspec), the tests still pass I assume.
(eg define a list that gets appended with some value inside this function, and yield that from this fixture, so we can see that it was appended after calling read_parquet. Not sure if that's worth the complexity)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. I added a print statement and validated that the message was printed with capsys

@mroeschke mroeschke added this to the 2.1 milestone Mar 11, 2023
)
elif pa_fs is not None and isinstance(fs, pa_fs.FileSystem) and storage_options:
raise NotImplementedError(
"storage_options not supported with a pyarrow Filesystem."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"storage_options not supported with a pyarrow Filesystem."
"storage_options not supported with a pyarrow FileSystem."

elif pa_fs is not None and isinstance(fs, pa_fs.FileSystem) and storage_options:
raise NotImplementedError(
"storage_options not supported with a pyarrow Filesystem."
)
if is_fsspec_url(path_or_handle) and fs is None:
fsspec = import_optional_dependency("fsspec")
Copy link
Member

Choose a reason for hiding this comment

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

You could move this import a bit lower to where fsspec is used? (so we don't try to import it if we already have a pyarrow filesystem)

@mroeschke mroeschke merged commit 0359f17 into pandas-dev:main Mar 14, 2023
@mroeschke mroeschke deleted the enh/parquet/arrow_fs branch March 14, 2023 21:14
@jorisvandenbossche
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality IO Parquet parquet, feather
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants