Skip to content

Commit 65e0927

Browse files
committed
chore: Add tests for useInt64Timestamp behavior
1 parent dd4e58f commit 65e0927

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,10 @@ public TableDataList call() throws IOException {
17311731
new PageImpl<>(
17321732
new TableDataPageFetcher(tableId, schema, serviceOptions, cursor, pageOptionMap),
17331733
cursor,
1734-
transformTableData(result.getRows(), schema, serviceOptions.getUseInt64Timestamps())),
1734+
transformTableData(
1735+
result.getRows(),
1736+
schema,
1737+
serviceOptions.getDataFormatOptions().useInt64Timestamp())),
17351738
result.getTotalRows());
17361739
} catch (BigQueryRetryHelperException e) {
17371740
throw BigQueryException.translateAndThrow(e);
@@ -2007,7 +2010,9 @@ public com.google.api.services.bigquery.model.QueryResponse call()
20072010
new QueryPageFetcher(jobId, schema, getOptions(), cursor, optionMap(options)),
20082011
cursor,
20092012
transformTableData(
2010-
results.getRows(), schema, getOptions().getUseInt64Timestamps())))
2013+
results.getRows(),
2014+
schema,
2015+
getOptions().getDataFormatOptions().useInt64Timestamp())))
20112016
.setJobId(jobId)
20122017
.setQueryId(results.getQueryId())
20132018
.build();
@@ -2021,7 +2026,9 @@ public com.google.api.services.bigquery.model.QueryResponse call()
20212026
new TableDataPageFetcher(null, schema, getOptions(), null, optionMap(options)),
20222027
null,
20232028
transformTableData(
2024-
results.getRows(), schema, getOptions().getUseInt64Timestamps())))
2029+
results.getRows(),
2030+
schema,
2031+
getOptions().getDataFormatOptions().useInt64Timestamp())))
20252032
// Return the JobID of the successful job
20262033
.setJobId(
20272034
results.getJobReference() != null ? JobId.fromPb(results.getJobReference()) : null)

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class BigQueryOptions extends ServiceOptions<BigQuery, BigQueryOptions> {
4343
// set the option ThrowNotFound when you want to throw the exception when the value not found
4444
private boolean setThrowNotFound;
4545
private boolean useInt64Timestamps;
46-
private final DataFormatOptions dataFormatOptions;
46+
private DataFormatOptions dataFormatOptions;
4747
private JobCreationMode defaultJobCreationMode = JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED;
4848
private boolean enableOpenTelemetryTracing;
4949
private Tracer openTelemetryTracer;
@@ -235,9 +235,13 @@ public void setThrowNotFound(boolean setThrowNotFound) {
235235
*
236236
* <p>Alternative: {@code DataFormatOptions.newBuilder().setUseInt64Timestamp(...).build()}
237237
*/
238-
@ObsoleteApi("Use setDataFormatOptions(DataFormatOptions) instead")
238+
@ObsoleteApi("Use Builder#setDataFormatOptions(DataFormatOptions) instead")
239239
public void setUseInt64Timestamps(boolean useInt64Timestamps) {
240240
this.useInt64Timestamps = useInt64Timestamps;
241+
// Because this setter exists outside the Builder, DataFormatOptions needs be rebuilt to
242+
// account for this setting.
243+
this.dataFormatOptions =
244+
dataFormatOptions.toBuilder().useInt64Timestamp(useInt64Timestamps).build();
241245
}
242246

243247
@Deprecated
@@ -262,7 +266,7 @@ public boolean getThrowNotFound() {
262266
*/
263267
@ObsoleteApi("Use getDataFormatOptions().isUseInt64Timestamp() instead")
264268
public boolean getUseInt64Timestamps() {
265-
return useInt64Timestamps;
269+
return dataFormatOptions.useInt64Timestamp();
266270
}
267271

268272
public DataFormatOptions getDataFormatOptions() {

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryOptionsTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package com.google.cloud.bigquery;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
1924
import com.google.cloud.TransportOptions;
2025
import org.junit.Assert;
2126
import org.junit.Test;
@@ -35,4 +40,52 @@ public void testInvalidTransport() {
3540
Assert.assertNotNull(expected.getMessage());
3641
}
3742
}
43+
44+
@Test
45+
public void dataFormatOptions_createdByDefault() {
46+
BigQueryOptions options = BigQueryOptions.newBuilder().build();
47+
48+
assertNotNull(options.getDataFormatOptions());
49+
assertFalse(options.getDataFormatOptions().useInt64Timestamp());
50+
assertEquals(
51+
DataFormatOptions.TimestampFormatOptions.TIMESTAMP_OUTPUT_FORMAT_UNSPECIFIED,
52+
options.getDataFormatOptions().timestampFormatOptions());
53+
}
54+
55+
@Test
56+
public void nonBuilderSetUseInt64Timestamp_capturedInDataFormatOptions() {
57+
BigQueryOptions options =
58+
BigQueryOptions.newBuilder()
59+
.setDataFormatOptions(DataFormatOptions.newBuilder().useInt64Timestamp(false).build())
60+
.build();
61+
options.setUseInt64Timestamps(true);
62+
63+
assertTrue(options.getDataFormatOptions().useInt64Timestamp());
64+
}
65+
66+
@Test
67+
public void nonBuilderSetUseInt64Timestamp_overridesEverything() {
68+
BigQueryOptions options = BigQueryOptions.newBuilder().build();
69+
options.setUseInt64Timestamps(true);
70+
71+
assertTrue(options.getDataFormatOptions().useInt64Timestamp());
72+
}
73+
74+
@Test
75+
public void noDataFormatOptions_capturesUseInt64TimestampSetInBuilder() {
76+
BigQueryOptions options = BigQueryOptions.newBuilder().setUseInt64Timestamps(true).build();
77+
78+
assertTrue(options.getDataFormatOptions().useInt64Timestamp());
79+
}
80+
81+
@Test
82+
public void dataFormatOptionsSetterHasPrecedence() {
83+
BigQueryOptions options =
84+
BigQueryOptions.newBuilder()
85+
.setDataFormatOptions(DataFormatOptions.newBuilder().useInt64Timestamp(true).build())
86+
.setUseInt64Timestamps(false)
87+
.build();
88+
89+
assertTrue(options.getDataFormatOptions().useInt64Timestamp());
90+
}
3891
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import com.google.cloud.bigquery.ConnectionSettings;
7272
import com.google.cloud.bigquery.CopyJobConfiguration;
7373
import com.google.cloud.bigquery.CsvOptions;
74+
import com.google.cloud.bigquery.DataFormatOptions;
7475
import com.google.cloud.bigquery.Dataset;
7576
import com.google.cloud.bigquery.DatasetId;
7677
import com.google.cloud.bigquery.DatasetInfo;
@@ -3462,8 +3463,11 @@ public void testLosslessTimestamp() throws InterruptedException {
34623463
// Create new BQ object to toggle lossless timestamps without affecting
34633464
// other tests.
34643465
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
3465-
BigQuery bigqueryLossless = bigqueryHelper.getOptions().getService();
3466-
bigqueryLossless.getOptions().setUseInt64Timestamps(true);
3466+
DataFormatOptions dataFormatOptions =
3467+
DataFormatOptions.newBuilder().useInt64Timestamp(true).build();
3468+
BigQueryOptions options =
3469+
bigqueryHelper.getOptions().toBuilder().setDataFormatOptions(dataFormatOptions).build();
3470+
BigQuery bigqueryLossless = options.getService();
34673471

34683472
TableResult resultLossless =
34693473
bigqueryLossless.query(

0 commit comments

Comments
 (0)