Skip to content

[Python] Convert Avro logical types when reading as Beam rows - #41701

Open
Viranshul-P wants to merge 1 commit into
apache:masterfrom
Viranshul-P:fix/avro-logical-types-as-rows
Open

Viranshul-P wants to merge 1 commit into
apache:masterfrom
Viranshul-P:fix/avro-logical-types-as-rows

Conversation

@Viranshul-P

Copy link
Copy Markdown
Contributor

Fixes #41700.

avro_type_to_beam_type ignored logicalType and mapped each field to its primitive (INT64, INT32, BYTES, STRING). fastavro decodes logical types to Python objects (datetime, date, Decimal, UUID), so ReadFromAvro(as_rows=True) failed in the RowCoder for any such field, for example the TIMESTAMP columns of a BigQuery Avro export. Beam YAML's ReadFromAvro uses as_rows: True by 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 AvroUtils uses, through the existing Python logical types:

Avro Beam field Row value
timestamp-millis, timestamp-micros Timestamp (micros_instant) Timestamp
date datetime.date (date) datetime.date
decimal decimal.Decimal (decimal) Decimal
uuid STRING str(uuid)

int/long logical types with no Python Beam type (time-millis, time-micros, local-timestamp-*) map to Any, 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_types writes a file with all six logical types (one nullable) and reads it with as_rows=True through a Reshuffle, so the rows go through the RowCoder. It fails on master with TypeError: an integer is required and passes with this change. The rest of avroio_test.py and yaml/yaml_io_test.py pass (Windows 11, Python 3.12; test_sink_transform hangs 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-by trailer.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123 or fixes #456).
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

🤖 Generated with Claude Code

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>
@github-actions github-actions Bot added python io size/m M: 50-249 lines changed labels Nov 26, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 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, and Any) is well-aligned with how fastavro decodes records and how RowCoder encodes them.
  • Notice that container converters (array_type, iterable_type, map_type, row_type) in avro_value_to_beam_value do not currently guard against None when a field is nullable, whereas the new MicrosInstant converter properly checks if 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, both bytes and fixed types can be annotated with logicalType: decimal, or potentially custom logical types that fastavro decodes into non-primitive objects. Because fixed is currently mapped to schema_pb2.STRING here (while fastavro returns bytes for fixed without a logical type, or Decimal with decimal logical type), decimal on fixed works since AVRO_LOGICAL_TYPES_TO_PYTHON_TYPES is checked first. However, if an unknown logical type is attached to bytes, fixed, or string and registered in fastavro.read.LOGICAL_READERS, it would fall through to primitive types instead of Any. Restricting the Any fallback on line 615 to type_name in AVRO_PRIMITIVES_TO_BEAM_PRIMITIVES or type_name == 'fixed' (or simply if logical_type is not None: when not in AVRO_LOGICAL_TYPES_TO_PYTHON_TYPES and type_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 of AVRO_LOGICAL_TYPES_TO_PYTHON_TYPES are indeed only on int/long (time-*, local-timestamp-*) or fixed (duration).

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping for label python.
Backup reviewer: tvalentyn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: [Python] ReadFromAvro(as_rows=True) fails on Avro logical types (timestamp, date, decimal, uuid)

1 participant