Repository navigation
IO: Delete only the exact file path - #4512
Anurag-S1ngh wants to merge 1 commit 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.
| 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?
Rationale for this change
Closes #4504.
FileIO.deletecould delete a different file than the one named.FsspecFileIO.deleteusedfs.rm, which expands*,?and[...]as glob patterns on some filesystems. It now usesfs.rm_file, which deletes the literal path.PyArrowFileIO.deleteparsed the location as a URL, so?queryand#fragmentwere dropped and the shortened path was deleted. It now keeps them as part of the object key.Iceberg file paths are names, not patterns or URLs with fragments.
Are these changes tested?
Yes. Added a test that a local fsspec delete with a
[...]name leaves sibling files untouched, a test that fsspec passes the full location torm_file, and a test that PyArrow deletes the exact key for paths with?and#.tests/io/test_pyarrow.py tests/io/test_fsspec.py -m unit: 296 passed, 1 skipped.Are there any user-facing changes?
No API changes. Delete now targets exactly the named file.