Skip to content

Commit

Permalink
bigquery: loudly report invalid operations on dryruns (googleapis#2772)
Browse files Browse the repository at this point in the history
  • Loading branch information
pongad committed Jan 16, 2018
1 parent 3448564 commit d629d3e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ public TableResult query(QueryJobConfiguration configuration, JobOption... optio
@Override
public TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOption... options)
throws InterruptedException, JobException {
Job.checkNotDryRun(configuration, "query");
return create(JobInfo.of(jobId, configuration), options).getQueryResults();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public Job build() {
* @throws BigQueryException upon failure
*/
public boolean exists() {
checkNotDryRun("exists");
return bigquery.getJob(getJobId(), JobOption.fields()) != null;
}

Expand All @@ -185,6 +186,7 @@ public boolean exists() {
* @throws BigQueryException upon failure
*/
public boolean isDone() {
checkNotDryRun("isDone");
Job job = bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS));
return job == null || job.getStatus().getState() == JobStatus.State.DONE;
}
Expand Down Expand Up @@ -231,6 +233,7 @@ public boolean isDone() {
* to complete
*/
public Job waitFor(RetryOption... waitOptions) throws InterruptedException {
checkNotDryRun("waitFor");
Object completedJobResponse;
if (getConfiguration().getType() == Type.QUERY) {
completedJobResponse =
Expand Down Expand Up @@ -268,6 +271,7 @@ public Job waitFor(RetryOption... waitOptions) throws InterruptedException {
*/
public TableResult getQueryResults(QueryResultsOption... options)
throws InterruptedException, JobException {
checkNotDryRun("getQueryResults");
if (getConfiguration().getType() != Type.QUERY) {
throw new UnsupportedOperationException(
"Getting query results is supported only for " + Type.QUERY + " jobs");
Expand Down Expand Up @@ -389,6 +393,7 @@ public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
* @throws BigQueryException upon failure
*/
public Job reload(JobOption... options) {
checkNotDryRun("reload");
return bigquery.getJob(getJobId(), options);
}

Expand All @@ -410,9 +415,36 @@ public Job reload(JobOption... options) {
* @throws BigQueryException upon failure
*/
public boolean cancel() {
checkNotDryRun("cancel");
return bigquery.cancel(getJobId());
}

private void checkNotDryRun(String op) {
checkNotDryRun(getConfiguration(), op);
}

static void checkNotDryRun(JobConfiguration jobConfig, String op) {
QueryJobConfiguration config;
if (jobConfig instanceof QueryJobConfiguration) {
config = (QueryJobConfiguration) jobConfig;
} else {
return;
}

Boolean dryRun = config.dryRun();
if (dryRun == null) {
dryRun = false;
}
if (dryRun) {
String msg =
"Operation \"%s\" does not work for dryrun queries, "
+ "since a dry run does not actually create a job. "
+ "To validate a query and obtain some processing statistics, consider calling "
+ "BigQuery.create(JobInfo).";
throw new UnsupportedOperationException(String.format(msg, op));
}
}

/** Returns the job's {@code BigQuery} object used to issue requests. */
public BigQuery getBigQuery() {
return bigquery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1400,4 +1400,17 @@ public void testRuntimeException() {
thrown.expectMessage(exceptionMessage);
bigquery.getDataset(DATASET);
}

@Test
public void testQueryDryRun() throws Exception {
// https://github.com/GoogleCloudPlatform/google-cloud-java/issues/2479
EasyMock.replay(bigqueryRpcMock);
thrown.expect(UnsupportedOperationException.class);
options
.toBuilder()
.setRetrySettings(ServiceOptions.getDefaultRetrySettings())
.build()
.getService()
.query(QueryJobConfiguration.newBuilder("foo").setDryRun(true).build());
}
}

0 comments on commit d629d3e

Please sign in to comment.