Skip to content

Commit e16c027

Browse files
committed
Add QueryResult class and tests
1 parent 9756dff commit e16c027

File tree

5 files changed

+324
-127
lines changed

5 files changed

+324
-127
lines changed

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

+29-97
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.gcloud.bigquery;
1818

1919
import com.google.common.base.MoreObjects;
20-
import com.google.gcloud.Page;
2120

2221
import java.io.Serializable;
2322
import java.util.List;
@@ -35,7 +34,12 @@
3534
* Thread.sleep(1000);
3635
* }
3736
* List<BigQueryError> executionErrors = response.executionErrors();
38-
* Page<List<FieldValue>> rows = response.rows();
37+
* QueryResult result = response.result();
38+
* Iterator<List<FieldValue>> rowIterator = result.iterateAll();
39+
* while(rowIterator.hasNext()) {
40+
* List<FieldValue> row = rowIterator.next();
41+
* // do something with row
42+
* }
3943
* }</pre>
4044
*
4145
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/getQueryResults">Get Query
@@ -46,37 +50,29 @@ public class QueryResponse implements Serializable {
4650

4751
private static final long serialVersionUID = 3549226764825005655L;
4852

53+
private final QueryResult result;
4954
private final String etag;
50-
private final Schema schema;
5155
private final JobId job;
52-
private final Long totalRows;
53-
private final Page<List<FieldValue>> rows;
54-
private final Long totalBytesProcessed;
5556
private final boolean jobComplete;
5657
private final List<BigQueryError> executionErrors;
57-
private final Boolean cacheHit;
5858

5959
static final class Builder {
6060

61+
private QueryResult result;
6162
private String etag;
62-
private Schema schema;
6363
private JobId job;
64-
private Long totalRows;
65-
private Page<List<FieldValue>> rows;
66-
private Long totalBytesProcessed;
6764
private boolean jobComplete;
6865
private List<BigQueryError> executionErrors;
69-
private Boolean cacheHit;
7066

7167
private Builder() {}
7268

73-
Builder etag(String etag) {
74-
this.etag = etag;
69+
Builder result(QueryResult result) {
70+
this.result = result;
7571
return this;
7672
}
7773

78-
Builder schema(Schema schema) {
79-
this.schema = schema;
74+
Builder etag(String etag) {
75+
this.etag = etag;
8076
return this;
8177
}
8278

@@ -85,21 +81,6 @@ Builder job(JobId job) {
8581
return this;
8682
}
8783

88-
Builder totalRows(Long totalRows) {
89-
this.totalRows = totalRows;
90-
return this;
91-
}
92-
93-
Builder rows(Page<List<FieldValue>> rows) {
94-
this.rows = rows;
95-
return this;
96-
}
97-
98-
Builder totalBytesProcessed(Long totalBytesProcessed) {
99-
this.totalBytesProcessed = totalBytesProcessed;
100-
return this;
101-
}
102-
10384
Builder jobComplete(boolean jobComplete) {
10485
this.jobComplete = jobComplete;
10586
return this;
@@ -110,41 +91,32 @@ Builder executionErrors(List<BigQueryError> executionErrors) {
11091
return this;
11192
}
11293

113-
Builder cacheHit(Boolean cacheHit) {
114-
this.cacheHit = cacheHit;
115-
return this;
116-
}
117-
11894
QueryResponse build() {
11995
return new QueryResponse(this);
12096
}
12197
}
12298

12399
private QueryResponse(Builder builder) {
100+
this.result = builder.result;
124101
this.etag = builder.etag;
125-
this.schema = builder.schema;
126102
this.job = builder.job;
127-
this.totalRows = builder.totalRows;
128-
this.rows = builder.rows;
129-
this.totalBytesProcessed = builder.totalBytesProcessed;
130103
this.jobComplete = builder.jobComplete;
131104
this.executionErrors = builder.executionErrors;
132-
this.cacheHit = builder.cacheHit;
133105
}
134106

135107
/**
136-
* Returns the hash of the {@code QueryResponse} resource or {@code null} if not set.
108+
* Returns the result of the query. Returns {@code null} if {@link #jobComplete()} is {@code
109+
* false}.
137110
*/
138-
public String etag() {
139-
return etag;
111+
public QueryResult result() {
112+
return result;
140113
}
141114

142115
/**
143-
* Returns the schema of the results if the query completed successfully. Returns {@code null}
144-
* otherwise.
116+
* Returns the hash of the {@code QueryResponse} resource or {@code null} if not set.
145117
*/
146-
public Schema schema() {
147-
return schema;
118+
public String etag() {
119+
return etag;
148120
}
149121

150122
/**
@@ -156,36 +128,10 @@ public JobId job() {
156128
}
157129

158130
/**
159-
* Returns the total number of rows in the complete query result set, which can be more than the
160-
* number of rows in the first page of results returned by {@link #rows()}. Returns {@code null}
161-
* if the query did not complete successfully.
162-
*/
163-
public Long totalRows() {
164-
return totalRows;
165-
}
166-
167-
/**
168-
* Returns the query result as a paginated list of rows, if the query completed successfully.
169-
* Returns {@code null} otherwise.
170-
*/
171-
public Page<List<FieldValue>> rows() {
172-
return rows;
173-
}
174-
175-
/**
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.
179-
*/
180-
public Long totalBytesProcessed() {
181-
return totalBytesProcessed;
182-
}
183-
184-
/**
185-
* Returns whether the job running the query has completed or not. If {@link #rows()} and
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.
131+
* Returns whether the job running the query has completed or not. If {@link #result()} is not
132+
* {@code null}, this method will always return {@code true}. If this method returns {@code false}
133+
* {@link #result()} returns {@code null}. This method can be used to check if query execution
134+
* completed and results are available.
189135
*/
190136
public boolean jobComplete() {
191137
return jobComplete;
@@ -199,25 +145,14 @@ public List<BigQueryError> executionErrors() {
199145
return executionErrors;
200146
}
201147

202-
/**
203-
* Returns whether the query result was fetched from the query cache.
204-
*
205-
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
206-
*/
207-
public Boolean cacheHit() {
208-
return cacheHit;
209-
}
210-
211148
@Override
212149
public String toString() {
213150
return MoreObjects.toStringHelper(this)
151+
.add("result", result)
152+
.add("etag", etag)
214153
.add("job", job)
215154
.add("jobComplete", jobComplete)
216-
.add("totalRows", totalRows)
217-
.add("schema", schema)
218-
.add("totalBytesProcessed", totalBytesProcessed)
219155
.add("executionErrors", executionErrors)
220-
.add("cacheHit", cacheHit)
221156
.toString();
222157
}
223158

@@ -236,13 +171,10 @@ public boolean equals(Object obj) {
236171
}
237172
QueryResponse response = (QueryResponse) obj;
238173
return jobComplete == response.jobComplete
239-
&& Objects.equals(schema, response.schema)
174+
&& Objects.equals(etag, response.etag)
175+
&& Objects.equals(result, response.result)
240176
&& Objects.equals(job, response.job)
241-
&& Objects.equals(totalRows, response.totalRows)
242-
&& Objects.equals(rows, response.rows)
243-
&& Objects.equals(totalBytesProcessed, response.totalBytesProcessed)
244-
&& Objects.equals(executionErrors, response.executionErrors)
245-
&& Objects.equals(cacheHit, response.cacheHit);
177+
&& Objects.equals(executionErrors, response.executionErrors);
246178
}
247179

248180
static Builder builder() {

0 commit comments

Comments
 (0)