Skip to content

Commit

Permalink
Empty table results can have a schema (#4185)
Browse files Browse the repository at this point in the history
* Update EmptyTableResult.java

* Update Job.java

* add test case
  • Loading branch information
mikekap authored and sduskis committed Feb 7, 2019
1 parent 68963f7 commit 2e239f7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

import com.google.api.core.InternalApi;
import com.google.cloud.PageImpl;
import javax.annotation.Nullable;

public class EmptyTableResult extends TableResult {

private static final long serialVersionUID = -4831062717210349819L;

/** An empty {@code TableResult} to avoid making API requests to unlistable tables. */
@InternalApi("Exposed for testing")
public EmptyTableResult() {
super(null, 0, new PageImpl<FieldValueList>(null, "", null));
public EmptyTableResult(@Nullable Schema schema) {
super(schema, 0, new PageImpl<FieldValueList>(null, "", null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public TableResult getQueryResults(QueryResultsOption... options)
// Listing table data might fail, such as with CREATE VIEW queries.
// Avoid a tabledata.list API request by returning an empty TableResult.
if (response.getTotalRows() == 0) {
return new EmptyTableResult();
return new EmptyTableResult(response.getSchema());
}

TableId table = ((QueryJobConfiguration) getConfiguration()).getDestinationTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,55 @@ public Iterable<FieldValueList> getValues() {
verify(status, mockOptions);
}

@Test
public void testWaitForAndGetQueryResultsEmptyWithSchema() throws InterruptedException {
QueryJobConfiguration jobConfig =
QueryJobConfiguration.newBuilder("CREATE VIEW").setDestinationTable(TABLE_ID1).build();
QueryStatistics jobStatistics =
QueryStatistics.newBuilder()
.setCreationTimestamp(1L)
.setEndTime(3L)
.setStartTime(2L)
.build();
JobInfo jobInfo =
JobInfo.newBuilder(jobConfig)
.setJobId(JOB_ID)
.setStatistics(jobStatistics)
.setJobId(JOB_ID)
.setEtag(ETAG)
.setGeneratedId(GENERATED_ID)
.setSelfLink(SELF_LINK)
.setUserEmail(EMAIL)
.setStatus(JOB_STATUS)
.build();

initializeExpectedJob(2, jobInfo);
JobStatus status = createStrictMock(JobStatus.class);
expect(bigquery.getOptions()).andReturn(mockOptions);
expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()).times(2);
Job completedJob = expectedJob.toBuilder().setStatus(status).build();
QueryResponse completedQuery =
QueryResponse.newBuilder()
.setCompleted(true)
.setTotalRows(0)
.setSchema(Schema.of(Field.of("field1", LegacySQLTypeName.BOOLEAN)))
.setErrors(ImmutableList.<BigQueryError>of())
.build();

expect(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS))
.andReturn(completedQuery);
expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(completedJob);
expect(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS))
.andReturn(completedQuery);

replay(status, bigquery, mockOptions);
initializeJob(jobInfo);
assertThat(job.waitFor(TEST_RETRY_OPTIONS)).isSameAs(completedJob);
assertThat(job.getQueryResults().getSchema())
.isEqualTo(Schema.of(Field.of("field1", LegacySQLTypeName.BOOLEAN)));
verify(status, mockOptions);
}

@Test
public void testWaitForAndGetQueryResults() throws InterruptedException {
QueryJobConfiguration jobConfig =
Expand Down

0 comments on commit 2e239f7

Please sign in to comment.