Skip to content

Commit 2e239f7

Browse files
mikekapsduskis
authored andcommitted
Empty table results can have a schema (#4185)
* Update EmptyTableResult.java * Update Job.java * add test case
1 parent 68963f7 commit 2e239f7

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/EmptyTableResult.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
import com.google.api.core.InternalApi;
2020
import com.google.cloud.PageImpl;
21+
import javax.annotation.Nullable;
2122

2223
public class EmptyTableResult extends TableResult {
2324

2425
private static final long serialVersionUID = -4831062717210349819L;
2526

2627
/** An empty {@code TableResult} to avoid making API requests to unlistable tables. */
2728
@InternalApi("Exposed for testing")
28-
public EmptyTableResult() {
29-
super(null, 0, new PageImpl<FieldValueList>(null, "", null));
29+
public EmptyTableResult(@Nullable Schema schema) {
30+
super(schema, 0, new PageImpl<FieldValueList>(null, "", null));
3031
}
3132
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public TableResult getQueryResults(QueryResultsOption... options)
305305
// Listing table data might fail, such as with CREATE VIEW queries.
306306
// Avoid a tabledata.list API request by returning an empty TableResult.
307307
if (response.getTotalRows() == 0) {
308-
return new EmptyTableResult();
308+
return new EmptyTableResult(response.getSchema());
309309
}
310310

311311
TableId table = ((QueryJobConfiguration) getConfiguration()).getDestinationTable();

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,55 @@ public Iterable<FieldValueList> getValues() {
302302
verify(status, mockOptions);
303303
}
304304

305+
@Test
306+
public void testWaitForAndGetQueryResultsEmptyWithSchema() throws InterruptedException {
307+
QueryJobConfiguration jobConfig =
308+
QueryJobConfiguration.newBuilder("CREATE VIEW").setDestinationTable(TABLE_ID1).build();
309+
QueryStatistics jobStatistics =
310+
QueryStatistics.newBuilder()
311+
.setCreationTimestamp(1L)
312+
.setEndTime(3L)
313+
.setStartTime(2L)
314+
.build();
315+
JobInfo jobInfo =
316+
JobInfo.newBuilder(jobConfig)
317+
.setJobId(JOB_ID)
318+
.setStatistics(jobStatistics)
319+
.setJobId(JOB_ID)
320+
.setEtag(ETAG)
321+
.setGeneratedId(GENERATED_ID)
322+
.setSelfLink(SELF_LINK)
323+
.setUserEmail(EMAIL)
324+
.setStatus(JOB_STATUS)
325+
.build();
326+
327+
initializeExpectedJob(2, jobInfo);
328+
JobStatus status = createStrictMock(JobStatus.class);
329+
expect(bigquery.getOptions()).andReturn(mockOptions);
330+
expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()).times(2);
331+
Job completedJob = expectedJob.toBuilder().setStatus(status).build();
332+
QueryResponse completedQuery =
333+
QueryResponse.newBuilder()
334+
.setCompleted(true)
335+
.setTotalRows(0)
336+
.setSchema(Schema.of(Field.of("field1", LegacySQLTypeName.BOOLEAN)))
337+
.setErrors(ImmutableList.<BigQueryError>of())
338+
.build();
339+
340+
expect(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS))
341+
.andReturn(completedQuery);
342+
expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(completedJob);
343+
expect(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS))
344+
.andReturn(completedQuery);
345+
346+
replay(status, bigquery, mockOptions);
347+
initializeJob(jobInfo);
348+
assertThat(job.waitFor(TEST_RETRY_OPTIONS)).isSameAs(completedJob);
349+
assertThat(job.getQueryResults().getSchema())
350+
.isEqualTo(Schema.of(Field.of("field1", LegacySQLTypeName.BOOLEAN)));
351+
verify(status, mockOptions);
352+
}
353+
305354
@Test
306355
public void testWaitForAndGetQueryResults() throws InterruptedException {
307356
QueryJobConfiguration jobConfig =

0 commit comments

Comments
 (0)