Repository navigation
[Python] Fix WriteToAvro for schema'd rows with map, nullable or repeated row fields - #41736
Viranshul-P wants to merge 4 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). |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #41736 +/- ##
=========================================
Coverage 58.22% 58.23%
Complexity 2846 2846
=========================================
Files 1211 1211
Lines 173867 173883 +16
Branches 2330 2330
=========================================
+ Hits 101238 101256 +18
+ Misses 69916 69914 -2
Partials 2713 2713
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:
|
jrmccluskey
left a comment
There was a problem hiding this comment.
A few small nits/changes, mainly quality of life changes.
| import tempfile | ||
| import unittest | ||
| from typing import Any | ||
| from typing import Mapping |
There was a problem hiding this comment.
Use the collections.abc versions of Mapping and Sequence
| def unnest_primitive_type( | ||
| beam_type: schema_pb2.FieldType, defined_names: Optional[set] = None): |
There was a problem hiding this comment.
Add a type hint here to make the return type explicit
| def beam_value_to_avro_value( | ||
| beam_type: schema_pb2.FieldType) -> Callable[[Any], Any]: | ||
| convert = _beam_value_to_avro_value(beam_type) | ||
| if beam_type.nullable and beam_type.WhichOneof("type_info") != "atomic_type": | ||
| return lambda value: None if value is None else convert(value) | ||
| return convert | ||
|
|
||
|
|
||
| def _beam_value_to_avro_value( | ||
| beam_type: schema_pb2.FieldType) -> Callable[[Any], Any]: |
There was a problem hiding this comment.
I don't love the private-public split here, but I see why it exists structurally.
There was a problem hiding this comment.
Thanks @jrmccluskey! The test imports Mapping and Sequence from collections.abc, and the return type of unnest_primitive_type has been added; both changes are in ee1ce10. On the public/private separation: the public function does the None check in one place, so the helper does not repeat it. I can combine them into one if you want. The one red check (ML 3.13, test_run_inference_timeout_does_garbage_collection) fails on master as well.
…ive_type Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Viranshul-P <viranshulpanchal23@gmail.com>
|
/gemini review |
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