Skip to content

Commit de19e9f

Browse files
ajaaymchingor13
authored andcommitted
Fix BigQuery NullPointerException when estimatedFields is empty (#3984)
* BigQuery client throws NullPointerException when estimatedFields is empty * fix formatting
1 parent dc0a1ae commit de19e9f

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public abstract class StandardTableDefinition extends TableDefinition {
4646
public static class StreamingBuffer implements Serializable {
4747

4848
private static final long serialVersionUID = 822027055549277843L;
49-
private final long estimatedRows;
50-
private final long estimatedBytes;
49+
private final Long estimatedRows;
50+
private final Long estimatedBytes;
5151
private final Long oldestEntryTime;
5252

53-
StreamingBuffer(long estimatedRows, long estimatedBytes, Long oldestEntryTime) {
53+
StreamingBuffer(Long estimatedRows, Long estimatedBytes, Long oldestEntryTime) {
5454
this.estimatedRows = estimatedRows;
5555
this.estimatedBytes = estimatedBytes;
5656
this.oldestEntryTime = oldestEntryTime;
@@ -113,8 +113,13 @@ static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) {
113113
if (streamingBufferPb.getOldestEntryTime() != null) {
114114
oldestEntryTime = streamingBufferPb.getOldestEntryTime().longValue();
115115
}
116-
return new StreamingBuffer(streamingBufferPb.getEstimatedRows().longValue(),
117-
streamingBufferPb.getEstimatedBytes().longValue(),
116+
return new StreamingBuffer(
117+
streamingBufferPb.getEstimatedRows() != null
118+
? streamingBufferPb.getEstimatedRows().longValue()
119+
: null,
120+
streamingBufferPb.getEstimatedBytes() != null
121+
? streamingBufferPb.getEstimatedBytes().longValue()
122+
: null,
118123
oldestEntryTime);
119124
}
120125
}

google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertNull;
2121
import static org.junit.Assert.assertTrue;
2222

23+
import com.google.api.services.bigquery.model.Streamingbuffer;
2324
import com.google.cloud.bigquery.StandardTableDefinition.StreamingBuffer;
2425

2526
import com.google.common.collect.ImmutableList;
@@ -117,6 +118,12 @@ public void testToAndFromPb() {
117118
TableDefinition.<StandardTableDefinition>fromPb(definition.toPb()));
118119
}
119120

121+
@Test
122+
public void testFromPbWithNullEstimatedRowsAndBytes() {
123+
StandardTableDefinition.fromPb(
124+
TABLE_DEFINITION.toPb().setStreamingBuffer(new Streamingbuffer()));
125+
}
126+
120127
private void compareStandardTableDefinition(StandardTableDefinition expected,
121128
StandardTableDefinition value) {
122129
assertEquals(expected, value);

0 commit comments

Comments
 (0)