Skip to content

[Beam SQL] Read DATE columns back from Parquet tables - #41636

Open
jonathansudhakar1 wants to merge 1 commit into
apache:masterfrom
jonathansudhakar1:sql-parquet-date-read-41017
Open

jonathansudhakar1 wants to merge 1 commit into
apache:masterfrom
jonathansudhakar1:sql-parquet-date-read-41017

Conversation

@jonathansudhakar1

Copy link
Copy Markdown
Contributor

Fixes #41017.

A Beam SQL Parquet table can write a DATE column, but reading it back fails: SELECT * throws a DateTimeException (Invalid value for EpochDay ...: 1752451200000) and a pushed-down projection (SELECT d FROM t) throws a ClassCastException (org.joda.time.Instant cannot be cast to java.time.LocalDate).

Cause. ParquetTable.buildIOReader read with ParquetIO.read(schema).withBeamSchemas(true) and then Convert.toRows(), so the rows used the Beam schema that AvroUtils infers from the Avro schema. That maps an Avro date to DATETIME (a Joda Instant), but SQL's DATE is SqlTypes.DATE (a java.time.LocalDate). The same mismatch breaks a pushed-down projection of a TINYINT or SMALLINT column: the inferred schema reads those Avro ints as INT32, and BeamPushDownIOSourceRel then sets the SQL row schema on rows that hold Integers (ClassCastException: Integer cannot be cast to Byte).

Fix. This follows the two steps @ahmedabu98 agreed to on the issue:

  1. AvroUtils: the strict Avro-to-Beam conversion returns a LocalDate for an Avro date when the target field is SqlTypes.DATE. Today convertDateStrict rejects every target except DATETIME.
  2. ParquetTable: convert the records to rows with the table's own schema, or its projected subset in the pushed-down field order, using AvroUtils.getGenericRecordToRowFunction(rowSchema) and setRowSchema(rowSchema), instead of Convert.toRows(). The read keeps withBeamSchemas(true): ParquetIO's records carry the file's full schema, and the Beam-schema coder matches fields by name, while a plain AvroCoder with the projection schema encodes by position.

One addition beyond the plan. With step 2 alone, SELECT * on a TINYINT or SMALLINT column, which works today, would fail with Can't convert 'int' to BYTE, expected: INT32. toAvroSchema writes BYTE and INT16 as Avro ints, but the strict conversion only accepts INT32 for them. So convertIntStrict now also returns a byte or short when the target is BYTE or INT16, and throws IllegalArgumentException if the value is out of range instead of truncating it.

Both AvroUtils changes are additive: they only affect targets that were rejected with an exception before, and schemas inferred from Avro still get DATETIME and INT32. #21003 (inferring SqlTypes.DATE from Avro date) is left alone.

Before and after, for INSERT then read on a table (id INTEGER, c <type>), on the DirectRunner (from a temporary parameterized test, not committed):

Type SELECT * SELECT c (pushed down) SELECT c, id
DATE fails → OK fails → OK fails → OK
TINYINT, SMALLINT OK → OK fails → OK OK → OK
INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL, BOOLEAN, TIMESTAMP, VARCHAR, CHAR, VARBINARY OK → OK OK → OK OK → OK

Testing

