Repository navigation
[Python] Document the file paths output by file-based sinks - #41318
jonathansudhakar1 wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR fixes the batch output timestamps of iobase.Write (_finalize_write and _WriteKeyedBundleDoFn) to use GlobalWindow().max_timestamp() instead of timestamp.MAX_TIMESTAMP, preventing downstream grouping failures in Prism and dropped elements in BundleBasedDirectRunner (#41245). It also clearly documents the output PCollection of finalized file paths (along with windowed behavior and finalization retry semantics) across iobase.Write, Sink.finalize_write, and the file-based sink transforms (#25769).
🔍 General Feedback
- Aligning
_finalize_writeand_WriteKeyedBundleDoFnwith_WriteBundleDoFn.finish_bundle()(window.GlobalWindow().max_timestamp()) is clean and ensures emitted elements remain within the bounds ofGlobalWindow. - The added unit tests in
textio_test.pythoroughly cover batch file path outputs, output timestamps for bothnum_shards=0andnum_shards=3, and streaming windowed file path outputs.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @shunping 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). |
a2de4de to
e55f824
Compare
|
@shunping a gentle ping when you have a moment: this fixes #41245 (file-based sinks emit their output file paths at |
e55f824 to
b84907d
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #41318 +/- ##
============================================
+ Coverage 59.38% 59.43% +0.05%
Complexity 5137 5137
============================================
Files 1470 1470
Lines 187512 187748 +236
Branches 3819 3819
============================================
+ Hits 111351 111596 +245
+ Misses 72756 72747 -9
Partials 3405 3405
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| shutil.rmtree(self.tempdir) | ||
|
|
||
| def test_write_streaming_outputs_final_file_paths(self): | ||
| prefix = self.tempdir + '/output' |
There was a problem hiding this comment.
The "/" is the culprit for the test failure on windows.
There was a problem hiding this comment.
Thanks, good catch. On Windows glob.glob joins the directory and file name with a backslash, so the expected paths built with '/' didn't match. ce693c9 builds the prefix with os.path.join(self.tempdir, 'output') instead, so the pipeline output, the expected list and the glob result all use the native separator. This was the only failure in the four Windows jobs (the same test each time). I can't run Windows locally; on macOS the three new tests pass, and the seven touched IO test modules give 268 passed, 10 skipped with the branch merged into current master.
|
Thanks for the review! The Windows failure is fixed in ce693c9: the new streaming test now builds its output prefix with |
Rerunning tests now. |
shunping
left a comment
There was a problem hiding this comment.
Hey @jonathansudhakar1, taking another look, the documentation changes seem orthogonal to the sink output timestamp fix. Could you please split them into two separate PRs?
For the documentation changes, instead of duplicating the text for different IOs, have you considered there a way to put the explanation in one class (like the base class) and refer to it in the derived ones?
Thanks again for working on this!
WriteToText, WriteToAvro, WriteToParquet, WriteToParquetBatched and WriteToTFRecord return a PCollection of the final paths of the files they write, but their docstrings didn't say so, and iobase.Write's docstring said it returned a PValue marking the end of the pipeline. Document the output in each transform, including that a finalization retry doesn't re-emit files that an earlier attempt already renamed, and document the return value of Sink.finalize_write(). Add batch and streaming tests that check WriteToText's output against the files written. Fixes apache#25769
…h os.path.join On Windows, glob.glob returns paths joined with the native separator, so the expected paths built with '/' didn't match the files that were found.
…e#41245 fix The batch test asserts on WriteToText's output with assert_that, which groups it, and that fails on Prism until the apache#41245 fix is in. Move it to the PR for that fix, so that this PR doesn't depend on it. The streaming test stays: windowed writes already emit their paths inside their window.
WriteToText, WriteToAvro, WriteToParquet, WriteToParquetBatched and WriteToTFRecord repeated the same paragraph about their output. Describe it once, in the docstring of FileBasedSink, the base class of the sinks behind them, which already said that the output lists the shards written, and have the transforms and iobase.Write refer to it.
ce693c9 to
719d6aa
Compare
|
Thanks for taking another look! I've split it as you suggested:
For the docs, the explanation now lives in one place, the |
Fixes #25769
The fix for #41245 that used to be part of this PR is now its own PR, #41738, as requested in review. This PR is docs only, plus one streaming test.
What
FileBasedSink(filebasedsink.py), the base class of the sinks behindWriteToText,WriteToAvro,WriteToParquet,WriteToParquetBatchedandWriteToTFRecord, already said its output is "aPCollectionof all written shards". Its docstring now describes that output in full: aPCollectionofstrholding the final path of each file written, one per shard, emitted once that file is in place. For windowed writes (for example streaming withtriggering_frequency), each window's paths are emitted after that window is finalized.It also notes the retry caveat raised in the issue thread: a finalization retry doesn't re-emit files that an earlier attempt already renamed, so the output may not list every file. I went ahead with option (1) from my comment on [Feature Request]: Document that file-based sinks return a PCollection of output filenames #25769 (document the caveat and leave any behavior change for a separate PR), since there was no reply after 5 business days.
PCollectionof the final paths of the files written, and refer toFileBasedSinkfor the details, so the explanation lives in one place.iobase.Write's docstring said it returned "apvalue.PValueobject that represents the end of the Pipeline". It now describes the actual output and refers toFileBasedSinkfor file-based sinks.Sink.finalize_write()documents its return value.WriteStreamingTest.test_write_streaming_outputs_final_file_pathschecks that a windowedWriteToText(TestStream,triggering_frequency=10) outputs exactly the paths of the files it wrote. It passes without [Python] Emit Write's output file paths at the end of the global window #41738, because windowed writes already emit their paths inside their window. The batch counterpart moved to [Python] Emit Write's output file paths at the end of the global window #41738, since it needs that fix to pass on Prism.Testing (Python 3.13, macOS, at 719d6aa on 1c20e38)
pytest apache_beam/io/textio_test.py -k test_write_streaming_outputs_final_file_paths: 1 passed.pytest -n 6onapache_beam/io/{textio,filebasedsink,iobase,avroio,parquetio,tfrecordio}_test.pyandapache_beam/transforms/write_ptransform_test.py: 266 passed, 10 skipped.pre-commit run --files <changed files>(yapf, ruff): passed.pylint --rcfile=.pylintrcon the changed.pyfiles: 10.00/10.:class:references. Windows isn't available locally; the earlier Windows failure was fixed in 1b91967 (os.path.join).