Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8f85a4d

Browse files
authoredSep 28, 2023
fix: dry run NPE when there is no query parameters (#2899)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [X] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [X] Appropriate docs were updated (if necessary) Fixes #2895 ☕️
1 parent 6824605 commit 8f85a4d

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
 

‎google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.io.IOException;
5656
import java.util.AbstractList;
5757
import java.util.ArrayList;
58+
import java.util.Collections;
5859
import java.util.HashMap;
5960
import java.util.LinkedList;
6061
import java.util.List;
@@ -173,7 +174,9 @@ public BigQueryDryRunResult dryRun(String sql) throws BigQuerySQLException {
173174
List<QueryParameter> queryParametersPb =
174175
dryRunJob.getStatistics().getQuery().getUndeclaredQueryParameters();
175176
List<Parameter> queryParameters =
176-
Lists.transform(queryParametersPb, QUERY_PARAMETER_FROM_PB_FUNCTION);
177+
queryParametersPb == null
178+
? Collections.emptyList()
179+
: Lists.transform(queryParametersPb, QUERY_PARAMETER_FROM_PB_FUNCTION);
177180
QueryStatistics queryStatistics = JobStatistics.fromPb(dryRunJob);
178181
SessionInfo sessionInfo =
179182
queryStatistics.getSessionInfo() == null ? null : queryStatistics.getSessionInfo();

‎google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,34 @@ public void testQueryDryRun() throws BigQuerySQLException {
237237
.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class));
238238
}
239239

240+
@Test
241+
public void testQueryDryRunNoQueryParameters() throws BigQuerySQLException {
242+
com.google.api.services.bigquery.model.JobStatistics2 queryMock =
243+
new com.google.api.services.bigquery.model.JobStatistics2()
244+
.setSchema(FAST_QUERY_TABLESCHEMA);
245+
com.google.api.services.bigquery.model.JobStatistics jobStatsMock =
246+
new com.google.api.services.bigquery.model.JobStatistics()
247+
.setCreationTime(1234L)
248+
.setStartTime(5678L)
249+
.setQuery(queryMock);
250+
com.google.api.services.bigquery.model.JobConfigurationQuery jobConfigurationQuery =
251+
new com.google.api.services.bigquery.model.JobConfigurationQuery();
252+
com.google.api.services.bigquery.model.JobConfiguration jobConfig =
253+
new com.google.api.services.bigquery.model.JobConfiguration()
254+
.setQuery(jobConfigurationQuery);
255+
com.google.api.services.bigquery.model.Job mockDryRunJob =
256+
new com.google.api.services.bigquery.model.Job()
257+
.setStatistics(jobStatsMock)
258+
.setConfiguration(jobConfig);
259+
when(bigqueryRpcMock.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class)))
260+
.thenReturn(mockDryRunJob);
261+
BigQueryDryRunResult dryRunResult = connection.dryRun(DRY_RUN_SQL);
262+
assertEquals(0, dryRunResult.getQueryParameters().size());
263+
assertEquals(QUERY_SCHEMA, dryRunResult.getSchema());
264+
verify(bigqueryRpcMock, times(1))
265+
.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class));
266+
}
267+
240268
@Test
241269
public void testParseDataTask() throws InterruptedException {
242270
BlockingQueue<Tuple<Iterable<FieldValueList>, Boolean>> pageCache =

‎google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,34 @@ public void testConnectionImplDryRun() throws SQLException {
29892989
assertEquals(StatementType.SELECT, queryStatistics.getStatementType());
29902990
}
29912991

2992+
@Test
2993+
public void testConnectionImplDryRunNoQueryParameters() throws SQLException {
2994+
String query =
2995+
String.format(
2996+
"select StringField, BigNumericField, BooleanField, BytesField, IntegerField, "
2997+
+ "TimestampField, FloatField, NumericField, TimeField, DateField, DateTimeField, "
2998+
+ "GeographyField, RecordField.BytesField, RecordField.BooleanField, "
2999+
+ "IntegerArrayField from %s order by TimestampField",
3000+
TABLE_ID_FASTQUERY_BQ_RESULTSET.getTable());
3001+
ConnectionSettings connectionSettings =
3002+
ConnectionSettings.newBuilder()
3003+
.setDefaultDataset(DatasetId.of(DATASET))
3004+
.setCreateSession(true)
3005+
.build();
3006+
Connection connection = bigquery.createConnection(connectionSettings);
3007+
BigQueryDryRunResult bigQueryDryRunResultSet = connection.dryRun(query);
3008+
assertNotNull(bigQueryDryRunResultSet.getSchema());
3009+
assertEquals(
3010+
BQ_RESULTSET_EXPECTED_SCHEMA, bigQueryDryRunResultSet.getSchema()); // match the schema
3011+
List<Parameter> queryParameters = bigQueryDryRunResultSet.getQueryParameters();
3012+
assertEquals(0, queryParameters.size());
3013+
QueryStatistics queryStatistics = bigQueryDryRunResultSet.getStatistics().getQueryStatistics();
3014+
assertNotNull(queryStatistics);
3015+
SessionInfo sessionInfo = bigQueryDryRunResultSet.getStatistics().getSessionInfo();
3016+
assertNotNull(sessionInfo.getSessionId());
3017+
assertEquals(StatementType.SELECT, queryStatistics.getStatementType());
3018+
}
3019+
29923020
@Test
29933021
// This test case test the order of the records, making sure that the result is not jumbled up due
29943022
// to the multithreaded BigQueryResult implementation

0 commit comments

Comments
 (0)
Failed to load comments.