github-actions[bot] commented on code in PR #41556:
URL: https://github.com/apache/beam/pull/41556#discussion_r4657004441
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryUtils.java:
##########
@@ -585,12 +587,18 @@ private static List<TableFieldSchema>
toTableFieldSchema(Schema schema) {
field.setMode(Mode.REPEATED.toString());
}
Schema.LogicalType<?, ?> logicalType = type.getLogicalType();
- if (logicalType != null &&
Timestamp.IDENTIFIER.equals(logicalType.getIdentifier())) {
- int precision =
Preconditions.checkArgumentNotNull(logicalType.getArgument());
- if (precision != 9) {
- throw new IllegalArgumentException(
- "Unsupported precision for Timestamp logical type " + precision);
+ if (logicalType != null
+ && (Timestamp.IDENTIFIER.equals(logicalType.getIdentifier())
+ || NanosInstant.IDENTIFIER.equals(logicalType.getIdentifier())))
{
+ if (Timestamp.IDENTIFIER.equals(logicalType.getIdentifier())) {
+ int precision =
Preconditions.checkArgumentNotNull(logicalType.getArgument());
+ if (precision != 9) {
+ throw new IllegalArgumentException(
+ "Unsupported precision for Timestamp logical type " +
precision);
+ }
}
+ // Both Timestamp.NANOS and NanosInstant carry nanosecond values,
which BigQuery
+ // stores in TIMESTAMP(12) columns.
field.setType(StandardSQLTypeName.TIMESTAMP.toString()).setTimestampPrecision(12L);
Review Comment:
**🔴 Critical**: Setting `timestampPrecision(12L)` on `TableFieldSchema` for
`NanosInstant` while only updating `BigQueryUtils` leaves
`BigQueryIO.Write.useBeamSchema()` broken across its write methods:
1. **`STORAGE_WRITE_API` / `STORAGE_API_AT_LEAST_ONCE`**:
`BeamRowToStorageApiProto` (`fieldDescriptorFromBeamField` and
`scalarToProtoValue`) does not handle `NanosInstant.IDENTIFIER`. Writing a
`Row` with `NanosInstant` via Storage Write API will fail with
`RuntimeException: Unsupported logical type`.
2. **`FILE_LOADS` (default batch method)**: When `.useBeamSchema()` is used
with `FILE_LOADS`, `BigQueryIO` uses `AvroRowWriter` and
`AvroUtils.toAvroSchema`, which throws `RuntimeException: Unhandled logical
type beam:logical_type:nanos_instant:v1`. Furthermore, BigQuery `FILE_LOADS`
jobs do not support `timestampPrecision = 12L`.
3. **`STREAMING_INSERTS`**: Uses `toTableSchema` and `toTableRow`, but
`TableDataInsertAll` does not support `TIMESTAMP(12)` columns.
To properly support writing `NanosInstant` to BigQuery,
`BeamRowToStorageApiProto` (and `AvroUtils` if Avro conversions are expected)
also need to handle `NanosInstant.IDENTIFIER`.
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryUtils.java:
##########
@@ -837,6 +845,8 @@ public static TableRow toTableRow(Row row) {
return localDateTimeFormatter.format(localDateTime);
} else if (Timestamp.IDENTIFIER.equals(identifier)) {
return BigQueryAvroUtils.formatTimestamp((java.time.Instant)
fieldValue);
+ } else if (NanosInstant.IDENTIFIER.equals(identifier)) {
Review Comment:
**🟢 Low**: Since this branch is identical to the `Timestamp.IDENTIFIER`
branch directly above it, they can be combined into a single condition: `} else
if (Timestamp.IDENTIFIER.equals(identifier) ||
NanosInstant.IDENTIFIER.equals(identifier)) {`.
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryUtils.java:
##########
@@ -343,6 +344,7 @@ public abstract static class Builder {
.put(SqlTypes.TIME.getIdentifier(), StandardSQLTypeName.TIME)
.put(SqlTypes.DATETIME.getIdentifier(), StandardSQLTypeName.DATETIME)
.put(SqlTypes.TIMESTAMP.getIdentifier(),
StandardSQLTypeName.TIMESTAMP)
+ .put(NanosInstant.IDENTIFIER, StandardSQLTypeName.TIMESTAMP)
Review Comment:
**🟡 Medium**: Notice that `toBeamRow` (around line 927 in
`BigQueryUtils.java`) handles reading `TIMESTAMP` values back into Beam `Row`s
for `SqlTypes.TIMESTAMP` (`MicrosInstant`) and `Timestamp.IDENTIFIER`, but does
not have a branch for `NanosInstant.IDENTIFIER`. If a schema containing
`NanosInstant` is used with `BigQueryUtils.toBeamRow`, `fromBigQueryFieldValue`
will fall through and fail to parse the string into a `java.time.Instant`.
--
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]