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 when reading BigQueryResultSet from empty tables #3627

Merged
merged 2 commits into from
Jan 9, 2025
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 @@ -113,6 +113,9 @@ private class BigQueryResultSet extends AbstractJdbcResultSet {
@Override
/*Advances the result set to the next row, returning false if no such row exists. Potentially blocking operation*/
public boolean next() throws SQLException {
if (buffer == null) {
return false;
}
if (hasReachedEnd) { // if end of stream is reached then we can simply return false
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ ListenableFuture<ExecuteSelectResponse> executeSelectAsync(String sql)
* &#64;code
* ConnectionSettings connectionSettings =
* ConnectionSettings.newBuilder()
* ..setUseReadAPI(true)
* .setUseReadAPI(true)
* .build();
* Connection connection = bigquery.createConnection(connectionSettings);
* String selectQuery =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public void testLegacyQuerySinglePage() throws BigQuerySQLException {

// calls executeSelect with a nonFast query where the query returns an empty result.
@Test
public void testLegacyQuerySinglePageEmptyResults() throws BigQuerySQLException {
public void testLegacyQuerySinglePageEmptyResults() throws BigQuerySQLException, SQLException {
ConnectionImpl connectionSpy = Mockito.spy(connection);
com.google.api.services.bigquery.model.Job jobResponseMock =
new com.google.api.services.bigquery.model.Job()
Expand All @@ -428,6 +428,10 @@ public void testLegacyQuerySinglePageEmptyResults() throws BigQuerySQLException
BigQueryResult res = connectionSpy.executeSelect(SQL_QUERY);
assertEquals(res.getTotalRows(), 0);
assertEquals(QUERY_SCHEMA, res.getSchema());
assertEquals(
false,
res.getResultSet()
.next()); // Validates that NPE does not occur when reading from empty ResultSet.
verify(bigqueryRpcMock, times(1))
.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3491,6 +3491,24 @@ public void testExecuteSelectDefaultConnectionSettings() throws SQLException {
assertEquals(42, bigQueryResult.getTotalRows());
}

@Test
public void testExecuteSelectReadApiEmptyResultSet() throws SQLException {
ConnectionSettings connectionSettings =
ConnectionSettings.newBuilder()
.setJobTimeoutMs(
Long.MAX_VALUE) // Force executeSelect to use ReadAPI instead of fast query.
.setUseReadAPI(true)
.setUseQueryCache(false)
.build();
Connection connection = bigquery.createConnection(connectionSettings);
String query = "SELECT TIMESTAMP '2022-01-24T23:54:25.095574Z' LIMIT 0";
BigQueryResult bigQueryResult = connection.executeSelect(query);

ResultSet rs = bigQueryResult.getResultSet();
assertThat(rs.next()).isFalse();
assertThat(bigQueryResult.getTotalRows()).isEqualTo(0);
}

@Test
public void testExecuteSelectWithCredentials() throws SQLException {
// This test validate that executeSelect uses the same credential provided by the BigQuery
Expand Down
Loading