Skip to content

Commit c6e53c8

Browse files
committed
Rename QueryResponse.job to jobId, better javadoc, no errors as empty list, add hasErrors
1 parent e16c027 commit c6e53c8

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Google Cloud BigQuery Query Request. This class can be used to run a BigQuery SQL query and
2828
* return results if the query completes within a specified timeout. The query results are saved to
2929
* a temporary table that is deleted approximately 24 hours after the query is run. The query is run
30-
* through a BigQuery Job whose identity can be accessed via {@link QueryResponse#job()}.
30+
* through a BigQuery Job whose identity can be accessed via {@link QueryResponse#jobId()}.
3131
*
3232
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/query">Query</a>
3333
* @see <a href="https://cloud.google.com/bigquery/query-reference">Query Reference</a>

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryResponse.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.gcloud.bigquery;
1818

1919
import com.google.common.base.MoreObjects;
20+
import com.google.common.collect.ImmutableList;
2021

2122
import java.io.Serializable;
2223
import java.util.List;
@@ -30,10 +31,11 @@
3031
* <pre> {@code
3132
* QueryResponse response = bigquery.query(request);
3233
* while (!response.jobComplete()) {
33-
* response = bigquery.getQueryResults(response.job());
34+
* response = bigquery.getQueryResults(response.jobId());
3435
* Thread.sleep(1000);
3536
* }
3637
* List<BigQueryError> executionErrors = response.executionErrors();
38+
* // look for errors in executionErrors
3739
* QueryResult result = response.result();
3840
* Iterator<List<FieldValue>> rowIterator = result.iterateAll();
3941
* while(rowIterator.hasNext()) {
@@ -52,15 +54,15 @@ public class QueryResponse implements Serializable {
5254

5355
private final QueryResult result;
5456
private final String etag;
55-
private final JobId job;
57+
private final JobId jobId;
5658
private final boolean jobComplete;
5759
private final List<BigQueryError> executionErrors;
5860

5961
static final class Builder {
6062

6163
private QueryResult result;
6264
private String etag;
63-
private JobId job;
65+
private JobId jobId;
6466
private boolean jobComplete;
6567
private List<BigQueryError> executionErrors;
6668

@@ -76,8 +78,8 @@ Builder etag(String etag) {
7678
return this;
7779
}
7880

79-
Builder job(JobId job) {
80-
this.job = job;
81+
Builder jobId(JobId jobId) {
82+
this.jobId = jobId;
8183
return this;
8284
}
8385

@@ -99,9 +101,10 @@ QueryResponse build() {
99101
private QueryResponse(Builder builder) {
100102
this.result = builder.result;
101103
this.etag = builder.etag;
102-
this.job = builder.job;
104+
this.jobId = builder.jobId;
103105
this.jobComplete = builder.jobComplete;
104-
this.executionErrors = builder.executionErrors;
106+
this.executionErrors = builder.executionErrors != null ? builder.executionErrors
107+
: ImmutableList.<BigQueryError>of();
105108
}
106109

107110
/**
@@ -123,8 +126,8 @@ public String etag() {
123126
* Returns the identity of the BigQuery Job that was created to run the query. This field will be
124127
* present even if the original request timed out.
125128
*/
126-
public JobId job() {
127-
return job;
129+
public JobId jobId() {
130+
return jobId;
128131
}
129132

130133
/**
@@ -137,6 +140,15 @@ public boolean jobComplete() {
137140
return jobComplete;
138141
}
139142

143+
/**
144+
* Returns whether errors and warnings occurred during the execution of the job. If this method
145+
* returns {@code true} it does not necessarily mean that the job has completed or was
146+
* unsuccessful.
147+
*/
148+
public boolean hasErrors() {
149+
return !executionErrors.isEmpty();
150+
}
151+
140152
/**
141153
* Returns errors and warnings encountered during the running of the job, if any. Errors here do
142154
* not necessarily mean that the job has completed or was unsuccessful.
@@ -150,15 +162,15 @@ public String toString() {
150162
return MoreObjects.toStringHelper(this)
151163
.add("result", result)
152164
.add("etag", etag)
153-
.add("job", job)
165+
.add("jobId", jobId)
154166
.add("jobComplete", jobComplete)
155167
.add("executionErrors", executionErrors)
156168
.toString();
157169
}
158170

159171
@Override
160172
public int hashCode() {
161-
return Objects.hash(job);
173+
return Objects.hash(jobId);
162174
}
163175

164176
@Override
@@ -173,7 +185,7 @@ public boolean equals(Object obj) {
173185
return jobComplete == response.jobComplete
174186
&& Objects.equals(etag, response.etag)
175187
&& Objects.equals(result, response.result)
176-
&& Objects.equals(job, response.job)
188+
&& Objects.equals(jobId, response.jobId)
177189
&& Objects.equals(executionErrors, response.executionErrors);
178190
}
179191

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryResult.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ Builder totalRows(long totalRows) {
7272
Builder pageFetcher(QueryResultsPageFetcher pageFetcher) {
7373
this.pageFetcher = pageFetcher;
7474
return this;
75-
};
75+
}
7676

7777
Builder cursor(String cursor) {
7878
this.cursor = cursor;
7979
return this;
80-
};
80+
}
8181

8282
Builder results(Iterable<List<FieldValue>> results) {
8383
this.results = results;
8484
return this;
85-
};
85+
}
8686

8787
QueryResult build() {
8888
return new QueryResult(this);
@@ -108,7 +108,7 @@ public boolean cacheHit() {
108108
}
109109

110110
/**
111-
* Returns the schema of the results.
111+
* Returns the schema of the results. This is present only when the query completes successfully.
112112
*/
113113
public Schema schema() {
114114
return schema;

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryResponseTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.assertNull;
22+
import static org.junit.Assert.assertTrue;
2223

2324
import com.google.common.collect.ImmutableList;
2425

@@ -62,7 +63,7 @@ public QueryResult nextPage() {
6263
.build();
6364
private static final QueryResponse QUERY_RESPONSE = QueryResponse.builder()
6465
.etag(ETAG)
65-
.job(JOB_ID)
66+
.jobId(JOB_ID)
6667
.jobComplete(JOB_COMPLETE)
6768
.executionErrors(ERRORS)
6869
.result(QUERY_RESULT)
@@ -72,19 +73,21 @@ public QueryResult nextPage() {
7273
public void testBuilder() {
7374
assertEquals(ETAG, QUERY_RESPONSE.etag());
7475
assertEquals(QUERY_RESULT, QUERY_RESPONSE.result());
75-
assertEquals(JOB_ID, QUERY_RESPONSE.job());
76+
assertEquals(JOB_ID, QUERY_RESPONSE.jobId());
7677
assertEquals(JOB_COMPLETE, QUERY_RESPONSE.jobComplete());
7778
assertEquals(ERRORS, QUERY_RESPONSE.executionErrors());
79+
assertTrue(QUERY_RESPONSE.hasErrors());
7880
}
7981

8082
@Test
8183
public void testBuilderIncomplete() {
8284
QueryResponse queryResponse = QueryResponse.builder().jobComplete(false).build();
8385
assertNull(queryResponse.etag());
8486
assertNull(queryResponse.result());
85-
assertNull(queryResponse.job());
87+
assertNull(queryResponse.jobId());
8688
assertFalse(queryResponse.jobComplete());
87-
assertNull(queryResponse.executionErrors());
89+
assertEquals(ImmutableList.<BigQueryError>of(), queryResponse.executionErrors());
90+
assertFalse(queryResponse.hasErrors());
8891
}
8992

9093
@Test
@@ -96,8 +99,9 @@ private void compareQueryResponse(QueryResponse expected, QueryResponse value) {
9699
assertEquals(expected, value);
97100
assertEquals(expected.etag(), value.etag());
98101
assertEquals(expected.result(), value.result());
99-
assertEquals(expected.job(), value.job());
102+
assertEquals(expected.jobId(), value.jobId());
100103
assertEquals(expected.jobComplete(), value.jobComplete());
101104
assertEquals(expected.executionErrors(), value.executionErrors());
105+
assertEquals(expected.hasErrors(), value.hasErrors());
102106
}
103107
}

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public class SerializationTest {
203203
.build();
204204
private static final QueryResponse QUERY_RESPONSE = QueryResponse.builder()
205205
.etag(ETAG)
206-
.job(JOB_ID)
206+
.jobId(JOB_ID)
207207
.jobComplete(true)
208208
.result(QUERY_RESULT)
209209
.build();

0 commit comments

Comments
 (0)