All with JDK 21 on b2bb831:

  • New ParquetTableProviderTest tests write a row with a value in every scalar column (TINYINT, SMALLINT, BIGINT, FLOAT, DOUBLE, DECIMAL, BOOLEAN, DATE, TIMESTAMP, VARCHAR, VARBINARY) and a row of NULLs, then check each value and its Java type through SELECT * (testReadAllTypes) and through SELECT d, si, ti, ts, id, which the test asserts is a BeamPushDownIOSourceRel (testReadAllTypesWithProjectionPushDown).
  • New AvroUtilsTest tests: testGenericRecordToRowWithSqlDateAndSmallIntegers (a toGenericRecord → toBeamRowStrict round trip with SqlTypes.DATE, BYTE, INT16 and nullable fields) and testGenericRecordToRowRejectsOutOfRangeSmallIntegers.
  • With the main-code changes reverted, all four new tests fail: testReadAllTypes with the issue's DateTimeException, testReadAllTypesWithProjectionPushDown with the ClassCastException, and the two AvroUtilsTest tests with Can't convert 'date' to LOGICAL_TYPE, expected: DATETIME and Can't convert 'int' to BYTE, expected: INT32. With only the convertIntStrict change reverted, both Parquet tests fail with Can't convert 'int' to BYTE / to INT16.
  • ./gradlew :sdks:java:extensions:avro:spotlessApply :sdks:java:extensions:sql:spotlessApply, then spotlessCheck for both: clean.
  • ./gradlew :sdks:java:extensions:avro:compileJava :sdks:java:extensions:avro:compileTestJava :sdks:java:extensions:avro:checkstyleMain :sdks:java:extensions:avro:checkstyleTest and the same four tasks for :sdks:java:extensions:sql (Checker Framework on): BUILD SUCCESSFUL.
  • ./gradlew :sdks:java:extensions:avro:test: 136 tests, 0 failures, 1 skipped (AvroVersionVerificationTest), including AvroUtilsTest 37/37.
  • ./gradlew :sdks:java:extensions:sql:test --tests '...parquet.ParquetTableProviderTest' --tests '...avro.AvroTableProviderTest': 5 tests, 0 failures.
  • ./gradlew :sdks:java:io:parquet:test --tests 'org.apache.beam.sdk.io.parquet.ParquetIOTest': 19 tests, 0 failures.

Open questions

  • toBeamRowStrict's Javadoc says the conversion "doesn't do widening or narrowing". The BYTE/INT16 case is a narrowing, but a range-checked one, and only for the types that toAvroSchema writes as Avro ints. I left the Javadoc as it is; happy to reword it, or to move this into ParquetTable if you'd rather keep AvroUtils' int handling unchanged.
  • The Avro table provider (AvroSchemaIOProvider.buildReader) uses the same withBeamSchemas(true) + Convert.toRows() pattern. I haven't checked whether it has the same DATE problem; I can look at it in a follow-up if that's useful.

ParquetTable converted the records it read with the Beam schema that
ParquetIO infers from the Avro schema, and that schema maps an Avro date
to DATETIME. SQL then got a Joda Instant where its DATE type expects a
java.time.LocalDate: SELECT * failed with a DateTimeException, and a
pushed-down projection with a ClassCastException. A pushed-down
projection of a TINYINT or SMALLINT column failed the same way, because
the inferred schema reads those Avro ints as INT32.

Convert the records with the table's own (projected) schema instead.
For that, AvroUtils' strict Avro-to-Beam conversion now returns a
LocalDate for an Avro date when the target field is SqlTypes.DATE, and a
byte or short for an Avro int when the target is BYTE or INT16, rejecting
values that are out of range. toAvroSchema already writes those types as
Avro date and int. Targets that were accepted before convert as they did.

Fixes apache#41017
@jonathansudhakar1

Copy link
Copy Markdown
Contributor Author

R: @ahmedabu98, as you suggested on #41017. Thanks for looking at the plan! The one addition is the range-checked BYTE/INT16 case in convertIntStrict, which step 2 needs so that SELECT * on TINYINT/SMALLINT columns keeps working (details in the description).

@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 cleanly resolves #41017 by ensuring ParquetTable converts Avro GenericRecords into Beam Rows using the SQL table's declared (or projected) Beam Schema rather than the schema inferred from the Avro schema. It also updates AvroUtils.toBeamRowStrict to support converting Avro date logical types back into SqlTypes.DATE (java.time.LocalDate) and range-checked Avro int values into BYTE (TINYINT) and INT16 (SMALLINT).

🔍 General Feedback

  • Preserving DATETIME conversion as the default when no SqlTypes.DATE logical type target is specified maintains full backward compatibility for existing callers of AvroUtils.
  • Explicit bounds checks in convertIntStrict (value.intValue() == value.byteValue() and value.intValue() == value.shortValue()) prevent silent truncation when narrowing Avro int values to BYTE or INT16.
  • Comprehensive unit and end-to-end SQL tests cover both full reads and projection pushdown with reordered and nullable columns.

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

@github-actions github-actions Bot added java extensions sql size/m M: 50-249 lines changed labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

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

@github-actions

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

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 can't read DATE columns back (Joda Instant where LocalDate is expected)

1 participant