Fix silent dataset truncation in batched map with mismatched empty column (#6879)#8311
Open
florian6973 wants to merge 1 commit into
Open
Conversation
…lumn (huggingface#6879) `ArrowWriter._write_batch` skips a batch when it "appears empty", but the emptiness check only looked at the first column's length. When a batched `map` function returns an empty list for an existing column alongside a non-empty column (e.g. `{"test": [], "y": [1]}`), the first column being empty caused the whole length-mismatched batch to be silently dropped, truncating the dataset to 0 rows instead of raising. Skip the batch only when *all* columns are empty, so genuinely empty batches are still ignored (unchanged behavior, covered by the existing `test_write_batch`) while mismatched batches fall through to `pa.Table.from_arrays`, which raises the informative `ArrowInvalid` already produced for the new-column case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #6879.
Dataset.map(fn, batched=True)silently truncates the dataset to 0 rows (instead of raising) when the mapping function empties an existing column while another column stays non-empty.Reproducer (from the issue)
Returning an empty list for a new column (e.g.
{"x": []}) already raisespyarrow.lib.ArrowInvalid, so the two cases were inconsistent.Root cause
ArrowWriter._write_batchskips a batch that "appears empty", but the emptiness check only looked at the first column:When a batched
mapoverwrites an existing column with[], that column stays first in the merged dict, so the whole length-mismatched batch was dropped instead of erroring.Fix
Skip the batch only when every column is empty. Genuinely empty batches (e.g. a batch that filters out all its rows) are still ignored, while mismatched batches now fall through to
pa.Table.from_arrays, which raises the same informativeArrowInvalidalready produced for the new-column case.Behavior
len(data) == 0ArrowInvalid: Column 1 named y expected length 0 but got length 1Normal batched
mapand legitimate "all columns empty" batches are unchanged.Tests
Added
test_write_batch_with_mismatched_empty_column_raisesintests/test_arrow_writer.py. The existingtest_write_batchalready covers the legitimate all-empty-batch skip, so that path stays protected.ruff check/ruff format --checkpass;tests/test_arrow_writer.pypasses locally.