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: fix timestamp rounding issue #1645

Merged
merged 2 commits into from Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -183,7 +184,10 @@ public long getTimestampValue() {
// 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);
// Rounding the BigDecimal to the nearest whole number before setting the longValue in order to
// address TimeStamp rounding issue described in
// https://github.com/googleapis/java-bigquery/issues/1644
BigDecimal scaled = secondsWithMicro.scaleByPowerOfTen(6).setScale(0, RoundingMode.HALF_UP);
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
return scaled.longValue();
}

Expand Down
Expand Up @@ -127,6 +127,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -1854,6 +1855,40 @@ public void testQuery() throws InterruptedException {
assertNotNull(statistics.getQueryPlan());
}

@Test
public void testQueryTimeStamp() throws InterruptedException {
String query = "SELECT TIMESTAMP '2022-01-24T23:54:25.095574Z'";
Instant beforeQueryInstant = Instant.parse("2022-01-24T23:54:25.095574Z");
long microsBeforeQuery =
TimeUnit.SECONDS.toMicros(beforeQueryInstant.getEpochSecond())
+ TimeUnit.NANOSECONDS.toMicros(beforeQueryInstant.getNano());

// Verify that timestamp remains the same when priority is set to INTERACTIVE
TableResult result =
bigquery.query(
QueryJobConfiguration.newBuilder(query)
.setDefaultDataset(DatasetId.of(DATASET))
.setPriority(QueryJobConfiguration.Priority.INTERACTIVE)
.build());
for (FieldValueList row : result.getValues()) {
FieldValue timeStampCell = row.get(0);
long microsAfterQuery = timeStampCell.getTimestampValue();
assertEquals(microsBeforeQuery, microsAfterQuery);
}

// Verify that timestamp remains the same without priority set to INTERACTIVE
TableResult resultInteractive =
bigquery.query(
QueryJobConfiguration.newBuilder(query)
.setDefaultDataset(DatasetId.of(DATASET))
.build());
for (FieldValueList row : resultInteractive.getValues()) {
FieldValue timeStampCell = row.get(0);
long microsAfterQuery = timeStampCell.getTimestampValue();
assertEquals(microsBeforeQuery, microsAfterQuery);
}
}

@Test
public void testQueryCaseInsensitiveSchemaFieldByGetName() throws InterruptedException {
String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable();
Expand Down