[Bug]: Beam SQL Parquet tables can't read DATE columns back (Joda Instant where LocalDate is expected)
What happened?
A Beam SQL Parquet table can write a DATE column, but reading it back fails. On master (74ab567d2e3), SELECT * throws a DateTimeException:
java.time.DateTimeException: Invalid value for EpochDay (valid values -365243219162 - 365241780471): 1752451200000
at java.time.LocalDate.ofEpochDay(LocalDate.java:345)
at org.apache.beam.sdk.extensions.sql.impl.rel.BeamCalcRel.toBeamObject(BeamCalcRel.java:443)
Projecting the column (SELECT d FROM t on a table t (id INTEGER, d DATE)) throws a ClassCastException instead:
java.lang.ClassCastException: class org.joda.time.Instant cannot be cast to class java.time.LocalDate
at org.apache.beam.sdk.schemas.logicaltypes.Date.toBaseType(Date.java:35)
Minimal repro, run as a JUnit test in sdks/java/extensions/sql with the DirectRunner (writePipeline and readPipeline are TestPipelines):
BeamSqlEnv env = BeamSqlEnv.inMemory(new ParquetTableProvider());
env.executeDdl("CREATE EXTERNAL TABLE t (d DATE) TYPE parquet LOCATION '/tmp/date-table/'");
BeamSqlRelUtils.toPCollection(
writePipeline, env.parseQuery("INSERT INTO t VALUES (DATE '2025-07-14')"));
writePipeline.run().waitUntilFinish(); // OK: d is written as an Avro int (date)
BeamSqlRelUtils.toPCollection(readPipeline, env.parseQuery("SELECT * FROM t"));
readPipeline.run().waitUntilFinish(); // DateTimeException above
1752451200000 is 2025-07-14T00:00Z in epoch millis, while LocalDate.ofEpochDay expects epoch days.
Cause. The read path gives SQL rows whose schema is inferred from Avro, not the table's schema:
ParquetTable.buildIOReaderreads withParquetIO.read(schema).withBeamSchemas(true)and then appliesConvert.toRows()(L62-L63, L71-L77).- With
withBeamSchemas(true), ParquetIO's coder isAvroUtils.schemaCoder(avroSchema)(ParquetIO L730-L732). That infers the Beam schema from Avro, and an AvrodatebecomesDATETIME(AvroUtils L1058-L1059). Its value is a JodaInstant(convertDateStrict, L1665-L1668). - The table schema says the column is
FieldType.logicalType(SqlTypes.DATE), backed byjava.time.LocalDate(CalciteUtils L111). So SQL receives anInstantwhere it expects aLocalDate.
The write side was fixed in #39414, so only reads are affected now. In the same temporary test, every other scalar type round-trips through INSERT and SELECT *: TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL, BOOLEAN, TIMESTAMP, CHAR, VARCHAR, BINARY and VARBINARY.
Expected: SELECT on a DATE column returns the LocalDate that was written.
Possible fix (I'd like a maintainer's view before sending one, since AvroUtils is shared by many IOs):
- In
AvroUtils, let the strict Avro-to-Beam conversion turn adatevalue into aLocalDatewhen the target field type isSqlTypes.DATE. TodayconvertDateStrictrejects every target exceptDATETIME. This is additive: callers that infer the schema from Avro still getDATETIME. - In
ParquetTable, convert the records with the table's own Beam schema, or its projected subset, instead of the Avro-inferred one. For example, useParquetIO.read(schema)withoutwithBeamSchemas, followed byMapElementswithAvroUtils.getGenericRecordToRowFunction(tableSchema)andsetRowSchema(tableSchema).
Changing AvroUtils.toBeamSchema to infer SqlTypes.DATE from Avro date would also work, but it changes behavior for every Avro-schema user, so I'd rather not.
Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
Issue Components
- [ ] Component: Python SDK
- [x] Component: Java SDK
- [ ] Component: Go SDK
- [ ] Component: Typescript SDK
- [ ] Component: IO connector
- [ ] Component: Beam YAML
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Infrastructure
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Prism Runner
- [ ] Component: Hazelcast Jet Runner
- [ ] Component: Google Cloud Dataflow Runner
@Abacn, since you fixed the write side of this in #39414: does the two-step fix in the description look right to you? Step 1 lets AvroUtils convert an Avro date to LocalDate when the target is SqlTypes.DATE. Step 2 has ParquetTable build rows with the table's schema instead of the Avro-inferred one. Or would you rather fix it somewhere else, for example in ParquetIO? If the approach works for you, I'll send a PR with round-trip tests in ParquetTableProviderTest (SELECT * and a projection) and an AvroUtilsTest case.
.take-issue
.add-labels bug,'awaiting triage'
@ahmedabu98, a polite follow-up, since you worked on the Avro/Beam logical-type conversions in AvroUtils (#36605). Could you take a look at the two-step plan in the description when you have a moment?
- Let
AvroUtils' strict conversion turn an Avrodatevalue into aLocalDatewhen the target field isSqlTypes.DATE. TodayconvertDateStrictonly acceptsDATETIME. This is additive: schemas inferred from Avro still getDATETIME. - Have
ParquetTablebuild rows with the table's own (projected) Beam schema instead of the one ParquetIO infers from Avro.
None of the files involved have changed on master since I filed this. Related: #21003 asks for the inference side (Avro date inferred as Beam DATE). I've left that alone, since it would change inferred schemas for every Avro-based IO.
If you'd rather fix this somewhere else, for example in ParquetIO, I'm happy to do it that way.