Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NPE for executeSelect nonFast path with empty result #3445

Merged
merged 2 commits into from
Aug 22, 2024
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 @@ -53,6 +53,7 @@
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.IOException;
import java.math.BigInteger;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -418,12 +419,15 @@ public ListenableFuture<ExecuteSelectResponse> executeSelectAsync(
@VisibleForTesting
BigQueryResult getResultSet(
GetQueryResultsResponse firstPage, JobId jobId, String sql, Boolean hasQueryParameters) {
return getSubsequentQueryResultsWithJob(
firstPage.getTotalRows().longValue(),
(long) firstPage.getRows().size(),
jobId,
firstPage,
hasQueryParameters);
if (firstPage.getTotalRows().compareTo(BigInteger.ZERO) > 0) {
return getSubsequentQueryResultsWithJob(
firstPage.getTotalRows().longValue(),
(long) firstPage.getRows().size(),
jobId,
firstPage,
hasQueryParameters);
}
return new BigQueryResultImpl(Schema.fromPb(firstPage.getSchema()), 0, null, null);
}

static class EndOfFieldValueList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ public class ConnectionImplTest {
.setTotalBytesProcessed(42L)
.setTotalRows(BigInteger.valueOf(1L))
.setSchema(FAST_QUERY_TABLESCHEMA);
private static final GetQueryResultsResponse GET_QUERY_RESULTS_RESPONSE_EMPTY =
new GetQueryResultsResponse()
.setJobReference(QUERY_JOB.toPb())
.setJobComplete(true)
.setCacheHit(false)
.setPageToken(PAGE_TOKEN)
.setTotalBytesProcessed(0L)
.setTotalRows(BigInteger.valueOf(0L))
.setSchema(FAST_QUERY_TABLESCHEMA);

private static final GetQueryResultsResponse GET_QUERY_RESULTS_RESPONSE_NULL_SCHEMA =
new GetQueryResultsResponse()
Expand Down Expand Up @@ -375,7 +384,6 @@ public void testLegacyQuerySinglePage() throws BigQuerySQLException {
ConnectionImpl connectionSpy = Mockito.spy(connection);
com.google.api.services.bigquery.model.Job jobResponseMock =
new com.google.api.services.bigquery.model.Job()
// .setConfiguration(QUERY_JOB.g)
.setJobReference(QUERY_JOB.toPb())
.setId(JOB)
.setStatus(new com.google.api.services.bigquery.model.JobStatus().setState("DONE"));
Expand All @@ -401,6 +409,29 @@ public void testLegacyQuerySinglePage() throws BigQuerySQLException {
.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class));
}

// calls executeSelect with a nonFast query where the query returns an empty result.
@Test
public void testLegacyQuerySinglePageEmptyResults() throws BigQuerySQLException {
ConnectionImpl connectionSpy = Mockito.spy(connection);
com.google.api.services.bigquery.model.Job jobResponseMock =
new com.google.api.services.bigquery.model.Job()
.setJobReference(QUERY_JOB.toPb())
.setId(JOB)
.setStatus(new com.google.api.services.bigquery.model.JobStatus().setState("DONE"));
// emulating a legacy query
doReturn(false).when(connectionSpy).isFastQuerySupported();
doReturn(GET_QUERY_RESULTS_RESPONSE_EMPTY)
.when(connectionSpy)
.getQueryResultsFirstPage(any(JobId.class));
when(bigqueryRpcMock.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class)))
.thenReturn(jobResponseMock); // RPC call in createQueryJob
BigQueryResult res = connectionSpy.executeSelect(SQL_QUERY);
assertEquals(res.getTotalRows(), 0);
assertEquals(QUERY_SCHEMA, res.getSchema());
verify(bigqueryRpcMock, times(1))
.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class));
}

// exercises getSubsequentQueryResultsWithJob for fast running queries
@Test
public void testFastQueryLongRunning() throws SQLException {
Expand Down
Loading