Skip to content

[Avro] Support the portable TIME logical type in Avro conversions - #41664

Open
Anurag-S1ngh wants to merge 5 commits into
apache:masterfrom
Anurag-S1ngh:fix/40994-parquet-time-logical-type
Open

Anurag-S1ngh wants to merge 5 commits into
apache:masterfrom
Anurag-S1ngh:fix/40994-parquet-time-logical-type

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Summary

Adds support for the portable SqlTypes.TIME logical type to the Avro schema conversion. TIME fields map to Avro time-micros, and LocalTime values convert to microseconds since midnight.

Previously, writing a Beam SQL Parquet table with a TIME column failed with Unhandled logical type beam:logical_type:time:v1.

Changes

  • Map SqlTypes.TIME to Avro time-micros in AvroUtils.getFieldSchema
  • Convert LocalTime to microseconds since midnight in genericFromBeamField
  • Add AvroUtilsTest coverage for the Avro schema and value conversion
  • Add a ParquetTableProviderTest case that inserts a TIME value into a Parquet table

Testing

  • :sdks:java:extensions:avro:test --tests "org.apache.beam.sdk.extensions.avro.schemas.utils.AvroUtilsTest"
  • :sdks:java:extensions:sql:test --tests "org.apache.beam.sdk.extensions.sql.meta.provider.parquet.ParquetTableProviderTest"

Closes #40994

Map SqlTypes.TIME to Avro time-micros and convert LocalTime values to microseconds since midnight. This lets Beam SQL write TIME columns to Parquet tables, which previously failed with "Unhandled logical type beam:logical_type:time:v1".
@github-actions github-actions Bot added java extensions sql size/s S: 10-49 lines changed labels Nov 25, 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 adds support for SqlTypes.TIME (converting LocalTime to/from Avro time-micros) in AvroUtils.toGenericRecord and AvroUtils.toBeamRowStrict, fixing a UnsupportedOperationException when writing TIME values via SQL to Parquet/Avro sinks. However, the read path for Parquet/Avro tables in SQL relies on AvroUtils.getGetter (used by RowWithGetters via AvroSchemaProvider), which is not updated in this PR and will fail at runtime with a ClassCastException when reading SqlTypes.TIME fields.

🔍 General Feedback

  • Incomplete Read Path (AvroUtils.getGetter): When reading from a ParquetTable or Avro-backed SchemaCoder, AvroUtils.toBeamRowFunction wraps GenericRecord in RowWithGetters using AvroUtils.getGetter. Because GetterBasedSchemaProvider invokes LogicalType.toInputType on getter return values, getGetter must return the logical type's base type (Long nanoseconds for SqlTypes.TIME), whereas Avro's time-micros returns a Long in microseconds.
  • Silent Sub-Microsecond Truncation: LocalTime and SqlTypes.TIME have nanosecond precision, whereas Avro time-micros only supports microsecond precision. TimeUnit.NANOSECONDS.toMicros(...) silently truncates nanoseconds.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

💬 Inline Findings

  • sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroUtils.java (line 713): 🔴 Critical: AvroUtils.getGetter (around line 1110) also needs a corresponding case for SqlTypes.TIME.getIdentifier().

When reading from ParquetTable (or decoding via AvroUtils.schemaCoder), AvroUtils.toBeamRowFunction creates a RowWithGetters backed by AvroSchemaProvider and AvroUtils.getGetter. For logical types, GetterBasedSchemaProvider wraps the field getter in GetLogicalInputType, which passes the getter's output directly to SqlTypes.TIME.toInputType(Long) (expecting nanoseconds of day). Currently, AvroUtils.getGetter falls through to the default branch for SqlTypes.TIME: if Avro returns an unkonwn/raw Long in microseconds, LocalTime.ofNanoOfDay will divide by 1000x too much; or if Avro's TimeMicrosConversion converts it to LocalTime, GetLogicalInputType will throw ClassCastException: class java.time.LocalTime cannot be cast to class java.lang.Long.

In AvroUtils.getGetter, please add handling for SqlTypes.TIME.getIdentifier() that converts the GenericRecord value (microseconds Long or LocalTime) to nanoseconds Long (the base type of SqlTypes.TIME), matching how SqlTypes.DATE and SqlTypes.TIMESTAMP are handled there.

  • sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/meta/provider/parquet/ParquetTableProviderTest.java (line 227): 🟠 High: This test only verifies writing to the ParquetTable, leaving the SELECT read path untested (which currently fails due to AvroUtils.getGetter not handling SqlTypes.TIME). Please add a read pipeline (SELECT time_col FROM TimeTable with PAssert) to verify the full round-trip.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @ahmedabu98 for label java.
Backup reviewer: damccorm (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).

Convert Avro time-micros values to LocalTime when the target Beam field is SqlTypes.TIME. Preserve raw Long values for INT64 targets.
Use the SQL table's TIME field type when converting Parquet records so time-micros values become LocalTime instead of raw longs. Apply the same conversion to projected reads while preserving inferred types for other fields.
@github-actions github-actions Bot added size/m M: 50-249 lines changed and removed size/s S: 10-49 lines changed labels Nov 27, 2026

@ahmedabu98 ahmedabu98 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.

LGTM

Please sync to head and resolve merge conflicts

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor Author

Synced with master and resolved the conflicts. The ParquetTable read change is no longer needed because #41636 now converts records with the table schema, so the PR is down to the Avro TIME conversions plus tests. Both AvroUtilsTest and ParquetTableProviderTest pass locally.

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]: Beam SQL Parquet tables fail on TIME columns: Unhandled logical type beam:logical_type:time:v1

2 participants