Skip to content

Commit 20d4841

Browse files
committed
Add query request and query response classes and tests
1 parent aacc383 commit 20d4841

4 files changed

Lines changed: 712 additions & 0 deletions

File tree

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
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 static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.MoreObjects;
22+
23+
import java.io.Serializable;
24+
import java.util.Objects;
25+
26+
/**
27+
* Google Cloud BigQuery Query Request. This class can be used to run a BigQuery SQL query and
28+
* 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
30+
* through a BigQuery Job whose identity can be accessed via {@link QueryResponse#job()}.
31+
*
32+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/query">Query</a>
33+
*/
34+
public class QueryRequest implements Serializable {
35+
36+
private static final long serialVersionUID = -8727328332415880852L;
37+
38+
private final String query;
39+
private final Long maxResults;
40+
private final DatasetId defaultDataset;
41+
private final Long maxWaitTime;
42+
private final Boolean dryRun;
43+
private final Boolean useQueryCache;
44+
45+
public static final class Builder {
46+
47+
private String query;
48+
private Long maxResults;
49+
private DatasetId defaultDataset;
50+
private Long maxWaitTime;
51+
private Boolean dryRun;
52+
private Boolean useQueryCache;
53+
54+
private Builder() {}
55+
56+
/**
57+
* Sets the BigQuery query to be executed.
58+
*/
59+
public Builder query(String query) {
60+
this.query = query;
61+
return this;
62+
}
63+
64+
/**
65+
* Sets the maximum number of rows of data to return per page of results. Setting this flag to a
66+
* small value such as 1000 and then paging through results might improve reliability when the
67+
* query result set is large. In addition to this limit, responses are also limited to 10 MB.
68+
* By default, there is no maximum row count, and only the byte limit applies.
69+
*/
70+
public Builder maxResults(Long maxResults) {
71+
this.maxResults = maxResults;
72+
return this;
73+
}
74+
75+
/**
76+
* Sets the default dataset to assume for any unqualified table names in the query.
77+
*/
78+
public Builder defaultDataset(DatasetId defaultDataset) {
79+
this.defaultDataset = defaultDataset;
80+
return this;
81+
}
82+
83+
/**
84+
* Sets how long to wait for the query to complete, in milliseconds, before the request times
85+
* out and returns. Note that this is only a timeout for the request, not the query. If the
86+
* 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).
90+
*/
91+
public Builder maxWaitTime(Long maxWaitTime) {
92+
this.maxWaitTime = maxWaitTime;
93+
return this;
94+
}
95+
96+
/**
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.
100+
*/
101+
public Builder dryRun(Boolean dryRun) {
102+
this.dryRun = dryRun;
103+
return this;
104+
}
105+
106+
/**
107+
* 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.
109+
*
110+
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
111+
*/
112+
public Builder useQueryCache(Boolean useQueryCache) {
113+
this.useQueryCache = useQueryCache;
114+
return this;
115+
}
116+
117+
public QueryRequest build() {
118+
return new QueryRequest(this);
119+
}
120+
}
121+
122+
private QueryRequest(Builder builder) {
123+
query = checkNotNull(builder.query);
124+
maxResults = builder.maxResults;
125+
defaultDataset = builder.defaultDataset;
126+
maxWaitTime = builder.maxWaitTime;
127+
dryRun = builder.dryRun;
128+
useQueryCache = builder.useQueryCache;
129+
}
130+
131+
/**
132+
* Sets the BigQuery query to be executed.
133+
*/
134+
public String query() {
135+
return query;
136+
}
137+
138+
/**
139+
* Returns the maximum number of rows of data to return per page of results.
140+
*/
141+
public Long maxResults() {
142+
return maxResults;
143+
}
144+
145+
/**
146+
* Returns the default dataset to assume for any unqualified table names in the query.
147+
*/
148+
public DatasetId defaultDataset() {
149+
return defaultDataset;
150+
}
151+
152+
/**
153+
* Returns how long to wait for the query to complete, in milliseconds, before the request times
154+
* out and returns. Note that this is only a timeout for the request, not the query. If the
155+
* query takes longer to run than the timeout value, the call returns without any results and
156+
* with the {@link QueryResponse#jobComplete()} set to {@code false}. You can call
157+
* {@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+
*/
160+
public Long maxWaitTime() {
161+
return maxWaitTime;
162+
}
163+
164+
/**
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.
168+
*/
169+
public Boolean dryRun() {
170+
return dryRun;
171+
}
172+
173+
/**
174+
* 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.
176+
*
177+
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
178+
*/
179+
public Boolean useQueryCache() {
180+
return useQueryCache;
181+
}
182+
183+
/**
184+
* Returns a builder for the {@code QueryRequest} object.
185+
*/
186+
public Builder toBuilder() {
187+
return new Builder()
188+
.query(query)
189+
.maxResults(maxResults)
190+
.defaultDataset(defaultDataset)
191+
.maxWaitTime(maxWaitTime)
192+
.dryRun(dryRun)
193+
.useQueryCache(useQueryCache);
194+
}
195+
196+
@Override
197+
public String toString() {
198+
return MoreObjects.toStringHelper(this)
199+
.add("query", query)
200+
.add("maxResults", maxResults)
201+
.add("defaultDataset", defaultDataset)
202+
.add("maxWaitTime", maxWaitTime)
203+
.add("dryRun", dryRun)
204+
.add("useQueryCache", useQueryCache)
205+
.toString();
206+
}
207+
208+
@Override
209+
public int hashCode() {
210+
return Objects.hash(query, maxResults, defaultDataset, maxWaitTime, dryRun, useQueryCache);
211+
}
212+
213+
@Override
214+
public boolean equals(Object obj) {
215+
return obj instanceof QueryRequest && Objects.equals(toPb(), ((QueryRequest) obj).toPb());
216+
}
217+
218+
com.google.api.services.bigquery.model.QueryRequest toPb() {
219+
com.google.api.services.bigquery.model.QueryRequest queryRequestPb =
220+
new com.google.api.services.bigquery.model.QueryRequest().setQuery(query);
221+
if (maxResults != null) {
222+
queryRequestPb.setMaxResults(maxResults);
223+
}
224+
if (defaultDataset != null) {
225+
queryRequestPb.setDefaultDataset(defaultDataset.toPb());
226+
}
227+
if (maxWaitTime != null) {
228+
queryRequestPb.setTimeoutMs(maxWaitTime);
229+
}
230+
if (dryRun != null) {
231+
queryRequestPb.setDryRun(dryRun);
232+
}
233+
if (useQueryCache != null) {
234+
queryRequestPb.setUseQueryCache(useQueryCache);
235+
}
236+
return queryRequestPb;
237+
}
238+
239+
/**
240+
* Creates a builder for a {@code QueryRequest} given the BigQuery SQL query to be executed.
241+
*/
242+
public static Builder builder(String query) {
243+
return new Builder().query(query);
244+
}
245+
246+
/**
247+
* Creates a {@code QueryRequest} object given the BigQuery SQL query to be executed.
248+
*/
249+
public static QueryRequest of(String query) {
250+
return new Builder().query(query).build();
251+
}
252+
253+
static QueryRequest fromPb(com.google.api.services.bigquery.model.QueryRequest queryRequestPb) {
254+
Builder builder = builder(queryRequestPb.getQuery());
255+
if (queryRequestPb.getMaxResults() != null) {
256+
builder.maxResults(queryRequestPb.getMaxResults());
257+
}
258+
if (queryRequestPb.getDefaultDataset() != null) {
259+
builder.defaultDataset(DatasetId.fromPb(queryRequestPb.getDefaultDataset()));
260+
}
261+
if (queryRequestPb.getTimeoutMs() != null) {
262+
builder.maxWaitTime(queryRequestPb.getTimeoutMs());
263+
}
264+
if (queryRequestPb.getDryRun() != null) {
265+
builder.dryRun(queryRequestPb.getDryRun());
266+
}
267+
if (queryRequestPb.getUseQueryCache() != null) {
268+
builder.useQueryCache(queryRequestPb.getUseQueryCache());
269+
}
270+
return builder.build();
271+
}
272+
}

0 commit comments

Comments
 (0)