Content-Length: 383449 | pFad | https://github.com/googleapis/java-bigquery/commit/113303fb41ec4855bb81a5bd3c7f8984bc70da3e

1D feat: add getTimestampInstant() method to FieldValue (#2350) · googleapis/java-bigquery@113303f · GitHub
Skip to content

Commit 113303f

Browse files
feat: add getTimestampInstant() method to FieldValue (#2350)
* feat: add getReadableTimestampValue() method to FieldValue * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: return java.time.Instant * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: change method name * Update FieldValue.java * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d3f6a6b commit 113303f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkState;
21+
import static java.time.temporal.ChronoUnit.MICROS;
2122

2223
import com.google.api.client.util.Data;
2324
import com.google.api.core.BetaApi;
@@ -26,6 +27,7 @@
2627
import java.io.Serializable;
2728
import java.math.BigDecimal;
2829
import java.math.RoundingMode;
30+
import java.time.Instant;
2931
import java.util.List;
3032
import java.util.Map;
3133
import java.util.Objects;
@@ -36,6 +38,7 @@
3638
* query or when listing table data.
3739
*/
3840
public class FieldValue implements Serializable {
41+
3942
private static final int MICROSECONDS = 1000000;
4043
private static final long serialVersionUID = 469098630191710061L;
4144

@@ -191,6 +194,21 @@ public long getTimestampValue() {
191194
return scaled.longValue();
192195
}
193196

197+
/**
198+
* Returns this field's value as a {@code String}, representing a timestamp as an Instant. This
199+
* method should only be used if the corresponding field has {@link LegacySQLTypeName#TIMESTAMP}
200+
* type.
201+
*
202+
* @throws ClassCastException if the field is not a primitive type
203+
* @throws NumberFormatException if the field's value could not be converted to {@link Long}
204+
* @throws NullPointerException if {@link #isNull()} returns {@code true}
205+
*/
206+
@SuppressWarnings("unchecked")
207+
public Instant getTimestampInstant() {
208+
checkNotNull(value);
209+
return Instant.EPOCH.plus(getTimestampValue(), MICROS);
210+
}
211+
194212
/**
195213
* Returns this field's value as a {@link java.math.BigDecimal}. This method should only be used
196214
* if the corresponding field has {@link LegacySQLTypeName#NUMERIC} type.

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,23 @@ public void testMultipleStatementsQueryException() throws InterruptedException {
23822382
}
23832383
}
23842384

2385+
@Test
2386+
public void testTimestamp() throws InterruptedException {
2387+
String query = "SELECT TIMESTAMP '2022-01-24T23:54:25.095574Z'";
2388+
String timestampStringValueExpected = "2022-01-24T23:54:25.095574Z";
2389+
2390+
TableResult resultInteractive =
2391+
bigquery.query(
2392+
QueryJobConfiguration.newBuilder(query)
2393+
.setDefaultDataset(DatasetId.of(DATASET))
2394+
.build());
2395+
for (FieldValueList row : resultInteractive.getValues()) {
2396+
FieldValue timeStampCell = row.get(0);
2397+
Instant timestampStringValueActual = timeStampCell.getTimestampInstant();
2398+
assertEquals(timestampStringValueExpected, timestampStringValueActual.toString());
2399+
}
2400+
}
2401+
23852402
/* TODO(prasmish): replicate the entire test case for executeSelect */
23862403
@Test
23872404
public void testQuery() throws InterruptedException {

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/googleapis/java-bigquery/commit/113303fb41ec4855bb81a5bd3c7f8984bc70da3e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy