feat(bigquery): job creation mode GA (#3804) · googleapis/java-bigquery@a21cde8 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit a21cde8

Browse files
authored
feat(bigquery): job creation mode GA (#3804)
* feat(bigquery): job creation mode GA * deprecate query preview * Update samples * rename snippet to job optional * fix style * rename sample test
1 parent e289390 commit a21cde8

File tree

7 files changed

+50
-45
lines changed

7 files changed

+50
-45
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.google.cloud.Tuple;
4141
import com.google.cloud.bigquery.BigQueryRetryHelper.BigQueryRetryHelperException;
4242
import com.google.cloud.bigquery.InsertAllRequest.RowToInsert;
43-
import com.google.cloud.bigquery.QueryJobConfiguration.JobCreationMode;
4443
import com.google.cloud.bigquery.spi.v2.BigQueryRpc;
4544
import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc;
4645
import com.google.common.annotations.VisibleForTesting;
@@ -1407,12 +1406,10 @@ public TableResult query(QueryJobConfiguration configuration, JobOption... optio
14071406
throws InterruptedException, JobException {
14081407
Job.checkNotDryRun(configuration, "query");
14091408

1410-
if (getOptions().isQueryPreviewEnabled()) {
1411-
configuration =
1412-
configuration.toBuilder()
1413-
.setJobCreationMode(JobCreationMode.JOB_CREATION_OPTIONAL)
1414-
.build();
1415-
}
1409+
configuration =
1410+
configuration.toBuilder()
1411+
.setJobCreationMode(getOptions().getDefaultJobCreationMode())
1412+
.build();
14161413

14171414
// If all parameters passed in configuration are supported by the query() method on the backend,
14181415
// put on fast path

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import com.google.cloud.ServiceOptions;
2121
import com.google.cloud.ServiceRpc;
2222
import com.google.cloud.TransportOptions;
23+
import com.google.cloud.bigquery.QueryJobConfiguration.JobCreationMode;
2324
import com.google.cloud.bigquery.spi.BigQueryRpcFactory;
2425
import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc;
2526
import com.google.cloud.http.HttpTransportOptions;
26-
import com.google.common.annotations.VisibleForTesting;
2727
import com.google.common.collect.ImmutableSet;
2828
import java.util.Set;
2929

@@ -38,7 +38,7 @@ public class BigQueryOptions extends ServiceOptions<BigQuery, BigQueryOptions> {
3838
// set the option ThrowNotFound when you want to throw the exception when the value not found
3939
private boolean setThrowNotFound;
4040
private boolean useInt64Timestamps;
41-
private String queryPreviewEnabled = System.getenv("QUERY_PREVIEW_ENABLED");
41+
private JobCreationMode defaultJobCreationMode = JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED;
4242

4343
public static class DefaultBigQueryFactory implements BigQueryFactory {
4444

@@ -139,8 +139,9 @@ public String getLocation() {
139139
return location;
140140
}
141141

142+
@Deprecated
142143
public boolean isQueryPreviewEnabled() {
143-
return queryPreviewEnabled != null && queryPreviewEnabled.equalsIgnoreCase("TRUE");
144+
return false;
144145
}
145146

146147
public void setThrowNotFound(boolean setThrowNotFound) {
@@ -151,9 +152,11 @@ public void setUseInt64Timestamps(boolean useInt64Timestamps) {
151152
this.useInt64Timestamps = useInt64Timestamps;
152153
}
153154

154-
@VisibleForTesting
155-
public void setQueryPreviewEnabled(String queryPreviewEnabled) {
156-
this.queryPreviewEnabled = queryPreviewEnabled;
155+
@Deprecated
156+
public void setQueryPreviewEnabled(String queryPreviewEnabled) {}
157+
158+
public void setDefaultJobCreationMode(JobCreationMode jobCreationMode) {
159+
this.defaultJobCreationMode = jobCreationMode;
157160
}
158161

159162
public boolean getThrowNotFound() {
@@ -164,6 +167,10 @@ public boolean getUseInt64Timestamps() {
164167
return useInt64Timestamps;
165168
}
166169

170+
public JobCreationMode getDefaultJobCreationMode() {
171+
return defaultJobCreationMode;
172+
}
173+
167174
@SuppressWarnings("unchecked")
168175
@Override
169176
public Builder toBuilder() {

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryJobConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public enum Priority {
9797
}
9898

9999
/** Job Creation Mode provides different options on job creation. */
100-
enum JobCreationMode {
100+
public enum JobCreationMode {
101101
/** Unspecified JobCreationMode, defaults to JOB_CREATION_REQUIRED. */
102102
JOB_CREATION_MODE_UNSPECIFIED,
103103
/** Default. Job creation is always required. */
@@ -683,7 +683,7 @@ public Builder setMaxResults(Long maxResults) {
683683
* Provides different options on job creation. If not specified the job creation mode is assumed
684684
* to be {@link JobCreationMode#JOB_CREATION_REQUIRED}.
685685
*/
686-
Builder setJobCreationMode(JobCreationMode jobCreationMode) {
686+
public Builder setJobCreationMode(JobCreationMode jobCreationMode) {
687687
this.jobCreationMode = jobCreationMode;
688688
return this;
689689
}
@@ -959,7 +959,7 @@ public Long getMaxResults() {
959959
}
960960

961961
/** Returns the job creation mode. */
962-
JobCreationMode getJobCreationMode() {
962+
public JobCreationMode getJobCreationMode() {
963963
return jobCreationMode;
964964
}
965965

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryRequestInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ boolean isFastQuerySupported(JobId jobId) {
8686
&& config.getTableDefinitions() == null
8787
&& config.getTimePartitioning() == null
8888
&& config.getUserDefinedFunctions() == null
89-
&& config.getWriteDisposition() == null;
89+
&& config.getWriteDisposition() == null
90+
&& config.getJobCreationMode() != JobCreationMode.JOB_CREATION_REQUIRED;
9091
}
9192

9293
QueryRequest toPb() {

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
import com.google.cloud.bigquery.PolicyTags;
112112
import com.google.cloud.bigquery.PrimaryKey;
113113
import com.google.cloud.bigquery.QueryJobConfiguration;
114+
import com.google.cloud.bigquery.QueryJobConfiguration.JobCreationMode;
114115
import com.google.cloud.bigquery.QueryJobConfiguration.Priority;
115116
import com.google.cloud.bigquery.QueryParameterValue;
116117
import com.google.cloud.bigquery.Range;
@@ -3483,6 +3484,7 @@ public void testExecuteSelectWithReadApi() throws SQLException {
34833484
final int rowLimit = 5000;
34843485
final String QUERY =
34853486
"SELECT * FROM bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2017 LIMIT %s";
3487+
bigquery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED);
34863488
// Job timeout is somewhat arbitrary - just ensures that fast query is not used.
34873489
// min result size and page row count ratio ensure that the ReadAPI is used.
34883490
ConnectionSettings connectionSettingsReadAPIEnabledFastQueryDisabled =
@@ -7084,26 +7086,19 @@ public void testStatelessQueries() throws InterruptedException {
70847086
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
70857087
BigQuery bigQuery = bigqueryHelper.getOptions().getService();
70867088

7087-
// Simulate setting the QUERY_PREVIEW_ENABLED environment variable.
7088-
bigQuery.getOptions().setQueryPreviewEnabled("TRUE");
7089+
// Stateless query should have no job id.
7090+
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_OPTIONAL);
70897091
TableResult tableResult = executeSimpleQuery(bigQuery);
70907092
assertNotNull(tableResult.getQueryId());
70917093
assertNull(tableResult.getJobId());
70927094

7093-
// The flag should be case-insensitive.
7094-
bigQuery.getOptions().setQueryPreviewEnabled("tRuE");
7095+
// Job creation takes over, no query id is created.
7096+
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED);
70957097
tableResult = executeSimpleQuery(bigQuery);
7096-
assertNotNull(tableResult.getQueryId());
7097-
assertNull(tableResult.getJobId());
7098-
7099-
// Any other values won't enable optional job creation mode.
7100-
bigQuery.getOptions().setQueryPreviewEnabled("test_value");
7101-
tableResult = executeSimpleQuery(bigQuery);
7102-
assertNotNull(tableResult.getQueryId());
7098+
assertNull(tableResult.getQueryId());
71037099
assertNotNull(tableResult.getJobId());
71047100

7105-
// Reset the flag.
7106-
bigQuery.getOptions().setQueryPreviewEnabled(null);
7101+
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED);
71077102
tableResult = executeSimpleQuery(bigQuery);
71087103
assertNotNull(tableResult.getQueryId());
71097104
assertNotNull(tableResult.getJobId());
@@ -7128,8 +7123,8 @@ public void testTableResultJobIdAndQueryId() throws InterruptedException {
71287123
// Create local BigQuery for test scenario 1 to not contaminate global test parameters.
71297124
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
71307125
BigQuery bigQuery = bigqueryHelper.getOptions().getService();
7131-
// Simulate setting the QUERY_PREVIEW_ENABLED environment variable.
7132-
bigQuery.getOptions().setQueryPreviewEnabled("TRUE");
7126+
// Allow queries to be stateless.
7127+
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_OPTIONAL);
71337128
String query = "SELECT 1 as one";
71347129
QueryJobConfiguration configStateless = QueryJobConfiguration.newBuilder(query).build();
71357130
TableResult result = bigQuery.query(configStateless);
@@ -7181,15 +7176,17 @@ public void testStatelessQueriesWithLocation() throws Exception {
71817176
table.getTableId().getTable());
71827177

71837178
// Test stateless query when BigQueryOption location matches dataset location.
7184-
bigQuery.getOptions().setQueryPreviewEnabled("TRUE");
7179+
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_OPTIONAL);
71857180
TableResult tb = bigQuery.query(QueryJobConfiguration.of(query));
71867181
assertNull(tb.getJobId());
71877182

71887183
// Test stateless query when BigQueryOption location does not match dataset location.
71897184
try {
71907185
BigQuery bigQueryWrongLocation =
71917186
bigqueryHelper.getOptions().toBuilder().setLocation(wrongLocation).build().getService();
7192-
bigQueryWrongLocation.getOptions().setQueryPreviewEnabled("TRUE");
7187+
bigQueryWrongLocation
7188+
.getOptions()
7189+
.setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_OPTIONAL);
71937190
bigQueryWrongLocation.query(QueryJobConfiguration.of(query));
71947191
fail("querying a table with wrong location shouldn't work");
71957192
} catch (BigQueryException e) {

samples/snippets/src/main/java/com/example/bigquery/QueryShortMode.java renamed to samples/snippets/src/main/java/com/example/bigquery/QueryJobOptional.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,37 @@
1616

1717
package com.example.bigquery;
1818

19-
// [START bigquery_query_shortquery]
19+
// [START bigquery_query_job_optional]
2020
import com.google.cloud.bigquery.BigQuery;
2121
import com.google.cloud.bigquery.BigQueryException;
2222
import com.google.cloud.bigquery.BigQueryOptions;
2323
import com.google.cloud.bigquery.JobId;
2424
import com.google.cloud.bigquery.QueryJobConfiguration;
25+
import com.google.cloud.bigquery.QueryJobConfiguration.JobCreationMode;
2526
import com.google.cloud.bigquery.TableResult;
2627

2728
// Sample demonstrating short mode query execution.
2829
//
29-
// While this feature is still in preview, it is controlled by
30-
// setting the environment variable QUERY_PREVIEW_ENABLED=TRUE
31-
// to request short mode execution.
32-
public class QueryShortMode {
30+
// This feature is controlled by setting the defaultJobCreationMode
31+
// field in the BigQueryOptions used for the client. JOB_CREATION_OPTIONAL
32+
// allows for the execution of queries without creating a job.
33+
public class QueryJobOptional {
3334

3435
public static void main(String[] args) {
3536
String query =
3637
"SELECT name, gender, SUM(number) AS total FROM "
3738
+ "bigquery-public-data.usa_names.usa_1910_2013 GROUP BY "
3839
+ "name, gender ORDER BY total DESC LIMIT 10";
39-
queryShortMode(query);
40+
queryJobOptional(query);
4041
}
4142

42-
public static void queryShortMode(String query) {
43+
public static void queryJobOptional(String query) {
4344
try {
4445
// Initialize client that will be used to send requests. This client only needs
4546
// to be created once, and can be reused for multiple requests.
46-
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
47+
BigQueryOptions options = BigQueryOptions.getDefaultInstance();
48+
options.setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_OPTIONAL);
49+
BigQuery bigquery = options.getService();
4750

4851
// Execute the query. The returned TableResult provides access information
4952
// about the query execution as well as query results.
@@ -72,4 +75,4 @@ public static void queryShortMode(String query) {
7275
}
7376
}
7477
}
75-
// [END bigquery_query_shortquery]
78+
// [END bigquery_query_job_optional]

samples/snippets/src/test/java/com/example/bigquery/QueryShortModeIT.java renamed to samples/snippets/src/test/java/com/example/bigquery/QueryJobOptionalIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.junit.Before;
2727
import org.junit.Test;
2828

29-
public class QueryShortModeIT {
29+
public class QueryJobOptionalIT {
3030

3131
private final Logger log = Logger.getLogger(this.getClass().getName());
3232
private ByteArrayOutputStream bout;
@@ -56,7 +56,7 @@ public void testQueryBatch() {
5656
+ "bigquery-public-data.usa_names.usa_1910_2013 GROUP BY "
5757
+ "name, gender ORDER BY total DESC LIMIT 10";
5858

59-
QueryShortMode.queryShortMode(query);
59+
QueryJobOptional.queryJobOptional(query);
6060
assertThat(bout.toString()).contains("Query was run");
6161
}
6262
}

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.