From 62ac72f8f540ad6f682b5098f5f1002068278560 Mon Sep 17 00:00:00 2001 From: Ajay Date: Fri, 16 Nov 2018 16:59:46 -0500 Subject: [PATCH 1/2] BigQuery client throws NullPointerException when estimatedFields is empty --- .../cloud/bigquery/StandardTableDefinition.java | 13 ++++++++----- .../cloud/bigquery/StandardTableDefinitionTest.java | 7 +++++++ 2 files changed, 15 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..2211d06d8ebb 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,11 @@ 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..86972674f906 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); From f3b82bbbb35b46d400a9f0b4788ec2db0f3ddc64 Mon Sep 17 00:00:00 2001 From: Ajay Date: Sat, 17 Nov 2018 08:25:10 -0500 Subject: [PATCH 2/2] fix formatting --- .../com/google/cloud/bigquery/StandardTableDefinition.java | 6 ++++-- .../google/cloud/bigquery/StandardTableDefinitionTest.java | 2 +- 2 files changed, 5 insertions(+), 3 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 2211d06d8ebb..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 @@ -115,9 +115,11 @@ static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) { } return new StreamingBuffer( streamingBufferPb.getEstimatedRows() != null - ? streamingBufferPb.getEstimatedRows().longValue() : null, + ? streamingBufferPb.getEstimatedRows().longValue() + : null, streamingBufferPb.getEstimatedBytes() != null - ? streamingBufferPb.getEstimatedBytes().longValue() : 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 86972674f906..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 @@ -121,7 +121,7 @@ public void testToAndFromPb() { @Test public void testFromPbWithNullEstimatedRowsAndBytes() { StandardTableDefinition.fromPb( - TABLE_DEFINITION.toPb().setStreamingBuffer( new Streamingbuffer())); + TABLE_DEFINITION.toPb().setStreamingBuffer(new Streamingbuffer())); } private void compareStandardTableDefinition(StandardTableDefinition expected,