Skip to content

IO: Delete the exact file path instead of expanding globs - #4512

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

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

Conversation

@Anurag-S1ngh

@Anurag-S1ngh Anurag-S1ngh commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Part of #4504.

FsspecFileIO.delete used fs.rm, which expands *, ? and [...] as glob patterns. Deleting data-[12].parquet also removed data-1.parquet and data-2.parquet. It now uses fs.rm_file, which deletes the literal path.

The PyArrow query and fragment handling is split out. parse_location strips 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.

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 Outdated
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 Outdated
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

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?

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.
@Anurag-S1ngh

Copy link
Copy Markdown
Contributor Author

Reduced the PR to the glob fix. FsspecFileIO.delete now uses fs.rm_file, with the local test that a [...] name leaves its siblings untouched. The PyArrow query/fragment change and its two tests are gone.

The ?/# behavior does belong in parse_location, and the double append you described on a local path reproduces. I can file a separate issue covering that and the versionId handling in s3fs, gcsfs and adlfs. Want me to?

@Anurag-S1ngh Anurag-S1ngh changed the title IO: Delete only the exact file path IO: Delete the exact file path instead of expanding globs Nov 30, 2026

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.

2 participants