Skip to content

Commit cf55fee

Browse files
authored
Fix Null Pointer Exception in BigQueryIO (#36209)
1 parent 9146029 commit cf55fee

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public void validate(@Nullable PipelineOptions maybeOptions) {
285285
PipelineOptions options = Preconditions.checkArgumentNotNull(maybeOptions);
286286
// We will use a BigQuery load job -- validate the temp location.
287287
String tempLocation;
288-
if (customGcsTempLocation == null) {
288+
if (customGcsTempLocation == null || customGcsTempLocation.get() == null) {
289289
tempLocation = options.getTempLocation();
290290
} else {
291291
if (!customGcsTempLocation.isAccessible()) {
@@ -589,7 +589,7 @@ private PCollectionView<String> createTempFilePrefixView(
589589
@ProcessElement
590590
public void getTempFilePrefix(ProcessContext c) {
591591
String tempLocationRoot;
592-
if (customGcsTempLocation != null) {
592+
if (customGcsTempLocation != null && customGcsTempLocation.get() != null) {
593593
tempLocationRoot = customGcsTempLocation.get();
594594
} else {
595595
tempLocationRoot = c.getPipelineOptions().getTempLocation();

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4441,4 +4441,29 @@ public void testUpsertAndDeleteBeamRows() throws Exception {
44414441
fakeDatasetService.getAllRows("project-id", "dataset-id", "table-id"),
44424442
containsInAnyOrder(Iterables.toArray(expected, TableRow.class)));
44434443
}
4444+
4445+
@Test
4446+
public void testCustomGcsTempLocationNull() throws Exception {
4447+
BigQueryIO.Write<TableRow> write =
4448+
BigQueryIO.writeTableRows()
4449+
.to("dataset-id.table-id")
4450+
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
4451+
.withSchema(
4452+
new TableSchema()
4453+
.setFields(
4454+
ImmutableList.of(new TableFieldSchema().setName("name").setType("STRING"))))
4455+
.withMethod(Method.FILE_LOADS)
4456+
.withoutValidation()
4457+
.withTestServices(fakeBqServices)
4458+
.withCustomGcsTempLocation(ValueProvider.StaticValueProvider.of(null));
4459+
4460+
p.apply(
4461+
Create.of(new TableRow().set("name", "a"), new TableRow().set("name", "b"))
4462+
.withCoder(TableRowJsonCoder.of()))
4463+
.apply("WriteToBQ", write);
4464+
p.run();
4465+
assertThat(
4466+
fakeDatasetService.getAllRows("project-id", "dataset-id", "table-id"),
4467+
containsInAnyOrder(new TableRow().set("name", "a"), new TableRow().set("name", "b")));
4468+
}
44444469
}

0 commit comments

Comments
 (0)