Repository navigation
[Python] Honor num_shards in WriteToCsv and WriteToJson - #41711
Viranshul-P wants to merge 1 commit into
Conversation
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>
There was a problem hiding this comment.
📋 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_shardsand_WriteToPandas.expandis clean, preserves existing behavior fornum_shards in (None, 0, 1), and avoids emitting empty slices when a batch has fewer rows thannum_shards. - The unit test in
io_test.pyclearly 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.
|
Assigning reviewers: R: @jrmccluskey for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Fixes #41702.
_WriteToPandas(apache_beam/dataframe/io.py) passesshards=num_shardstofileio.WriteToFiles, butWriteToFilesassigns 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-00001file. This was first reported in #34188 (closed by the stale bot).This PR splits each DataFrame batch into
num_shardsrow ranges beforeWriteToFileswhennum_shards > 1, so every shard gets rows.num_shards=None,0and1take 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, sonum_shards=50with 10 rows writes 10 files (named-of-00010) rather than 40 empty ones.Test:
dataframe/io_test.py::test_num_shards_with_one_batchwrites one 10-row batch withnum_shards=3and 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.pyandio/textio_test.pypass (153 passed, Windows 11, Python 3.12). I also ranWriteToCsvwithnum_shards0, 1, 3 and 50 andWriteToJsonwith 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-bytrailer.Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123orfixes #456).CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
🤖 Generated with Claude Code