Repository navigation
IO: Delete the exact file path instead of expanding globs - #4512
Anurag-S1ngh wants to merge 2 commits into
Conversation
FsspecFileIO.delete used fs.rm, which expands glob characters in the path. PyArrowFileIO.delete parsed the location as a URL, dropping ?query and #fragment from the object key. Delete the literal file in both backends.
Fokko
left a comment
There was a problem hiding this comment.
Thanks for picking this up! The rm -> rm_file change looks good to me, but I have some concerns about the ?/# handling. Could we split this: land the glob fix with the local test on its own, and take the URI semantics to a separate discussion?
| scheme, netloc, path = self.parse_location(str_location, self.properties) | ||
| fs = self.fs_by_scheme(scheme, netloc) | ||
|
|
||
| if not _is_local_path(str_location): |
There was a problem hiding this comment.
_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}" | ||
|
|
There was a problem hiding this comment.
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.
| uri = urlparse(str_location) | ||
| fs = self._get_fs_from_uri(uri, str_location) | ||
| fs.rm(str_location) | ||
| fs.rm_file(str_location) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Kept this PR to the glob fix (rm to rm_file plus the local test). The s3fs/gcsfs/adlfs versionId behavior goes with the parse_location discussion, so it is not part of this change.
| 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) |
There was a problem hiding this comment.
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).
|
|
||
| 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"): |
There was a problem hiding this comment.
Nit: could you use @pytest.mark.parametrize here instead of a loop?
The query and fragment handling only covered PyArrowFileIO.delete while parse_location still strips them on write, so a location could be written and deleted under different keys. Split that out; this change only stops fsspec from expanding glob characters in the file name.
|
Reduced the PR to the glob fix. The |
Rationale for this change
Part of #4504.
FsspecFileIO.deleteusedfs.rm, which expands*,?and[...]as glob patterns. Deletingdata-[12].parquetalso removeddata-1.parquetanddata-2.parquet. It now usesfs.rm_file, which deletes the literal path.The PyArrow query and fragment handling is split out.
parse_locationstrips them on write as well, so changing delete alone would leave the two operations disagreeing.Are these changes tested?
Yes. Added a test that deleting a local file with a
[...]name leaves its sibling files untouched.tests/io/test_fsspec.py tests/io/test_pyarrow.py -m unit -k delete: 25 passed.Are there any user-facing changes?
No API changes. Delete now targets exactly the named file.