PCTEJA opened a new pull request, #41556:
URL: https://github.com/apache/beam/pull/41556

   Fixes #20523.
   
   ## Problem
   
   `NanosInstant` (`beam:logical_type:nanos_instant:v1`) could not be written 
to BigQuery through schemas:
   
   - `NanosInstant` was missing from 
`BigQueryUtils.BEAM_TO_BIGQUERY_LOGICAL_MAPPING`, so `toTableSchema` threw 
`IllegalArgumentException: Cannot convert Beam logical type: 
beam:logical_type:nanos_instant:v1 to BigQuery type.`
   - `BigQueryUtils.toTableRow` had no branch for the identifier, so values 
fell through to `Object.toString()` instead of BigQuery TIMESTAMP formatting.
   
   ## Fix
   
   
`sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryUtils.java`:
   
   - Map `NanosInstant.IDENTIFIER` to `StandardSQLTypeName.TIMESTAMP` in 
`BEAM_TO_BIGQUERY_LOGICAL_MAPPING`.
   - In `toTableRow`, format `NanosInstant` values with the existing 
`BigQueryAvroUtils.formatTimestamp` nanosecond formatter, exactly as the 
`Timestamp` branch does.
   - In `toTableFieldSchema`, the precision branch now covers both 
`Timestamp.IDENTIFIER` and `NanosInstant.IDENTIFIER`, setting `TIMESTAMP` with 
`timestampPrecision(12L)` (BigQuery `TIMESTAMP(12)`), matching the repo's own 
conventions (`BigQueryAvroUtils` timestamp-nanos → `TIMESTAMP(12)`; 
`BeamRowToStorageApiProto` `Timestamp.NANOS` → `TIMESTAMP(12)`). The `precision 
!= 9` validation stays scoped to `Timestamp`, since `NanosInstant` carries no 
precision argument.
   
   ## Tests
   
   
`sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryUtilsTest.java`:
   
   - `testToTableSchema_nanosInstant`: the field maps to `TIMESTAMP` and 
`getTimestampPrecision() == 12L`.
   - `testToTableRow_nanosInstant`: the row formats as `2024-08-10 
16:52:07.123456789 UTC`.
   
   Note: the Storage API write path (`BeamRowToStorageApiProto.LOGICAL_TYPES`) 
and the read-back path (`toBeamValue`) still have no `NanosInstant` branch — 
those are separate follow-ups, out of scope for this `BigQueryUtils` fix.
   


-- 
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]

Reply via email to