Skip to content

Commit 4dc2a5e

Browse files
charlesliqlogicJesseLovelace
authored andcommitted
Bigquery: corrected equality check on subclasses of StringEnumValue (#4283)
* Bigquery: corrected equality check on subclasses of StringEnumValue * update format for FieldTest.java
1 parent 3d11659 commit 4dc2a5e

File tree

4 files changed

+30
-8
lines changed
  • google-cloud-clients/google-cloud-bigquery/src
  • google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets

4 files changed

+30
-8
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public Builder setType(LegacySQLTypeName type, Field... subFields) {
125125
* Types</a>
126126
*/
127127
public Builder setType(LegacySQLTypeName type, FieldList subFields) {
128-
if (type == LegacySQLTypeName.RECORD) {
128+
// LegacySQLTypeName is not an enum, cannot use reference equal.
129+
if (LegacySQLTypeName.RECORD.equals(type)) {
129130
if (subFields == null || subFields.isEmpty()) {
130131
throw new IllegalArgumentException(
131132
"The " + type + " field must have at least one sub-field");

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public boolean exists() {
189189
public boolean isDone() {
190190
checkNotDryRun("isDone");
191191
Job job = bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS));
192-
return job == null || job.getStatus().getState() == JobStatus.State.DONE;
192+
return job == null || JobStatus.State.DONE.equals(job.getStatus().getState());
193193
}
194194
/**
195195
* Blocks until this job completes its execution, either failing or succeeding. This method
@@ -293,7 +293,7 @@ public TableResult getQueryResults(QueryResultsOption... options)
293293

294294
// Get the job resource to determine if it has errored.
295295
Job job = this;
296-
if (job.getStatus() == null || job.getStatus().getState() != JobStatus.State.DONE) {
296+
if (job.getStatus() == null || !JobStatus.State.DONE.equals(job.getStatus().getState())) {
297297
job = reload();
298298
}
299299
if (job.getStatus() != null && job.getStatus().getError() != null) {
@@ -362,7 +362,7 @@ public TimedAttemptSettings createNextAttempt(
362362
@Override
363363
public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
364364
return prevResponse != null
365-
&& prevResponse.getStatus().getState() != JobStatus.State.DONE;
365+
&& !JobStatus.State.DONE.equals(prevResponse.getStatus().getState());
366366
}
367367
},
368368
options.getClock());
@@ -377,7 +377,7 @@ public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
377377
* <p>Example of reloading all fields until job status is DONE.
378378
*
379379
* <pre>{@code
380-
* while (job.getStatus().getState() != JobStatus.State.DONE) {
380+
* while (!JobStatus.State.DONE.equals(job.getStatus().getState())) {
381381
* Thread.sleep(1000L);
382382
* job = job.reload();
383383
* }
@@ -386,7 +386,7 @@ public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
386386
* <p>Example of reloading status field until job status is DONE.
387387
*
388388
* <pre>{@code
389-
* while (job.getStatus().getState() != JobStatus.State.DONE) {
389+
* while (!JobStatus.State.DONE.equals(job.getStatus().getState())) {
390390
* Thread.sleep(1000L);
391391
* job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
392392
* }

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020

21+
import java.io.ByteArrayInputStream;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.InputStream;
24+
import java.io.ObjectInputStream;
25+
import java.io.ObjectOutputStream;
2126
import org.junit.Test;
2227

2328
public class FieldTest {
@@ -92,6 +97,22 @@ public void testToAndFromPb() {
9297
compareFieldSchemas(field, Field.fromPb(field.toPb()));
9398
}
9499

100+
@Test
101+
public void testSubFieldWithClonedType() throws Exception {
102+
LegacySQLTypeName record = LegacySQLTypeName.RECORD;
103+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
104+
ObjectOutputStream oos = new ObjectOutputStream(baos);
105+
oos.writeObject(record);
106+
oos.flush();
107+
oos.close();
108+
InputStream is = new ByteArrayInputStream(baos.toByteArray());
109+
ObjectInputStream ois = new ObjectInputStream(is);
110+
LegacySQLTypeName clonedRecord = (LegacySQLTypeName) ois.readObject();
111+
ois.close();
112+
113+
Field.of("field", clonedRecord, Field.of("subfield", LegacySQLTypeName.BOOLEAN));
114+
}
115+
95116
private void compareFieldSchemas(Field expected, Field value) {
96117
assertEquals(expected, value);
97118
assertEquals(expected.getName(), value.getName());

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public boolean waitForWithOptions() throws InterruptedException {
113113
// [TARGET reload(JobOption...)]
114114
public JobStatus.State reload() throws InterruptedException {
115115
// [START ]
116-
while (job.getStatus().getState() != JobStatus.State.DONE) {
116+
while (!JobStatus.State.DONE.equals(job.getStatus().getState())) {
117117
Thread.sleep(1000L);
118118
job = job.reload();
119119
}
@@ -125,7 +125,7 @@ public JobStatus.State reload() throws InterruptedException {
125125
// [TARGET reload(JobOption...)]
126126
public JobStatus.State reloadStatus() throws InterruptedException {
127127
// [START ]
128-
while (job.getStatus().getState() != JobStatus.State.DONE) {
128+
while (!JobStatus.State.DONE.equals(job.getStatus().getState())) {
129129
Thread.sleep(1000L);
130130
job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
131131
}

0 commit comments

Comments
 (0)