Skip to content
This repository was archived by the owner on Aug 30, 2026. It is now read-only.
This repository was archived by the owner on Aug 30, 2026. It is now read-only.

BigQuery: Timestamp rounding issue #1644

Closed

Description

@liu-du

Environment details

  1. BigQuery
  2. OS type and version: macOS
  3. Java version: 8
  4. bigquery version(s): at commit: 2b942da

Code example

I run the following query (note the last 3 digits are 574):

SELECT TIMESTAMP '2022-01-24T23:54:25.095574Z'

but I get back (note the last 3 digits are 573):

2022-01-24T23:54:25.095573Z

The following is a screen shot of the test and the result that I added to src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java:

BigQueryTimestamp

Seems the java client is affected by this issue, If I run the same query in BigQuery web console, the query returns correctly:
Screen Shot 2021-10-08 at 6 39 28 pm

Steps to reproduce

apply this patch and run the "tmpTest" in src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java.

Any additional information below

  • Only certain timestamp values may cause the rounding error
  • it seems that query priority has to be set to INTERATIVE to reproduce the rounding error
  • Although BigQuery Timestamp is up to microsecond according to documentation, but the value in FieldValue (see screenshot above) actually returns 7 digits instead of 6 (again this only happens for certain timestamps).

the following patch fixes the problem by rounding the big decimal to neareast whole number before taking the longValue:

diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java
index a2ce187..5bab4b3 100644
--- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java
+++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java
@@ -25,6 +25,7 @@ import com.google.common.base.MoreObjects;
 import com.google.common.io.BaseEncoding;
 import java.io.Serializable;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -183,7 +184,7 @@ public class FieldValue implements Serializable {
     // timestamps are encoded in the format 1408452095.22 where the integer part is seconds since
     // epoch (e.g. 1408452095.22 == 2014-08-19 07:41:35.220 -05:00)
     BigDecimal secondsWithMicro = new BigDecimal(getStringValue());
-    BigDecimal scaled = secondsWithMicro.scaleByPowerOfTen(6);
+    BigDecimal scaled = secondsWithMicro.scaleByPowerOfTen(6).setScale(0, RoundingMode.HALF_UP);
     return scaled.longValue();
   }

Activity

  1. added 3 commits that reference this issue on Oct 8, 2021
    a5f6d46
    2ecbcc3
    53ead88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api: bigqueryIssues related to the googleapis/java-bigquery API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions