diff --git a/README.md b/README.md index 949a73cc42..9d3f2494c9 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ If you are using Maven without the BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies: ```Groovy -implementation platform('com.google.cloud:libraries-bom:26.13.0') +implementation platform('com.google.cloud:libraries-bom:26.14.0') implementation 'com.google.cloud:google-cloud-bigquery' ``` diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java index eb0072905c..17a459312b 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java @@ -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 { diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java index 4b379629cf..d6348f053e 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java @@ -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); @@ -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);