Repository navigation
[Python] Convert Avro logical types when reading as Beam rows - #41701
Viranshul-P wants to merge 1 commit into
Conversation
ReadFromAvro(as_rows=True) mapped every Avro field to its primitive type and ignored logicalType, but fastavro decodes logical types to Python objects (datetime, date, Decimal, UUID), so the RowCoder rejected them. Map timestamp-millis/micros, date, decimal and uuid to Timestamp, datetime.date, decimal.Decimal and str, as the Java SDK's AvroUtils does, and fall back to Any for the int/long logical types with no Python Beam type (time-*, local-timestamp-*). Fixes apache#41700 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 updates ReadFromAvro(as_rows=True) to recognize Avro logical types (timestamp-millis, timestamp-micros, date, decimal, uuid) and convert both the schema field types and decoded fastavro values into their corresponding Beam Python types, falling back to Any for unsupported int/long logical types. The implementation is clean, aligns with Beam's schema conventions, and includes a test case covering RowCoder serialization.
🔍 General Feedback
- The mapping of Avro logical types to Beam schema types (
Timestamp,datetime.date,decimal.Decimal,str, andAny) is well-aligned with howfastavrodecodes records and howRowCoderencodes them. - Notice that container converters (
array_type,iterable_type,map_type,row_type) inavro_value_to_beam_valuedo not currently guard againstNonewhen a field is nullable, whereas the newMicrosInstantconverter properly checksif value is None.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
💬 Inline Findings
sdks/python/apache_beam/io/avroio.py(line 631): 🟢 Low: In Avro, bothbytesandfixedtypes can be annotated withlogicalType: decimal, or potentially custom logical types thatfastavrodecodes into non-primitive objects. Becausefixedis currently mapped toschema_pb2.STRINGhere (whilefastavroreturnsbytesforfixedwithout a logical type, orDecimalwithdecimallogical type),decimalonfixedworks sinceAVRO_LOGICAL_TYPES_TO_PYTHON_TYPESis checked first. However, if an unknown logical type is attached tobytes,fixed, orstringand registered infastavro.read.LOGICAL_READERS, it would fall through to primitive types instead ofAny. Restricting theAnyfallback on line 615 totype_name in AVRO_PRIMITIVES_TO_BEAM_PRIMITIVES or type_name == 'fixed'(or simplyif logical_type is not None:when not inAVRO_LOGICAL_TYPES_TO_PYTHON_TYPESandtype_name not in ('record', 'array', 'map', 'enum')) could be slightly safer if unknown logical types are encountered on non-int/long primitives, though standard Avro built-in logical types outside ofAVRO_LOGICAL_TYPES_TO_PYTHON_TYPESare indeed only onint/long(time-*,local-timestamp-*) orfixed(duration).
|
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). |
Fixes #41700.
avro_type_to_beam_typeignoredlogicalTypeand mapped each field to its primitive (INT64,INT32,BYTES,STRING). fastavro decodes logical types to Python objects (datetime,date,Decimal,UUID), soReadFromAvro(as_rows=True)failed in theRowCoderfor any such field, for example theTIMESTAMPcolumns of a BigQuery Avro export. Beam YAML'sReadFromAvrousesas_rows: Trueby default, so YAML pipelines hit this too. This was first reported in #31656 (closed by the stale bot).This PR maps the logical types to the Beam types the Java SDK's
AvroUtilsuses, through the existing Python logical types:timestamp-millis,timestamp-microsTimestamp(micros_instant)Timestampdatedatetime.date(date)datetime.datedecimaldecimal.Decimal(decimal)DecimaluuidSTRINGstr(uuid)int/longlogical types with no Python Beam type (time-millis,time-micros,local-timestamp-*) map toAny, as non-nullable unions already do in this file, so their values pass through unchanged. Fields without a logical type are not touched. The write side (beam_type_to_avro_type) is unchanged; it doesn't handle logical types today either.Test:
avroio_test.py::test_read_as_rows_with_logical_typeswrites a file with all six logical types (one nullable) and reads it withas_rows=Truethrough aReshuffle, so the rows go through theRowCoder. It fails on master withTypeError: an integer is requiredand passes with this change. The rest ofavroio_test.pyandyaml/yaml_io_test.pypass (Windows 11, Python 3.12;test_sink_transformhangs on Windows on master too, so I deselected it). 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