Skip to content

Commit 2a0d86d

Browse files
fix: throw jobexception for invalid multiple statements query (#732)
* fix: throw jobexception for invalid multiple statements query add another test case add exception checking Job class update reload() method * add unit test
1 parent 38c8716 commit 2a0d86d

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,16 @@ public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
401401
*/
402402
public Job reload(JobOption... options) {
403403
checkNotDryRun("reload");
404-
return bigquery.getJob(getJobId(), options);
404+
Job job = bigquery.getJob(getJobId(), options);
405+
if (job != null && job.getStatus().getError() != null) {
406+
// TODO(pmakani): change to BigQueryException when fast query path change is merged
407+
throw new JobException(
408+
getJobId(),
409+
job.getStatus().getExecutionErrors() == null
410+
? ImmutableList.of(job.getStatus().getError())
411+
: ImmutableList.copyOf(job.getStatus().getExecutionErrors()));
412+
}
413+
return job;
405414
}
406415

407416
/**

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertNotEquals;
24+
import static org.junit.Assert.assertNotNull;
2425
import static org.junit.Assert.assertNull;
2526
import static org.junit.Assert.assertSame;
2627
import static org.junit.Assert.assertTrue;
28+
import static org.junit.Assert.fail;
2729
import static org.mockito.Mockito.any;
2830
import static org.mockito.Mockito.eq;
2931
import static org.mockito.Mockito.mock;
@@ -36,6 +38,7 @@
3638
import com.google.cloud.RetryOption;
3739
import com.google.cloud.bigquery.JobStatistics.CopyStatistics;
3840
import com.google.cloud.bigquery.JobStatistics.QueryStatistics;
41+
import com.google.cloud.bigquery.JobStatus.State;
3942
import com.google.common.collect.ImmutableList;
4043
import java.util.Collections;
4144
import org.junit.Assert;
@@ -436,6 +439,24 @@ public void testReload() {
436439
verify(bigquery).getJob(JOB_INFO.getJobId());
437440
}
438441

442+
@Test
443+
public void testReloadJobException() {
444+
JobInfo updatedInfo = JOB_INFO.toBuilder().setEtag("etag").build();
445+
Job expectedJob = new Job(bigquery, new JobInfo.BuilderImpl(updatedInfo));
446+
BigQueryError bigQueryError = new BigQueryError("invalidQuery", "US", "invalidQuery");
447+
expectedJob =
448+
expectedJob.toBuilder().setStatus(new JobStatus(State.DONE, bigQueryError, null)).build();
449+
ImmutableList<BigQueryError> bigQueryErrorList = ImmutableList.of(bigQueryError);
450+
JobException jobException = new JobException(expectedJob.getJobId(), bigQueryErrorList);
451+
when(bigquery.getJob(JOB_INFO.getJobId())).thenReturn(expectedJob).thenThrow(jobException);
452+
try {
453+
job.reload();
454+
fail("JobException expected");
455+
} catch (JobException e) {
456+
assertNotNull(e.getErrors());
457+
}
458+
}
459+
439460
@Test
440461
public void testReloadNull() {
441462
when(bigquery.getJob(JOB_INFO.getJobId())).thenReturn(null);

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

+34
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.google.cloud.bigquery.InsertAllRequest;
6666
import com.google.cloud.bigquery.InsertAllResponse;
6767
import com.google.cloud.bigquery.Job;
68+
import com.google.cloud.bigquery.JobException;
6869
import com.google.cloud.bigquery.JobId;
6970
import com.google.cloud.bigquery.JobInfo;
7071
import com.google.cloud.bigquery.JobStatistics;
@@ -1379,6 +1380,39 @@ public void testRoutineAPICreation() {
13791380
assertEquals(routine.getRoutineType(), "SCALAR_FUNCTION");
13801381
}
13811382

1383+
@Test
1384+
public void testSingleStatementsQueryException() throws InterruptedException {
1385+
String invalidQuery =
1386+
String.format("INSERT %s.%s VALUES('3', 10);", DATASET, TABLE_ID.getTable());
1387+
try {
1388+
bigquery.create(JobInfo.of(QueryJobConfiguration.of(invalidQuery))).waitFor();
1389+
fail("BigQueryException was expected");
1390+
} catch (BigQueryException e) {
1391+
BigQueryError error = e.getError();
1392+
assertNotNull(error);
1393+
assertEquals("invalidQuery", error.getReason());
1394+
assertNotNull(error.getMessage());
1395+
}
1396+
}
1397+
1398+
@Test
1399+
public void testMultipleStatementsQueryException() throws InterruptedException {
1400+
String invalidQuery =
1401+
String.format(
1402+
"INSERT %s.%s VALUES('3', 10); DELETE %s.%s where c2=3;",
1403+
DATASET, TABLE_ID.getTable(), DATASET, TABLE_ID.getTable());
1404+
try {
1405+
bigquery.create(JobInfo.of(QueryJobConfiguration.of(invalidQuery))).waitFor();
1406+
fail("JobException was expected");
1407+
} catch (JobException e) {
1408+
for (BigQueryError error : e.getErrors()) {
1409+
assertNotNull(error);
1410+
assertEquals("invalidQuery", error.getReason());
1411+
assertNotNull(error.getMessage());
1412+
}
1413+
}
1414+
}
1415+
13821416
@Test
13831417
public void testQuery() throws InterruptedException {
13841418
String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable();

0 commit comments

Comments
 (0)