Fokko commented on code in PR #4512:
URL: https://github.com/apache/iceberg-python/pull/4512#discussion_r4732191515
##########
pyiceberg/io/pyarrow.py:
##########
@@ -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):
Review 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`.
##########
pyiceberg/io/fsspec.py:
##########
@@ -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)
Review 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.
##########
pyiceberg/io/pyarrow.py:
##########
@@ -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):
+ parsed = urlparse(str_location)
+ if parsed.query:
+ path = f"{path}?{parsed.query}"
+ if parsed.fragment:
+ path = f"{path}#{parsed.fragment}"
+
Review 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.
##########
tests/io/test_pyarrow.py:
##########
@@ -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"):
Review Comment:
Nit: could you use `@pytest.mark.parametrize` here instead of a loop?
##########
tests/io/test_fsspec.py:
##########
@@ -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)
Review 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`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]