Repository navigation
[Beam SQL] Read DATE columns back from Parquet tables - #41636
jonathansudhakar1 wants to merge 1 commit into
Conversation
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
|
R: @ahmedabu98, as you suggested on #41017. Thanks for looking at the plan! The one addition is the range-checked |
There was a problem hiding this comment.
📋 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
DATETIMEconversion as the default when noSqlTypes.DATElogical type target is specified maintains full backward compatibility for existing callers ofAvroUtils. - Explicit bounds checks in
convertIntStrict(value.intValue() == value.byteValue()andvalue.intValue() == value.shortValue()) prevent silent truncation when narrowing Avrointvalues toBYTEorINT16. - 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.
|
Assigning reviewers: R: @chamikaramj for label java. 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). |
|
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment |
Fixes #41017.
A Beam SQL Parquet table can write a
DATEcolumn, but reading it back fails:SELECT *throws aDateTimeException(Invalid value for EpochDay ...: 1752451200000) and a pushed-down projection (SELECT d FROM t) throws aClassCastException(org.joda.time.Instantcannot be cast tojava.time.LocalDate).Cause.
ParquetTable.buildIOReaderread withParquetIO.read(schema).withBeamSchemas(true)and thenConvert.toRows(), so the rows used the Beam schema thatAvroUtilsinfers from the Avro schema. That maps an AvrodatetoDATETIME(a JodaInstant), but SQL'sDATEisSqlTypes.DATE(ajava.time.LocalDate). The same mismatch breaks a pushed-down projection of aTINYINTorSMALLINTcolumn: the inferred schema reads those Avro ints asINT32, andBeamPushDownIOSourceRelthen sets the SQL row schema on rows that holdIntegers (ClassCastException: Integer cannot be cast to Byte).Fix. This follows the two steps @ahmedabu98 agreed to on the issue:
AvroUtils: the strict Avro-to-Beam conversion returns aLocalDatefor an Avrodatewhen the target field isSqlTypes.DATE. TodayconvertDateStrictrejects every target exceptDATETIME.ParquetTable: convert the records to rows with the table's own schema, or its projected subset in the pushed-down field order, usingAvroUtils.getGenericRecordToRowFunction(rowSchema)andsetRowSchema(rowSchema), instead ofConvert.toRows(). The read keepswithBeamSchemas(true): ParquetIO's records carry the file's full schema, and the Beam-schema coder matches fields by name, while a plainAvroCoderwith the projection schema encodes by position.One addition beyond the plan. With step 2 alone,
SELECT *on aTINYINTorSMALLINTcolumn, which works today, would fail withCan't convert 'int' to BYTE, expected: INT32.toAvroSchemawritesBYTEandINT16as Avro ints, but the strict conversion only acceptsINT32for them. SoconvertIntStrictnow also returns abyteorshortwhen the target isBYTEorINT16, and throwsIllegalArgumentExceptionif the value is out of range instead of truncating it.Both
AvroUtilschanges are additive: they only affect targets that were rejected with an exception before, and schemas inferred from Avro still getDATETIMEandINT32. #21003 (inferringSqlTypes.DATEfrom Avrodate) is left alone.Before and after, for
INSERTthen read on a table(id INTEGER, c <type>), on the DirectRunner (from a temporary parameterized test, not committed):SELECT *SELECT c(pushed down)SELECT c, idTesting
All with JDK 21 on b2bb831:
ParquetTableProviderTesttests 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 throughSELECT *(testReadAllTypes) and throughSELECT d, si, ti, ts, id, which the test asserts is aBeamPushDownIOSourceRel(testReadAllTypesWithProjectionPushDown).AvroUtilsTesttests:testGenericRecordToRowWithSqlDateAndSmallIntegers(atoGenericRecord→toBeamRowStrictround trip withSqlTypes.DATE,BYTE,INT16and nullable fields) andtestGenericRecordToRowRejectsOutOfRangeSmallIntegers.testReadAllTypeswith the issue'sDateTimeException,testReadAllTypesWithProjectionPushDownwith theClassCastException, and the twoAvroUtilsTesttests withCan't convert 'date' to LOGICAL_TYPE, expected: DATETIMEandCan't convert 'int' to BYTE, expected: INT32. With only theconvertIntStrictchange reverted, both Parquet tests fail withCan't convert 'int' to BYTE/to INT16../gradlew :sdks:java:extensions:avro:spotlessApply :sdks:java:extensions:sql:spotlessApply, thenspotlessCheckfor both: clean../gradlew :sdks:java:extensions:avro:compileJava :sdks:java:extensions:avro:compileTestJava :sdks:java:extensions:avro:checkstyleMain :sdks:java:extensions:avro:checkstyleTestand 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), includingAvroUtilsTest37/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". TheBYTE/INT16case is a narrowing, but a range-checked one, and only for the types thattoAvroSchemawrites as Avro ints. I left the Javadoc as it is; happy to reword it, or to move this intoParquetTableif you'd rather keepAvroUtils' int handling unchanged.AvroSchemaIOProvider.buildReader) uses the samewithBeamSchemas(true)+Convert.toRows()pattern. I haven't checked whether it has the sameDATEproblem; I can look at it in a follow-up if that's useful.