Skip to content

IO: Delete only the exact file path - #4512

Open
Anurag-S1ngh wants to merge 1 commit into
apache:mainfrom
Anurag-S1ngh:fix/4504-fileio-delete-exact
Open

Anurag-S1ngh wants to merge 1 commit into
apache:mainfrom
Anurag-S1ngh:fix/4504-fileio-delete-exact

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Rationale for this change

Closes #4504.

FileIO.delete could delete a different file than the one named.

  • FsspecFileIO.delete used fs.rm, which expands *, ? and [...] as glob patterns on some filesystems. It now uses fs.rm_file, which deletes the literal path.
  • PyArrowFileIO.delete parsed the location as a URL, so ?query and #fragment were 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 to rm_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.

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 Fokko left a comment

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.

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?

Comment thread pyiceberg/io/pyarrow.py
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.

Comment thread pyiceberg/io/pyarrow.py
Comment on lines +729 to +734
parsed = urlparse(str_location)
if parsed.query:
path = f"{path}?{parsed.query}"
if parsed.fragment:
path = f"{path}#{parsed.fragment}"

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.

Comment thread pyiceberg/io/fsspec.py
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.

Comment thread tests/io/test_fsspec.py
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).

Comment thread tests/io/test_pyarrow.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"):

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?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileIO delete can delete other files: fsspec expands globs, PyArrow drops ? and #

2 participants