Skip to content

[Python] Honor num_shards in WriteToCsv and WriteToJson - #41711

Open
Viranshul-P wants to merge 1 commit into
apache:masterfrom
Viranshul-P:fix/write-csv-num-shards
Open

Viranshul-P wants to merge 1 commit into
apache:masterfrom
Viranshul-P:fix/write-csv-num-shards

Conversation

@Viranshul-P

Copy link
Copy Markdown
Contributor

Fixes #41702.

_WriteToPandas (apache_beam/dataframe/io.py) passes shards=num_shards to fileio.WriteToFiles, but WriteToFiles assigns a shard per element, and the elements here are whole pandas DataFrame batches. When the input arrives as one batch, which is common for small and medium PCollections, one shard gets every row and the others get nothing. WriteToCsv(..., num_shards=3) then writes a single -00000-of-00001 file. This was first reported in #34188 (closed by the stale bot).

This PR splits each DataFrame batch into num_shards row ranges before WriteToFiles when num_shards > 1, so every shard gets rows. num_shards=None, 0 and 1 take the same path as before. Each shard is still written by the existing incremental sink, so every CSV file keeps its header.

One difference from WriteToText: when there are fewer rows than shards, empty pieces are skipped, so num_shards=50 with 10 rows writes 10 files (named -of-00010) rather than 40 empty ones.

Test: dataframe/io_test.py::test_num_shards_with_one_batch writes one 10-row batch with num_shards=3 and checks for 3 non-empty files whose rows add up to the input. It fails on master (['out-00000-of-00001']) and passes with this change. dataframe/io_test.py and io/textio_test.py pass (153 passed, Windows 11, Python 3.12). I also ran WriteToCsv with num_shards 0, 1, 3 and 50 and WriteToJson with 4: all rows are written in each case. yapf 0.43.0 and ruff 0.15.7 report nothing.

This change was written with help from Claude Code; the commit carries a Generated-by trailer.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123 or fixes #456).
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

🤖 Generated with Claude Code

fileio.WriteToFiles assigns a shard per element, and the elements that
_WriteToPandas writes are whole DataFrame batches. When the input
arrives as one batch, only one shard got rows and the output was a
single file, whatever num_shards was.

When num_shards > 1, split each batch into num_shards row ranges before
WriteToFiles. num_shards None, 0 and 1 are unchanged.

Fixes apache#41702

Generated-by: Claude Code (Opus 5.5)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added python size/s S: 10-49 lines changed labels Nov 27, 2026

@github-actions github-actions Bot 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.

📋 Review Summary

This PR fixes an issue where _WriteToPandas (backing WriteToCsv, WriteToJson, and DeferredDataFrame.to_* sinks) ignored num_shards > 1 when the input arrived as a single DataFrame batch because fileio.WriteToFiles assigns shards per PCollection element. By splitting each DataFrame batch into up to num_shards non-empty slices prior to WriteToFiles, rows are properly distributed across the requested number of output shards.

🔍 General Feedback

  • The implementation in _split_for_shards and _WriteToPandas.expand is clean, preserves existing behavior for num_shards in (None, 0, 1), and avoids emitting empty slices when a batch has fewer rows than num_shards.
  • The unit test in io_test.py clearly verifies both the shard file naming and row completeness across all output shards.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @jrmccluskey for label python.
Backup reviewer: tvalentyn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: [Python] WriteToCsv and WriteToJson ignore num_shards

1 participant