Skip to content

ArrowFSWrapper should not use "/" root_marker for all filesystems #1467

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

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 14 additions & 0 deletions fsspec/implementations/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ def protocol(self):
def fsid(self):
return "hdfs_" + tokenize(self.fs.host, self.fs.port)

@classmethod
def from_fs(cls, fs, **kwargs):
override = {"local": "file"}
type_name = override.get(fs.type_name, fs.type_name)

try:
fs.from_uri(f"{type_name}:///")
Copy link
Member

Choose a reason for hiding this comment

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

This is the foolproof way to know whether the first character can/should be "/"?

Copy link
Author

Choose a reason for hiding this comment

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

It definitely works for (3/4) Local, S3, and GCS. I'm not sure about Hadoop, I'm never used it and am not sure how to test locally.

https://arrow.apache.org/docs/python/filesystems.html#filesystem-interface

Copy link
Member

Choose a reason for hiding this comment

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

hadoop has a derived class anyway, HadoopFileSystem. We should make sure it has "/", and it doesn't need to go through the check in this PR.

root_marker = "/"
except BaseException:
root_marker = ""

wrapper = type(cls.__name__, (cls,), {"root_marker": root_marker})
Copy link
Member

Choose a reason for hiding this comment

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

So this is effectively a metaclass?

Copy link
Author

Choose a reason for hiding this comment

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

There's no metaclasses involved, but it works similarly yes. For such a simple case, metaclasses might be overkill, but maybe a top-level function that isn't attached to ArrowFSWrapper instead?

def wrap_arrow_fs(fs: pyarrow.fs.FileSystem, **kwargs) -> ArrowFSWrapper:
    # build class dynamically based on fs

Because it's a class attribute, I don't think we can get around creating different classes per arrow filesystem. An alternative to the dynamic class construction would be to have wrapper subclasses for each root_marker variant and a lookup table (dict) based on the provided arrow filesystem.

return wrapper(fs, **kwargs)

@classmethod
def _strip_protocol(cls, path):
ops = infer_storage_options(path)
Expand Down
14 changes: 14 additions & 0 deletions fsspec/implementations/tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,17 @@ def test_seekable(fs, remote_dir):
with fs.open(remote_dir + "/a.txt", "rb", seekable=False) as file:
with pytest.raises(OSError):
file.seek(5)


@pytest.mark.parametrize(
["filesystem", "root_marker"],
[
(pyarrow_fs.LocalFileSystem(), "/"),
(pyarrow_fs.S3FileSystem(), ""),
(pyarrow_fs.GcsFileSystem(), ""),
],
)
def test_from_fs(filesystem, root_marker):
wrapper = ArrowFSWrapper.from_fs(filesystem)
assert wrapper.root_marker == root_marker
assert wrapper.__class__.root_marker == root_marker