Skip to content

Commit

Permalink
fix: throw jobexception for invalid multiple statements query
Browse files Browse the repository at this point in the history
add another test case

add exception checking Job class

update reload() method
  • Loading branch information
stephaniewang526 committed Sep 15, 2020
1 parent b2ca177 commit 946de7b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Expand Up @@ -401,7 +401,16 @@ public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
*/
public Job reload(JobOption... options) {
checkNotDryRun("reload");
return bigquery.getJob(getJobId(), options);
Job job = bigquery.getJob(getJobId(), options);
if (job != null && job.getStatus().getError() != null) {
// TODO(pmakani): change to BigQueryException when fast query path change is merged
throw new JobException(
getJobId(),
job.getStatus().getExecutionErrors() == null
? ImmutableList.of(job.getStatus().getError())
: ImmutableList.copyOf(job.getStatus().getExecutionErrors()));
}
return job;
}

/**
Expand Down
Expand Up @@ -65,6 +65,7 @@
import com.google.cloud.bigquery.InsertAllRequest;
import com.google.cloud.bigquery.InsertAllResponse;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobException;
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.JobStatistics;
Expand Down Expand Up @@ -1380,6 +1381,39 @@ public void testRoutineAPICreation() {
assertEquals(routine.getRoutineType(), "SCALAR_FUNCTION");
}

@Test
public void testSingleStatementsQueryException() throws InterruptedException {
String invalidQuery =
String.format("INSERT %s.%s VALUES('3', 10);", DATASET, TABLE_ID.getTable());
try {
bigquery.create(JobInfo.of(QueryJobConfiguration.of(invalidQuery))).waitFor();
fail("BigQueryException was expected");
} catch (BigQueryException e) {
BigQueryError error = e.getError();
assertNotNull(error);
assertEquals("invalidQuery", error.getReason());
assertNotNull(error.getMessage());
}
}

@Test
public void testMultipleStatementsQueryException() throws InterruptedException {
String invalidQuery =
String.format(
"INSERT %s.%s VALUES('3', 10); DELETE %s.%s where c2=3;",
DATASET, TABLE_ID.getTable(), DATASET, TABLE_ID.getTable());
try {
bigquery.create(JobInfo.of(QueryJobConfiguration.of(invalidQuery))).waitFor();
fail("JobException was expected");
} catch (JobException e) {
for (BigQueryError error : e.getErrors()) {
assertNotNull(error);
assertEquals("invalidQuery", error.getReason());
assertNotNull(error.getMessage());
}
}
}

@Test
public void testQuery() throws InterruptedException {
String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable();
Expand Down

0 comments on commit 946de7b

Please sign in to comment.