Skip to content

FileIO must use absolute paths. #2248

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions pyiceberg/io/fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ def new_input(self, location: str) -> FsspecInputFile:
FsspecInputFile: An FsspecInputFile instance for the given location.
"""
uri = urlparse(location)
if uri.scheme in ("", "file"):
path_to_check = uri.path if uri.scheme else location
if not os.path.isabs(path_to_check):
raise ValueError(f"FileIO implementation for local files requires absolute paths: {location}")
fs = self.get_fs(uri.scheme)
return FsspecInputFile(location=location, fs=fs)

Expand All @@ -364,6 +368,10 @@ def new_output(self, location: str) -> FsspecOutputFile:
FsspecOutputFile: An FsspecOutputFile instance for the given location.
"""
uri = urlparse(location)
if uri.scheme in ("", "file"):
path_to_check = uri.path if uri.scheme else location
if not os.path.isabs(path_to_check):
raise ValueError(f"FileIO implementation for local files requires absolute paths: {location}")
fs = self.get_fs(uri.scheme)
return FsspecOutputFile(location=location, fs=fs)

Expand Down
8 changes: 7 additions & 1 deletion pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,13 @@ def parse_location(location: str) -> Tuple[str, str, str]:
"""Return the path without the scheme."""
uri = urlparse(location)
if not uri.scheme:
return "file", uri.netloc, os.path.abspath(location)
if not os.path.isabs(location):
raise ValueError(f"FileIO implementation for local files requires absolute paths: {location}")
return "file", uri.netloc, location
elif uri.scheme == "file":
if not os.path.isabs(uri.path):
raise ValueError(f"FileIO implementation for local files requires absolute paths: {location}")
return uri.scheme, uri.netloc, uri.path
elif uri.scheme in ("hdfs", "viewfs"):
return uri.scheme, uri.netloc, uri.path
else:
Expand Down
11 changes: 10 additions & 1 deletion tests/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def test_output_file_to_input_file() -> None:
@pytest.mark.parametrize(
"string_uri",
[
"foo/bar/baz.parquet",
"file:/foo/bar/baz.parquet",
"file:/foo/bar/baz.parquet",
],
Expand All @@ -185,6 +184,16 @@ def test_custom_file_io_locations(string_uri: str) -> None:
output_file = file_io.new_output(location=string_uri)
assert output_file.location == string_uri

def test_custom_file_io_location_relative_path() -> None:
string_uri = "foo/bar/baz.parquet"
# Instantiate the file-io and create a new input and output file
file_io = PyArrowFileIO()
with pytest.raises(ValueError) as exc_info:
file_io.new_input(location=string_uri)

assert "FileIO implementation for local files requires absolute paths" in str(exc_info.value)



def test_deleting_local_file_using_file_io() -> None:
"""Test deleting a local file using FileIO.delete(...)"""
Expand Down
Loading