You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Seems the java client is affected by this issue, If I run the same query in BigQuery web console, the query returns correctly:
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();
}
The text was updated successfully, but these errors were encountered:
Environment details
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
:Seems the java client is affected by this issue, If I run the same query in BigQuery web console, the query returns correctly:
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
the following patch fixes the problem by rounding the big decimal to neareast whole number before taking the longValue:
The text was updated successfully, but these errors were encountered: