Skip to content

Commit

Permalink
fix error in BigQueryUtils when retrieving non existent avro field (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
tilgalas authored Apr 1, 2024
1 parent fc5df6f commit d59ef19
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/trigger_files/beam_PostCommit_Java_DataflowV1.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"comment": "Modify this file in a trivial way to cause this test suite to run"
"comment": "Modify this file in a trivial way to cause this test suite to run"
}
3 changes: 3 additions & 0 deletions .github/trigger_files/beam_PostCommit_Java_DataflowV2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"comment": "Modify this file in a trivial way to cause this test suite to run"
}
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,10 @@ public static Row toBeamRow(GenericRecord record, Schema schema, ConversionOptio
.map(
field -> {
try {
return convertAvroFormat(field.getType(), record.get(field.getName()), options);
org.apache.avro.Schema.Field avroField =
record.getSchema().getField(field.getName());
Object value = avroField != null ? record.get(avroField.pos()) : null;
return convertAvroFormat(field.getType(), value, options);
} catch (Exception cause) {
throw new IllegalArgumentException(
"Error converting field " + field + ": " + cause.getMessage(), cause);
Expand Down Expand Up @@ -709,7 +712,8 @@ public static Row toBeamRow(Schema rowSchema, TableSchema bqSchema, TableRow jso
+ jsonBQValue.getClass()
+ "' to '"
+ fieldType
+ "' because the BigQuery type is a List, while the output type is not a collection.");
+ "' because the BigQuery type is a List, while the output type is not a"
+ " collection.");
}

boolean innerTypeIsMap = fieldType.getCollectionElementType().getTypeName().isMapType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,22 @@ public void testToBeamRow_avro_array_array_row() {
assertEquals(expected, beamRow);
}

@Test
public void testToBeamRow_projection() {
long testId = 123L;
// recordSchema is a projection of FLAT_TYPE schema
org.apache.avro.Schema recordSchema =
org.apache.avro.SchemaBuilder.record("__root__").fields().optionalLong("id").endRecord();
GenericData.Record record = new GenericData.Record(recordSchema);
record.put("id", testId);

Row expected = Row.withSchema(FLAT_TYPE).withFieldValue("id", testId).build();
Row actual =
BigQueryUtils.toBeamRow(
record, FLAT_TYPE, BigQueryUtils.ConversionOptions.builder().build());
assertEquals(expected, actual);
}

@Test
public void testToTableSpec() {
TableReference withProject =
Expand Down

0 comments on commit d59ef19

Please sign in to comment.