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: Move ratio calculation for whether to use read API to avoid NPE with setUseReadAPI(false) #2509

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -1203,22 +1203,18 @@ boolean isFastQuerySupported() {

@VisibleForTesting
boolean useReadAPI(Long totalRows, Long pageRows, Schema schema, Boolean hasQueryParameters) {
if ((totalRows == null || pageRows == null)
&& Boolean.TRUE.equals(
connectionSettings
.getUseReadAPI())) { // totalRows and pageRows are returned null when the job is not
// complete
return true;
}

// Read API does not yet support Interval Type or QueryParameters
if (containsIntervalType(schema) || hasQueryParameters) {
logger.log(Level.INFO, "\n Schema has IntervalType, or QueryParameters. Disabling ReadAPI");
return false;
}

long resultRatio = totalRows / pageRows;
if (totalRows == null || pageRows == null) {
return connectionSettings.getUseReadAPI();
}

if (Boolean.TRUE.equals(connectionSettings.getUseReadAPI())) {
long resultRatio = totalRows / pageRows;
return resultRatio >= connectionSettings.getTotalToPageRowCountRatio()
&& totalRows > connectionSettings.getMinResultSize();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public class ConnectionImplTest {
Field.newBuilder("state_name", StandardSQLTypeName.STRING)
.setMode(Field.Mode.NULLABLE)
.build());

private static final Schema QUERY_SCHEMA_WITH_INTERVAL_FIELD =
Schema.of(
Field.newBuilder("interval", StandardSQLTypeName.INTERVAL)
.setMode(Field.Mode.NULLABLE)
.build());
private static final TableSchema FAST_QUERY_TABLESCHEMA = QUERY_SCHEMA.toPb();
private static final BigQueryResult BQ_RS_MOCK_RES =
new BigQueryResultImpl(QUERY_SCHEMA, 2, null, null);
Expand Down Expand Up @@ -661,6 +667,32 @@ public void testGetSubsequentQueryResultsWithJob() {
.getSubsequentQueryResultsWithJob(10000L, 100L, jobId, GET_QUERY_RESULTS_RESPONSE, false);
}

@Test
public void testUseReadApi() {
ConnectionSettings connectionSettingsSpy = Mockito.spy(ConnectionSettings.class);
doReturn(true).when(connectionSettingsSpy).getUseReadAPI();
doReturn(2).when(connectionSettingsSpy).getTotalToPageRowCountRatio();
doReturn(100).when(connectionSettingsSpy).getMinResultSize();

connection = (ConnectionImpl) bigquery.createConnection(connectionSettingsSpy);

// defaults to connectionSettings.getUseReadAPI() when total/page rows are null (job is still
// running)
assertTrue(connection.useReadAPI(null, null, QUERY_SCHEMA, false));

assertFalse(connection.useReadAPI(10000L, 10000L, QUERY_SCHEMA, false));
assertFalse(connection.useReadAPI(50L, 10L, QUERY_SCHEMA, false));
assertTrue(connection.useReadAPI(10000L, 10L, QUERY_SCHEMA, false));

// interval and query parameters not supported
assertFalse(connection.useReadAPI(10000L, 10L, QUERY_SCHEMA_WITH_INTERVAL_FIELD, false));
assertFalse(connection.useReadAPI(10000L, 10L, QUERY_SCHEMA, true));

doReturn(false).when(connectionSettingsSpy).getUseReadAPI();
assertFalse(connection.useReadAPI(null, null, QUERY_SCHEMA, false));
assertFalse(connection.useReadAPI(10000L, 10L, QUERY_SCHEMA, false));
}

@Test
public void testGetPageCacheSize() {
ConnectionImpl connectionSpy = Mockito.spy(connection);
Expand Down