Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error in BigQueryUtils when retrieving non existent avro field #30720

Merged
merged 5 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading