Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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