Skip to content

Commit 9756dff

Browse files
committed
Refactor QueryRequest and QueryResponse
- Add better javadoc and snippet to QueryResponse - Use primitive boolean for jobComplete - Add test for NPE in QueryRequestTests - Add QueryRequest and QueryResponse to SerializationTest
1 parent 20d4841 commit 9756dff

File tree

5 files changed

+85
-42
lines changed

5 files changed

+85
-42
lines changed

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
/**
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
29-
* a temporary table that is deleted approximately 24 hours after the query is run. Query is run
29+
* a temporary table that is deleted approximately 24 hours after the query is run. The query is run
3030
* through a BigQuery Job whose identity can be accessed via {@link QueryResponse#job()}.
3131
*
3232
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/query">Query</a>
33+
* @see <a href="https://cloud.google.com/bigquery/query-reference">Query Reference</a>
3334
*/
3435
public class QueryRequest implements Serializable {
3536

@@ -57,7 +58,7 @@ private Builder() {}
5758
* Sets the BigQuery query to be executed.
5859
*/
5960
public Builder query(String query) {
60-
this.query = query;
61+
this.query = checkNotNull(query);
6162
return this;
6263
}
6364

@@ -84,19 +85,18 @@ public Builder defaultDataset(DatasetId defaultDataset) {
8485
* Sets how long to wait for the query to complete, in milliseconds, before the request times
8586
* out and returns. Note that this is only a timeout for the request, not the query. If the
8687
* query takes longer to run than the timeout value, the call returns without any results and
87-
* with the {@link QueryResponse#jobComplete()} set to {@code false}. You can call
88-
* {@link BigQuery#getQueryResults(JobId, BigQuery.QueryResultsOption...)} to wait for the query
89-
* to complete and read the results. The default value is 10000 milliseconds (10 seconds).
88+
* with the {@link QueryResponse#jobComplete()} set to {@code false}. If not set, a wait time of
89+
* 10000 milliseconds (10 seconds) is used.
9090
*/
9191
public Builder maxWaitTime(Long maxWaitTime) {
9292
this.maxWaitTime = maxWaitTime;
9393
return this;
9494
}
9595

9696
/**
97-
* Sets whether the query has to be dry run or not. If set, the query is not executed: if the
98-
* query is valid statistics are returned on how many bytes would be processed, if the query is
99-
* invalid an error is returned.
97+
* Sets whether the query has to be dry run or not. If set, the query is not executed. If the
98+
* query is valid statistics are returned on how many bytes would be processed. If the query is
99+
* invalid an error is returned. If not set the query is executed.
100100
*/
101101
public Builder dryRun(Boolean dryRun) {
102102
this.dryRun = dryRun;
@@ -105,7 +105,8 @@ public Builder dryRun(Boolean dryRun) {
105105

106106
/**
107107
* Sets whether to look for the result in the query cache. The query cache is a best-effort
108-
* cache that will be flushed whenever tables in the query are modified.
108+
* cache that will be flushed whenever tables in the query are modified. If not specified the
109+
* query cache is used.
109110
*
110111
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
111112
*/
@@ -120,7 +121,7 @@ public QueryRequest build() {
120121
}
121122

122123
private QueryRequest(Builder builder) {
123-
query = checkNotNull(builder.query);
124+
query = builder.query;
124125
maxResults = builder.maxResults;
125126
defaultDataset = builder.defaultDataset;
126127
maxWaitTime = builder.maxWaitTime;
@@ -155,24 +156,26 @@ public DatasetId defaultDataset() {
155156
* query takes longer to run than the timeout value, the call returns without any results and
156157
* with the {@link QueryResponse#jobComplete()} set to {@code false}. You can call
157158
* {@link BigQuery#getQueryResults(JobId, BigQuery.QueryResultsOption...)} to wait for the query
158-
* to complete and read the results. The default value is 10000 milliseconds (10 seconds).
159+
* to complete and read the results. If not set, a wait time of 10000 milliseconds (10 seconds)
160+
* is used.
159161
*/
160162
public Long maxWaitTime() {
161163
return maxWaitTime;
162164
}
163165

164166
/**
165-
* Returns whether the query has to be dry run or not. If set, the query is not executed: if the
166-
* query is valid statistics are returned on how many bytes would be processed, if the query is
167-
* invalid an error is returned.
167+
* Returns whether the query has to be dry run or not. If set, the query is not executed. If the
168+
* query is valid statistics are returned on how many bytes would be processed. If the query is
169+
* invalid an error is returned. If not set the query is executed.
168170
*/
169171
public Boolean dryRun() {
170172
return dryRun;
171173
}
172174

173175
/**
174176
* Returns whether to look for the result in the query cache. The query cache is a best-effort
175-
* cache that will be flushed whenever tables in the query are modified.
177+
* cache that will be flushed whenever tables in the query are modified. If not specified the
178+
* query cache is used.
176179
*
177180
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
178181
*/

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

+38-20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
/**
2727
* Google Cloud BigQuery Query Response. This class contains the results of a Query Job or of a
2828
* Query Request.
29+
*
30+
* <p>Example usage of a query response:
31+
* <pre> {@code
32+
* QueryResponse response = bigquery.query(request);
33+
* while (!response.jobComplete()) {
34+
* response = bigquery.getQueryResults(response.job());
35+
* Thread.sleep(1000);
36+
* }
37+
* List<BigQueryError> executionErrors = response.executionErrors();
38+
* Page<List<FieldValue>> rows = response.rows();
39+
* }</pre>
40+
*
41+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/getQueryResults">Get Query
42+
* Results</a>
43+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/query">Query</a>
2944
*/
3045
public class QueryResponse implements Serializable {
3146

@@ -37,8 +52,8 @@ public class QueryResponse implements Serializable {
3752
private final Long totalRows;
3853
private final Page<List<FieldValue>> rows;
3954
private final Long totalBytesProcessed;
40-
private final Boolean jobComplete;
41-
private final List<BigQueryError> errors;
55+
private final boolean jobComplete;
56+
private final List<BigQueryError> executionErrors;
4257
private final Boolean cacheHit;
4358

4459
static final class Builder {
@@ -49,8 +64,8 @@ static final class Builder {
4964
private Long totalRows;
5065
private Page<List<FieldValue>> rows;
5166
private Long totalBytesProcessed;
52-
private Boolean jobComplete;
53-
private List<BigQueryError> errors;
67+
private boolean jobComplete;
68+
private List<BigQueryError> executionErrors;
5469
private Boolean cacheHit;
5570

5671
private Builder() {}
@@ -85,13 +100,13 @@ Builder totalBytesProcessed(Long totalBytesProcessed) {
85100
return this;
86101
}
87102

88-
Builder jobComplete(Boolean jobComplete) {
103+
Builder jobComplete(boolean jobComplete) {
89104
this.jobComplete = jobComplete;
90105
return this;
91106
}
92107

93-
Builder errors(List<BigQueryError> errors) {
94-
this.errors = errors;
108+
Builder executionErrors(List<BigQueryError> executionErrors) {
109+
this.executionErrors = executionErrors;
95110
return this;
96111
}
97112

@@ -113,7 +128,7 @@ private QueryResponse(Builder builder) {
113128
this.rows = builder.rows;
114129
this.totalBytesProcessed = builder.totalBytesProcessed;
115130
this.jobComplete = builder.jobComplete;
116-
this.errors = builder.errors;
131+
this.executionErrors = builder.executionErrors;
117132
this.cacheHit = builder.cacheHit;
118133
}
119134

@@ -125,7 +140,7 @@ public String etag() {
125140
}
126141

127142
/**
128-
* Returns the schema of the results when the query completed successfully. Returns {@code null}
143+
* Returns the schema of the results if the query completed successfully. Returns {@code null}
129144
* otherwise.
130145
*/
131146
public Schema schema() {
@@ -158,27 +173,30 @@ public Page<List<FieldValue>> rows() {
158173
}
159174

160175
/**
161-
* Returns the total number of bytes processed for the query.
176+
* Returns the total number of bytes processed for the query. If this query was a dry run, this is
177+
* the number of bytes that would be processed if the query were run. Returns {@code null}
178+
* if the query did not complete.
162179
*/
163180
public Long totalBytesProcessed() {
164181
return totalBytesProcessed;
165182
}
166183

167184
/**
168185
* Returns whether the job running the query has completed or not. If {@link #rows()} and
169-
* {@link #totalRows()} are present, this method will always return {@code true}. If this method
170-
* returns {@code false}, {@link #totalRows()} will not be available.
186+
* {@link #totalRows()} are not {@code null}, this method will always return {@code true}. If this
187+
* method returns {@code false} {@link #totalRows()} and {@link #rows()} return {@code null}. This
188+
* method can be used to check if query execution completed and results are available.
171189
*/
172-
public Boolean jobComplete() {
190+
public boolean jobComplete() {
173191
return jobComplete;
174192
}
175193

176194
/**
177195
* Returns errors and warnings encountered during the running of the job, if any. Errors here do
178196
* not necessarily mean that the job has completed or was unsuccessful.
179197
*/
180-
public List<BigQueryError> errors() {
181-
return errors;
198+
public List<BigQueryError> executionErrors() {
199+
return executionErrors;
182200
}
183201

184202
/**
@@ -198,7 +216,7 @@ public String toString() {
198216
.add("totalRows", totalRows)
199217
.add("schema", schema)
200218
.add("totalBytesProcessed", totalBytesProcessed)
201-
.add("errors", errors)
219+
.add("executionErrors", executionErrors)
202220
.add("cacheHit", cacheHit)
203221
.toString();
204222
}
@@ -217,17 +235,17 @@ public boolean equals(Object obj) {
217235
return false;
218236
}
219237
QueryResponse response = (QueryResponse) obj;
220-
return Objects.equals(schema, response.schema)
238+
return jobComplete == response.jobComplete
239+
&& Objects.equals(schema, response.schema)
221240
&& Objects.equals(job, response.job)
222241
&& Objects.equals(totalRows, response.totalRows)
223242
&& Objects.equals(rows, response.rows)
224243
&& Objects.equals(totalBytesProcessed, response.totalBytesProcessed)
225-
&& Objects.equals(jobComplete, response.jobComplete)
226-
&& Objects.equals(errors, response.errors)
244+
&& Objects.equals(executionErrors, response.executionErrors)
227245
&& Objects.equals(cacheHit, response.cacheHit);
228246
}
229247

230248
static Builder builder() {
231249
return new Builder();
232250
}
233-
}
251+
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNull;
2121

22+
import org.junit.Rule;
2223
import org.junit.Test;
24+
import org.junit.rules.ExpectedException;
2325

2426
public class QueryRequestTest {
2527

@@ -37,6 +39,9 @@ public class QueryRequestTest {
3739
.maxWaitTime(MAX_WAIT_TIME)
3840
.build();
3941

42+
@Rule
43+
public ExpectedException thrown = ExpectedException.none();
44+
4045
@Test
4146
public void testToBuilder() {
4247
compareQueryRequest(QUERY_REQUEST, QUERY_REQUEST.toBuilder().build());
@@ -62,6 +67,8 @@ public void testBuilder() {
6267
assertEquals(DRY_RUN, QUERY_REQUEST.dryRun());
6368
assertEquals(MAX_RESULTS, QUERY_REQUEST.maxResults());
6469
assertEquals(MAX_WAIT_TIME, QUERY_REQUEST.maxWaitTime());
70+
thrown.expect(NullPointerException.class);
71+
QueryRequest.builder(null);
6572
}
6673

6774
@Test
@@ -73,6 +80,8 @@ public void testOf() {
7380
assertNull(request.dryRun());
7481
assertNull(request.maxResults());
7582
assertNull(request.maxWaitTime());
83+
thrown.expect(NullPointerException.class);
84+
QueryRequest.of(null);
7685
}
7786

7887
@Test

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Page<List<FieldValue>> nextPage() {
4646
}
4747
};
4848
private static final Page<List<FieldValue>> ROWS =
49-
new PageImpl<List<FieldValue>>(FETCHER, "cursor", ImmutableList.<List<FieldValue>>of());
49+
new PageImpl<>(FETCHER, "cursor", ImmutableList.<List<FieldValue>>of());
5050
private static final Long TOTAL_BYTES_PROCESSED = 4200L;
5151
private static final Boolean JOB_COMPLETE = true;
5252
private static final List<BigQueryError> ERRORS = ImmutableList.of(
@@ -62,7 +62,7 @@ public Page<List<FieldValue>> nextPage() {
6262
.rows(ROWS)
6363
.totalBytesProcessed(TOTAL_BYTES_PROCESSED)
6464
.jobComplete(JOB_COMPLETE)
65-
.errors(ERRORS)
65+
.executionErrors(ERRORS)
6666
.cacheHit(CACHE_HIT)
6767
.build();
6868

@@ -73,9 +73,9 @@ public void testBuilder() {
7373
assertEquals(JOB_ID, QUERY_RESPONSE.job());
7474
assertEquals(TOTAL_ROWS, QUERY_RESPONSE.totalRows());
7575
assertEquals(ROWS, QUERY_RESPONSE.rows());
76-
assertEquals(TOTAL_BYTES_PROCESSED, QUERY_RESPONSE.totalBytesProcessed());
76+
assertEquals(TOTAL_BYTES_PROCESSED, (Long) QUERY_RESPONSE.totalBytesProcessed());
7777
assertEquals(JOB_COMPLETE, QUERY_RESPONSE.jobComplete());
78-
assertEquals(ERRORS, QUERY_RESPONSE.errors());
78+
assertEquals(ERRORS, QUERY_RESPONSE.executionErrors());
7979
assertEquals(CACHE_HIT, QUERY_RESPONSE.cacheHit());
8080
}
8181

@@ -89,7 +89,7 @@ public void testBuilderIncomplete() {
8989
assertNull(queryResponse.rows());
9090
assertNull(queryResponse.totalBytesProcessed());
9191
assertEquals(false, queryResponse.jobComplete());
92-
assertNull(queryResponse.errors());
92+
assertNull(queryResponse.executionErrors());
9393
assertNull(queryResponse.cacheHit());
9494
}
9595

@@ -107,7 +107,7 @@ private void compareQueryResponse(QueryResponse expected, QueryResponse value) {
107107
assertEquals(expected.rows(), value.rows());
108108
assertEquals(expected.totalBytesProcessed(), value.totalBytesProcessed());
109109
assertEquals(expected.jobComplete(), value.jobComplete());
110-
assertEquals(expected.errors(), value.errors());
110+
assertEquals(expected.executionErrors(), value.executionErrors());
111111
assertEquals(expected.cacheHit(), value.cacheHit());
112112
}
113113
}

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ public class SerializationTest {
186186
private static final InsertAllResponse INSERT_ALL_RESPONSE = new InsertAllResponse(ERRORS_MAP);
187187
private static final FieldValue FIELD_VALUE =
188188
new FieldValue(FieldValue.Attribute.PRIMITIVE, "value");
189+
private static final QueryRequest QUERY_REQUEST = QueryRequest.builder("query")
190+
.useQueryCache(true)
191+
.defaultDataset(DATASET_ID)
192+
.dryRun(false)
193+
.maxResults(42L)
194+
.maxWaitTime(10L)
195+
.build();
196+
private static final QueryResponse QUERY_RESPONSE = QueryResponse.builder()
197+
.etag(ETAG)
198+
.schema(TABLE_SCHEMA)
199+
.job(JOB_ID)
200+
.totalRows(1L)
201+
.build();
189202

190203
@Test
191204
public void testServiceOptions() throws Exception {
@@ -212,7 +225,7 @@ public void testModelAndRequests() throws Exception {
212225
TABLE_SCHEMA, TABLE_INFO, VIEW_INFO, EXTERNAL_TABLE_INFO, INLINE_FUNCTION, URI_FUNCTION,
213226
JOB_STATISTICS, EXTRACT_STATISTICS, LOAD_STATISTICS, QUERY_STATISTICS, BIGQUERY_ERROR,
214227
JOB_STATUS, JOB_ID, COPY_JOB, EXTRACT_JOB, LOAD_JOB, QUERY_JOB, INSERT_ALL_REQUEST,
215-
INSERT_ALL_RESPONSE, FIELD_VALUE};
228+
INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE};
216229
for (Serializable obj : objects) {
217230
Object copy = serializeAndDeserialize(obj);
218231
assertEquals(obj, obj);

0 commit comments

Comments
 (0)