diff --git a/pyiceberg/io/fsspec.py b/pyiceberg/io/fsspec.py index 505b5eebbf..b7e8bd5b9a 100644 --- a/pyiceberg/io/fsspec.py +++ b/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) def _get_fs_from_uri(self, uri: "ParseResult", location: str = "") -> AbstractFileSystem: """Get a filesystem from a parsed URI, using hostname for ADLS account resolution.""" diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index 8aff1f7e73..fa8f4a4527 100644 --- a/pyiceberg/io/pyarrow.py +++ b/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}" + try: fs.delete_file(path) except FileNotFoundError: diff --git a/tests/io/test_fsspec.py b/tests/io/test_fsspec.py index 9c76e535fe..585aec99d6 100644 --- a/tests/io/test_fsspec.py +++ b/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) + 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] = [] diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 92d533651c..42ca3538ad 100644 --- a/tests/io/test_pyarrow.py +++ b/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"): + 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",