jonathansudhakar1 opened a new pull request, #41636: URL: https://github.com/apache/beam/pull/41636
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 `Integer`s (`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 b2bb8313405: - 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
