Skip to content

Commit

Permalink
fix: fix timestamp ronuding issue
Browse files Browse the repository at this point in the history
Fixes #1644
  • Loading branch information
stephaniewang526 committed Oct 8, 2021
1 parent d700577 commit a5f6d46
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
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,7 @@ 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);
BigDecimal scaled = secondsWithMicro.scaleByPowerOfTen(6).setScale(0, RoundingMode.HALF_UP);
return scaled.longValue();
}

Expand Down
Original file line number Diff line number Diff line change
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 @@ -682,6 +683,22 @@ public void testGetNonExistingTable() {
assertNull(bigquery.getTable(DATASET, "test_get_non_existing_table"));
}

@Test
public void testCreateTableExpiration() {
String tableName = "test_create_table_expiration";
TableId tableId = TableId.of(DATASET, tableName);
try {
StandardTableDefinition tableDefinition = StandardTableDefinition.newBuilder().build();
Table createdTable = bigquery.create(TableInfo.newBuilder(tableId, tableDefinition).build());
assertNotNull(createdTable);
Table updatedTable =
bigquery.update(createdTable.toBuilder().setExpirationTime(null).build());
assertThat(updatedTable.getExpirationTime()).isEqualTo(null);
} finally {
bigquery.delete(tableId);
}
}

@Test
public void testCreateTableWithRangePartitioning() {
String tableName = "test_create_table_rangepartitioning";
Expand Down Expand Up @@ -1854,6 +1871,41 @@ 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))
.setPriority(QueryJobConfiguration.Priority.INTERACTIVE)
.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

0 comments on commit a5f6d46

Please sign in to comment.