Skip to content
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
2 changes: 1 addition & 1 deletion pyiceberg/io/fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def delete(self, location: str | InputFile | OutputFile) -> None:

uri = urlparse(str_location)
fs = self._get_fs_from_uri(uri, str_location)
fs.rm(str_location)
fs.rm_file(str_location)

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.

This fixes the glob expansion, but not ?/#. s3fs split_path does keypart.partition("?versionId=") regardless of version_aware, so rm_file("s3://b/data.parquet?versionId=1") still deletes data.parquet. gcsfs (when version_aware) treats a numeric # or ?generation= as a generation, and adlfs parses ?versionid=.

Also a small behavior change: rm raised FileNotFoundError for a missing path through expand_path, while rm_file on S3 is a silent delete_object. Probably fine for the callers, but worth noting.


def _get_fs_from_uri(self, uri: "ParseResult", location: str = "") -> AbstractFileSystem:
"""Get a filesystem from a parsed URI, using hostname for ADLS account resolution."""
Expand Down
7 changes: 7 additions & 0 deletions pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,13 @@ def delete(self, location: str | InputFile | OutputFile) -> None:
scheme, netloc, path = self.parse_location(str_location, self.properties)
fs = self.fs_by_scheme(scheme, netloc)

if not _is_local_path(str_location):

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.

_is_local_path only returns true for Windows drive letters, so on POSIX a path without a scheme like /tmp/data#1.parquet also ends up here. parse_location already returns os.path.abspath(location) with the #1.parquet intact, so this appends it a second time: /tmp/data#1.parquet#1.parquet. The delete then raises FileNotFoundError.

parsed = urlparse(str_location)
if parsed.query:
path = f"{path}?{parsed.query}"
if parsed.fragment:
path = f"{path}#{parsed.fragment}"

Comment on lines +729 to +734

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.

new_input and new_output still go through parse_location, which strips ? and #. So writing to s3://b/data.parquet#1 creates data.parquet, and after this change deleting the same location targets data.parquet#1 and leaves the actual file orphaned. Java's S3URI also strips these. If we want to change this, I think it belongs in parse_location so all operations agree, and it's probably worth raising on the dev list.

try:
fs.delete_file(path)
except FileNotFoundError:
Expand Down
24 changes: 24 additions & 0 deletions tests/io/test_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ def test_fsspec_local_fs_can_create_path_without_parent_dir(fsspec_fileio: Fsspe
pytest.fail("Failed to write to file without parent directory")


def test_fsspec_delete_does_not_expand_glob(fsspec_fileio: FsspecFileIO) -> None:
"""Test delete removes only the named file when the name contains glob characters"""
with tempfile.TemporaryDirectory() as tmpdirname:
for name in ("data-1.parquet", "data-2.parquet", "data-[12].parquet"):
with open(f"{tmpdirname}/{name}", "wb") as f:
f.write(b"foo")

fsspec_fileio.delete(f"{tmpdirname}/data-[12].parquet")

assert sorted(os.listdir(tmpdirname)) == ["data-1.parquet", "data-2.parquet"]


def test_fsspec_delete_preserves_query_and_fragment(fsspec_fileio: FsspecFileIO) -> None:
"""Test delete passes the location to rm_file without parsing its query or fragment"""
fs = mock.Mock(spec=AbstractFileSystem)
location = "s3://bucket/data.parquet?versionId=1#fragment"

with mock.patch.object(fsspec_fileio, "_get_fs_from_uri", return_value=fs):
fsspec_fileio.delete(location)

fs.rm_file.assert_called_once_with(location)

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.

Since fs is mocked, this only checks that the string is passed through. With real s3fs this location would delete data.parquet (see the comment on fsspec.py).

fs.rm.assert_not_called()


def test_fsspec_get_fs_instance_per_thread_caching(fsspec_fileio: FsspecFileIO) -> None:
"""Test that filesystem instances are cached per-thread by `FsspecFileIO.get_fs`"""
fs_instances: list[AbstractFileSystem] = []
Expand Down
12 changes: 12 additions & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,18 @@ def test_deleting_hdfs_file_not_found() -> None:
assert "Cannot delete file, does not exist:" in str(exc_info.value)


def test_deleting_s3_file_preserves_query_and_fragment() -> None:
"""Test delete keeps a query or fragment as part of the object key instead of dropping it"""
for name in ("data.parquet?versionId=1", "data.parquet#1"):

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 instead of a loop?

s3fs_mock = MagicMock()

with patch.object(PyArrowFileIO, "_initialize_fs") as submocked:
submocked.return_value = s3fs_mock
PyArrowFileIO().delete(f"s3://foo/{name}")

s3fs_mock.delete_file.assert_called_once_with(f"foo/{name}")


def test_pyarrow_s3_session_properties() -> None:
session_properties: Properties = {
"s3.endpoint": "http://localhost:9000",
Expand Down
Loading