From de19e9fd1f071bd7688832ec6bb7d0886c1ca2e6 Mon Sep 17 00:00:00 2001 From: ajaaym <34161822+ajaaym@users.noreply.github.com> Date: Mon, 19 Nov 2018 17:11:04 -0500 Subject: [PATCH] Fix BigQuery NullPointerException when estimatedFields is empty (#3984) * BigQuery client throws NullPointerException when estimatedFields is empty * fix formatting --- .../cloud/bigquery/StandardTableDefinition.java | 15 ++++++++++----- .../bigquery/StandardTableDefinitionTest.java | 7 +++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java index b4329b19065b..7a90397f8b80 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java @@ -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; @@ -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); } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java index 85649eebc7c9..fb6d810ab028 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java @@ -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; @@ -117,6 +118,12 @@ public void testToAndFromPb() { TableDefinition.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);