Skip to content

Commit

Permalink
null arrays default to empty array (#33919)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedabu98 authored Feb 11, 2025
1 parent f74ea9d commit 2b18c28
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -714,6 +715,9 @@ public static Row toBeamRow(Schema rowSchema, TableSchema bqSchema, TableRow jso
if (fieldType.getNullable()) {
return null;
} else {
if (fieldType.getTypeName().isCollectionType()) {
return Collections.emptyList();
}
throw new IllegalArgumentException(
"Received null value for non-nullable field \"" + field.getName() + "\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ public class BigQueryUtilsTest {

private static final TableRow BQ_ENUM_ROW = new TableRow().set("color", "GREEN");

private static final Row NULL_ARRAY_ROW =
Row.withSchema(ARRAY_TYPE_NULLS).addValue(Collections.emptyList()).build();

private static final Row ARRAY_ROW_NULLS =
Row.withSchema(ARRAY_TYPE_NULLS).addValues((Object) Arrays.asList(123L, null, null)).build();

Expand All @@ -459,6 +462,8 @@ public class BigQueryUtilsTest {
private static final Row MAP_ROW =
Row.withSchema(MAP_MAP_TYPE).addValues(ImmutableMap.of("test", 123.456)).build();

private static final TableRow BQ_NULL_ARRAY_ROW = new TableRow().set("ids", null);

private static final TableRow BQ_ARRAY_ROW_NULLS =
new TableRow()
.set(
Expand Down Expand Up @@ -1021,6 +1026,12 @@ public void testToBeamRow_enum() {
assertEquals(ENUM_STRING_ROW, beamRow);
}

@Test
public void testToBeamRow_nullArray() {
Row beamRow = BigQueryUtils.toBeamRow(ARRAY_TYPE_NULLS, BQ_NULL_ARRAY_ROW);
assertEquals(NULL_ARRAY_ROW, beamRow);
}

@Test
public void testToBeamRow_arrayNulls() {
Row beamRow = BigQueryUtils.toBeamRow(ARRAY_TYPE_NULLS, BQ_ARRAY_ROW_NULLS);
Expand Down

0 comments on commit 2b18c28

Please sign in to comment.