github-actions[bot] commented on code in PR #41858:
URL: https://github.com/apache/beam/pull/41858#discussion_r4759557418
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/AvroRowWriter.java:
##########
@@ -56,7 +57,48 @@ public void write(T element) throws IOException,
BigQueryRowSerializationExcepti
} catch (Exception e) {
throw new BigQueryRowSerializationException(e);
}
- writer.append(serializedRequest);
+ try {
+ writer.append(serializedRequest);
+ } catch (DataFileWriter.AppendWriteException e) {
+ org.apache.avro.UnresolvedUnionException unionError =
findUnresolvedUnion(e);
+ if (unionError != null) {
+ throw new
BigQueryRowSerializationException(namespaceMismatchMessage(unionError), e);
Review Comment:
**🔴 Critical**: Throwing `BigQueryRowSerializationException` here causes
`WriteBundlesToFiles` and `WriteGroupedRecordsToFiles` to route the failed
record via `badRecordRouter` and continue writing subsequent records to the
same `AvroRowWriter` instance (when an error handler is configured). However,
Avro's `DataFileWriter.append()` serializes fields sequentially into its
internal `bufferOut` before the nested field fails and does not reset the
buffer on `AppendWriteException` (and may even close the underlying stream if
`sync()` was triggered). Continuing to append to this writer will corrupt the
resulting Avro file.
Additionally, `unionError != null` matches *any* `UnresolvedUnionException`
(such as putting an `Integer` into a nullable `STRING` field `["null",
"string"]`), producing a misleading error message: `Failed to write nested Avro
record 'unknown' because it does not match the schema Beam generated from the
BigQuery table ('unknown')`.
Please only wrap the exception with the namespace mismatch message when the
failure actually involves a `GenericContainer` record and a `RECORD` union
branch, and consider wrapping it in an `IOException` (or
`DataFileWriter.AppendWriteException`) so that `WriteBundlesToFiles` properly
cleans up and closes the tainted writer, unless the `GenericRecord` is
validated prior to calling `writer.append()`.
##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryAvroUtilsTest.java:
##########
@@ -1239,4 +1244,98 @@ public void testSchemaCollisionsInAvroConversion() {
String output = BigQueryAvroUtils.toGenericAvroSchema(schema,
false).toString();
assertThat(output.length(), greaterThan(0));
}
+
+ @Test
+ public void testNullableNestedRecordWithDifferentNamespaceFailsToWrite() {
+ TableFieldSchema street = new
TableFieldSchema().setName("street").setType("STRING");
+ TableFieldSchema city = new
TableFieldSchema().setName("city").setType("STRING");
+ TableFieldSchema address =
+ new TableFieldSchema()
+ .setName("address")
+ .setType("RECORD")
+ .setMode("NULLABLE")
+ .setFields(Lists.newArrayList(street, city));
+ TableSchema table = new
TableSchema().setFields(Lists.newArrayList(address));
+
+ Schema beamSchema = BigQueryAvroUtils.toGenericAvroSchema(table);
+
+ Schema userAddressSchema =
+ SchemaBuilder.record("address")
+ .namespace("com.example.events")
+ .fields()
+ .optionalString("street")
+ .optionalString("city")
+ .endRecord();
+
+ GenericRecord userAddress =
+ new GenericRecordBuilder(userAddressSchema)
+ .set("street", "1 Main St")
+ .set("city", "New York")
+ .build();
+
+ GenericRecord event = new GenericRecordBuilder(beamSchema).set("address",
userAddress).build();
+
+ GenericDatumWriter<GenericRecord> writer = new
GenericDatumWriter<>(beamSchema);
+
+ assertThrows(
+ UnresolvedUnionException.class,
+ () ->
+ writer.write(
+ event, EncoderFactory.get().binaryEncoder(new
ByteArrayOutputStream(), null)));
+ }
+
+ @Test
+ public void testAvroRowWriterReportsNamespaceMismatch() throws Exception {
+ org.apache.beam.sdk.io.FileSystems.setDefaultPipelineOptions(
+ org.apache.beam.sdk.options.PipelineOptionsFactory.create());
+
+ TableFieldSchema street = new
TableFieldSchema().setName("street").setType("STRING");
+ TableFieldSchema city = new
TableFieldSchema().setName("city").setType("STRING");
+ TableFieldSchema address =
+ new TableFieldSchema()
+ .setName("address")
+ .setType("RECORD")
+ .setMode("NULLABLE")
+ .setFields(Lists.newArrayList(street, city));
+ TableSchema table = new
TableSchema().setFields(Lists.newArrayList(address));
+ Schema beamSchema = BigQueryAvroUtils.toGenericAvroSchema(table);
+
+ Schema userAddressSchema =
+ SchemaBuilder.record("address")
+ .namespace("com.example.events")
+ .fields()
+ .optionalString("street")
+ .optionalString("city")
+ .endRecord();
+ GenericRecord userAddress =
+ new GenericRecordBuilder(userAddressSchema)
+ .set("street", "1 Main St")
+ .set("city", "New York")
+ .build();
+
+ String basename =
+
java.nio.file.Files.createTempDirectory("bq-avro").toAbsolutePath().toString()
+ "/out-";
Review Comment:
**🟢 Nit**: Use JUnit's `@Rule public TemporaryFolder tmpFolder = new
TemporaryFolder();` (or delete the directory after the test) so temporary
directories and files are cleaned up after test execution.
--
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]