Repository navigation
[Python] Fix WriteToAvro for schema'd rows with map, nullable or repeated row fields - #41736
Viranshul-P wants to merge 2 commits into
Conversation
…ated row fields beam_type_to_avro_type read map_type.element_type (MapType has value_type), ignored nullable on arrays, maps and rows, and defined a nested record again each time its schema was reused, which fastavro rejects. Generated-by: Claude Code (Opus 5.5) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Viranshul-P <viranshulpanchal23@gmail.com>
Generated-by: Claude Code (Opus 5.5) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Viranshul-P <viranshulpanchal23@gmail.com>
There was a problem hiding this comment.
📋 Review Summary
This PR fixes three crashes in WriteToAvro when deriving Avro schemas and converting Beam rows without an explicit schema: using value_type instead of element_type for MapType, handling nullable complex types (array, map, row) in both schema generation and value conversion, and avoiding duplicate record definitions by referencing already-defined record names.
🔍 General Feedback
- The changes in
avroio.pyare clean, minimal, and preserve existing behavior for non-nullable and atomic types. - The added test in
avroio_test.pyeffectively covers map fields, nullable array/row fields, and repeated row types.
💡 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 #41735.
WriteToAvrowithout an explicitschema(and Beam YAMLWriteToAvro) derives the Avro schema withbeam_type_to_avro_typeand converts each row withbeam_value_to_avro_value. Three field kinds broke that:AttributeError: element_type: the code readmap_type.element_type, butMapTypehasvalue_type.None:nullablewas only applied to atomic types, so the Avro type had nonullbranch, and the value converter iterated theNone. Such fields are now[<type>, "null"], the same order already used for nullable atomics, andNonepasses through.SchemaParseException: redefined named type: the nested record was defined again on every use. The first use now defines it, and later uses refer to it by name, as Avro requires.Schemas that worked before come out unchanged (
test_avro_schema_to_beam_and_backstill passes). This PR only touches the write path. Reading those files back withReadFromAvro(as_rows=True)still fails for nullable records and named-type references. That is inavro_type_to_beam_type/avro_value_to_beam_value, which #41701 also edits, so I'll send it separately after that one.Test:
avroio_test.py::test_schema_write_map_nullable_and_repeated_row_fieldswrites rows with all three field kinds and reads the file back withfastavro.reader. It fails on master and passes with this change.io/avroio_test.pyandyaml/yaml_io_test.pypass (54 passed, 26 skipped, Windows 11, Python 3.12;test_sink_transformis deselected because it hangs on Windows on master too). yapf 0.43.0 and ruff 0.15.7 report nothing.This change was written with help from Claude Code; the commits carry 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