Skip to content

Commit 7af63f7

Browse files
tilgalashjtran
authored andcommitted
fix error in BigQueryUtils when retrieving non existent avro field (apache#30720)
* dump the record schema in BigQueryUtils exception message for debugging purposes * include the right test * check if the field exists in the record before fetching it * add test, restore previous exception message * trigger V2 PostCommit as well
1 parent 354dbb3 commit 7af63f7

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"comment": "Modify this file in a trivial way to cause this test suite to run"
2+
"comment": "Modify this file in a trivial way to cause this test suite to run"
33
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"comment": "Modify this file in a trivial way to cause this test suite to run"
3+
}

sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,10 @@ public static Row toBeamRow(GenericRecord record, Schema schema, ConversionOptio
497497
.map(
498498
field -> {
499499
try {
500-
return convertAvroFormat(field.getType(), record.get(field.getName()), options);
500+
org.apache.avro.Schema.Field avroField =
501+
record.getSchema().getField(field.getName());
502+
Object value = avroField != null ? record.get(avroField.pos()) : null;
503+
return convertAvroFormat(field.getType(), value, options);
501504
} catch (Exception cause) {
502505
throw new IllegalArgumentException(
503506
"Error converting field " + field + ": " + cause.getMessage(), cause);
@@ -709,7 +712,8 @@ public static Row toBeamRow(Schema rowSchema, TableSchema bqSchema, TableRow jso
709712
+ jsonBQValue.getClass()
710713
+ "' to '"
711714
+ fieldType
712-
+ "' because the BigQuery type is a List, while the output type is not a collection.");
715+
+ "' because the BigQuery type is a List, while the output type is not a"
716+
+ " collection.");
713717
}
714718

715719
boolean innerTypeIsMap = fieldType.getCollectionElementType().getTypeName().isMapType();

sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryUtilsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,22 @@ public void testToBeamRow_avro_array_array_row() {
10041004
assertEquals(expected, beamRow);
10051005
}
10061006

1007+
@Test
1008+
public void testToBeamRow_projection() {
1009+
long testId = 123L;
1010+
// recordSchema is a projection of FLAT_TYPE schema
1011+
org.apache.avro.Schema recordSchema =
1012+
org.apache.avro.SchemaBuilder.record("__root__").fields().optionalLong("id").endRecord();
1013+
GenericData.Record record = new GenericData.Record(recordSchema);
1014+
record.put("id", testId);
1015+
1016+
Row expected = Row.withSchema(FLAT_TYPE).withFieldValue("id", testId).build();
1017+
Row actual =
1018+
BigQueryUtils.toBeamRow(
1019+
record, FLAT_TYPE, BigQueryUtils.ConversionOptions.builder().build());
1020+
assertEquals(expected, actual);
1021+
}
1022+
10071023
@Test
10081024
public void testToTableSpec() {
10091025
TableReference withProject =

0 commit comments

Comments
 (0)