Skip to content

Commit

Permalink
Fix BigQuery NullPointerException when estimatedFields is empty (#3984)
Browse files Browse the repository at this point in the history
* BigQuery client throws NullPointerException when estimatedFields is empty

* fix formatting
  • Loading branch information
ajaaym authored and chingor13 committed Nov 19, 2018
1 parent dc0a1ae commit de19e9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public abstract class StandardTableDefinition extends TableDefinition {
public static class StreamingBuffer implements Serializable {

private static final long serialVersionUID = 822027055549277843L;
private final long estimatedRows;
private final long estimatedBytes;
private final Long estimatedRows;
private final Long estimatedBytes;
private final Long oldestEntryTime;

StreamingBuffer(long estimatedRows, long estimatedBytes, Long oldestEntryTime) {
StreamingBuffer(Long estimatedRows, Long estimatedBytes, Long oldestEntryTime) {
this.estimatedRows = estimatedRows;
this.estimatedBytes = estimatedBytes;
this.oldestEntryTime = oldestEntryTime;
Expand Down Expand Up @@ -113,8 +113,13 @@ static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) {
if (streamingBufferPb.getOldestEntryTime() != null) {
oldestEntryTime = streamingBufferPb.getOldestEntryTime().longValue();
}
return new StreamingBuffer(streamingBufferPb.getEstimatedRows().longValue(),
streamingBufferPb.getEstimatedBytes().longValue(),
return new StreamingBuffer(
streamingBufferPb.getEstimatedRows() != null
? streamingBufferPb.getEstimatedRows().longValue()
: null,
streamingBufferPb.getEstimatedBytes() != null
? streamingBufferPb.getEstimatedBytes().longValue()
: null,
oldestEntryTime);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.api.services.bigquery.model.Streamingbuffer;
import com.google.cloud.bigquery.StandardTableDefinition.StreamingBuffer;

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

@Test
public void testFromPbWithNullEstimatedRowsAndBytes() {
StandardTableDefinition.fromPb(
TABLE_DEFINITION.toPb().setStreamingBuffer(new Streamingbuffer()));
}

private void compareStandardTableDefinition(StandardTableDefinition expected,
StandardTableDefinition value) {
assertEquals(expected, value);
Expand Down

0 comments on commit de19e9f

Please sign in to comment.