Skip to content

Commit ea54699

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

5 files changed

Lines changed: 318 additions & 120 deletions

File tree

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

Lines changed: 29 additions & 97 deletions
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() {
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.bigquery;
18+
19+
import com.google.common.base.MoreObjects;
20+
import com.google.common.collect.ImmutableList;
21+
import com.google.gcloud.PageImpl;
22+
23+
import java.util.List;
24+
import java.util.Objects;
25+
26+
public class QueryResult extends PageImpl<List<FieldValue>> {
27+
28+
private static final long serialVersionUID = -4831062717210349818L;
29+
30+
private final boolean cacheHit;
31+
private final Schema schema;
32+
private final long totalRows;
33+
private final long totalBytesProcessed;
34+
35+
interface QueryResultsPageFetcher extends PageImpl.NextPageFetcher<List<FieldValue>> {
36+
@Override
37+
QueryResult nextPage();
38+
}
39+
40+
static final class Builder {
41+
42+
private NextPageFetcher<List<FieldValue>> pageFetcher;
43+
private String cursor;
44+
private Iterable<List<FieldValue>> results;
45+
private boolean cacheHit;
46+
private Schema schema;
47+
private long totalRows;
48+
private long totalBytesProcessed;
49+
50+
private Builder() {}
51+
52+
Builder cacheHit(boolean cacheHit) {
53+
this.cacheHit = cacheHit;
54+
return this;
55+
}
56+
57+
Builder schema(Schema schema) {
58+
this.schema = schema;
59+
return this;
60+
}
61+
62+
Builder totalBytesProcessed(long totalBytesProcessed) {
63+
this.totalBytesProcessed = totalBytesProcessed;
64+
return this;
65+
}
66+
67+
Builder totalRows(long totalRows) {
68+
this.totalRows = totalRows;
69+
return this;
70+
}
71+
72+
Builder pageFetcher(NextPageFetcher<List<FieldValue>> pageFetcher) {
73+
this.pageFetcher = pageFetcher;
74+
return this;
75+
};
76+
77+
Builder cursor(String cursor) {
78+
this.cursor = cursor;
79+
return this;
80+
};
81+
82+
Builder results(Iterable<List<FieldValue>> results) {
83+
this.results = results;
84+
return this;
85+
};
86+
87+
QueryResult build() {
88+
return new QueryResult(this);
89+
}
90+
}
91+
92+
private QueryResult(Builder builder) {
93+
super(builder.pageFetcher, builder.cursor, builder.results != null ? builder.results
94+
: ImmutableList.<List<FieldValue>>of());
95+
this.cacheHit = builder.cacheHit;
96+
this.schema = builder.schema;
97+
this.totalBytesProcessed = builder.totalBytesProcessed;
98+
this.totalRows = builder.totalRows;
99+
}
100+
101+
/**
102+
* Returns whether the query result was fetched from the query cache.
103+
*
104+
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
105+
*/
106+
public boolean cacheHit() {
107+
return cacheHit;
108+
}
109+
110+
/**
111+
* Returns the schema of the results.
112+
*/
113+
public Schema schema() {
114+
return schema;
115+
}
116+
117+
/**
118+
* Returns the total number of bytes processed for the query. If this query was a dry run, this is
119+
* the number of bytes that would be processed if the query were run.
120+
*/
121+
public long totalBytesProcessed() {
122+
return totalBytesProcessed;
123+
}
124+
125+
/**
126+
* Returns the total number of rows in the complete query result set, which can be more than the
127+
* number of rows in the first page of results returned by {@link #values()}. Returns {@code 0}
128+
* if the query was a dry run.
129+
*/
130+
public long totalRows() {
131+
return totalRows;
132+
}
133+
134+
@Override
135+
public String toString() {
136+
return MoreObjects.toStringHelper(this)
137+
.add("rows", values())
138+
.add("cacheHit", cacheHit)
139+
.add("schema", schema)
140+
.add("totalBytesProcessed", totalBytesProcessed)
141+
.add("totalRows", totalRows)
142+
.add("cursor", nextPageCursor())
143+
.toString();
144+
}
145+
146+
@Override
147+
public int hashCode() {
148+
return Objects.hash(super.hashCode(), cacheHit, schema, totalBytesProcessed, totalRows);
149+
}
150+
151+
@Override
152+
public boolean equals(Object obj) {
153+
if (this == obj) {
154+
return true;
155+
}
156+
if (obj == null || getClass() != obj.getClass()) {
157+
return false;
158+
}
159+
QueryResult response = (QueryResult) obj;
160+
return Objects.equals(nextPageCursor(), response.nextPageCursor())
161+
&& Objects.equals(values(), response.values())
162+
&& Objects.equals(schema, response.schema)
163+
&& Objects.equals(totalRows, response.totalRows)
164+
&& Objects.equals(totalBytesProcessed, response.totalBytesProcessed)
165+
&& Objects.equals(cacheHit, response.cacheHit);
166+
}
167+
168+
static Builder builder() {
169+
return new Builder();
170+
}
171+
}

0 commit comments

Comments
 (0)