Skip to content

BigQueryResultImpl.getDouble throws ClassCastException if read API is used #2905

@obada-ab

Description

@obada-ab

Line causing the issue:

return curVal == null ? 0.0d : ((BigDecimal) curVal).doubleValue();

Steps to reproduce

Call getDouble in a read API path result set.

Can be reproduced by adding the following test to ITBigQueryTest (copied from testReadAPIIterationAndOrderAsync with minor changes):

  @Test
  public void testReadAPIGetDouble()
      throws SQLException, ExecutionException,
      InterruptedException {
    String query =
        "SELECT date, county, state_name, confirmed_cases, deaths FROM "
            + TABLE_ID_LARGE.getTable()
            + " where date is not null and county is not null and state_name is not null order by confirmed_cases asc limit 300000";

    ConnectionSettings connectionSettings =
        ConnectionSettings.newBuilder()
            .setDefaultDataset(DatasetId.of(DATASET))
            .setPriority(
                QueryJobConfiguration.Priority
                    .INTERACTIVE)
            .build();
    Connection connection = bigquery.createConnection(connectionSettings);

    ListenableFuture<ExecuteSelectResponse> executeSelectFut = connection.executeSelectAsync(query);
    ExecuteSelectResponse exSelRes = executeSelectFut.get();
    BigQueryResult bigQueryResult = exSelRes.getResultSet();
    ResultSet rs = bigQueryResult.getResultSet();
    int cnt = 0;
    int lasConfirmedCases = Integer.MIN_VALUE;
    while (rs.next()) {
      assertTrue(rs.getDouble(3) >= 0); // throws ClassCastException
      ++cnt;
    }
    assertEquals(300000, cnt);
    connection.close();
  }

Stack trace

java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.math.BigDecimal (java.lang.Long and java.math.BigDecimal are in module java.base of loader 'bootstrap')

	at com.google.cloud.bigquery.BigQueryResultImpl$BigQueryResultSet.getDouble(BigQueryResultImpl.java:340)
	at com.google.cloud.bigquery.BigQueryResultImpl$BigQueryResultSet.getDouble(BigQueryResultImpl.java:355)
	at com.google.cloud.bigquery.it.ITBigQueryTest.testReadAPIGetDouble(ITBigQueryTest.java:3296)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.lang.Thread.run(Thread.java:833)

Workaround

Adding setUseReadAPI(false) to force-avoid read API:

ConnectionSettings connectionSettings =
        ConnectionSettings.newBuilder()
            .setDefaultDataset(DatasetId.of(DATASET))
            .setPriority(
                QueryJobConfiguration.Priority
                    .INTERACTIVE)
            .setUseReadAPI(false)
            .build();

References

Internal b/300130086

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the googleapis/java-bigquery API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions