Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 6e16c81

Browse files
committed
fix: Update existing references of useInt64Timestamp to use DataFormatOption's variant
1 parent 9caca67 commit 6e16c81

5 files changed

Lines changed: 90 additions & 63 deletions

File tree

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,10 +2066,8 @@ && getOptions().getOpenTelemetryTracer() != null) {
20662066
}
20672067
try (Scope queryScope = querySpan != null ? querySpan.makeCurrent() : null) {
20682068
// If all parameters passed in configuration are supported by the query() method on the
2069-
// backend,
2070-
// put on fast path
2071-
QueryRequestInfo requestInfo =
2072-
new QueryRequestInfo(configuration, getOptions().getUseInt64Timestamps());
2069+
// backend, put on fast path
2070+
QueryRequestInfo requestInfo = new QueryRequestInfo(configuration, getOptions().getDataFormatOptions());
20732071
if (requestInfo.isFastQuerySupported(jobId)) {
20742072
// Be careful when setting the projectID in JobId, if a projectID is specified in the JobId,
20752073
// the job created by the query method will use that project. This may cause the query to

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.cloud.bigquery.spi.BigQueryRpcFactory;
2828
import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc;
2929
import com.google.cloud.http.HttpTransportOptions;
30+
import com.google.common.base.Preconditions;
3031
import com.google.common.collect.ImmutableSet;
3132
import io.opentelemetry.api.trace.Tracer;
3233
import java.util.Set;
@@ -72,7 +73,7 @@ public static class Builder extends ServiceOptions.Builder<BigQuery, BigQueryOpt
7273

7374
private String location;
7475
private boolean useInt64Timestamps;
75-
private DataFormatOptions dataFormatOptions = DataFormatOptions.newBuilder().build();
76+
private DataFormatOptions dataFormatOptions;
7677
private boolean enableOpenTelemetryTracing;
7778
private Tracer openTelemetryTracer;
7879
private ResultRetryAlgorithm<?> resultRetryAlgorithm;
@@ -118,6 +119,7 @@ public Builder setUseInt64Timestamps(boolean useInt64Timestamps) {
118119
* @param dataFormatOptions Configuration of the formatting options
119120
*/
120121
public Builder setDataFormatOptions(DataFormatOptions dataFormatOptions) {
122+
Preconditions.checkNotNull(dataFormatOptions, "DataFormatOptions cannot be null");
121123
this.dataFormatOptions = dataFormatOptions;
122124
return this;
123125
}
@@ -159,14 +161,22 @@ private BigQueryOptions(Builder builder) {
159161
super(BigQueryFactory.class, BigQueryRpcFactory.class, builder, new BigQueryDefaults());
160162
this.location = builder.location;
161163
this.useInt64Timestamps = builder.useInt64Timestamps;
162-
this.dataFormatOptions = builder.dataFormatOptions;
163164
this.enableOpenTelemetryTracing = builder.enableOpenTelemetryTracing;
164165
this.openTelemetryTracer = builder.openTelemetryTracer;
165166
if (builder.resultRetryAlgorithm != null) {
166167
this.resultRetryAlgorithm = builder.resultRetryAlgorithm;
167168
} else {
168169
this.resultRetryAlgorithm = BigQueryBaseService.DEFAULT_BIGQUERY_EXCEPTION_HANDLER;
169170
}
171+
172+
// If dataFormatOptions is not set, then create a new instance and set it with the
173+
// useInt64Timestamps configured in BigQueryOptions
174+
if (builder.dataFormatOptions == null) {
175+
this.dataFormatOptions =
176+
DataFormatOptions.newBuilder().useInt64Timestamp(builder.useInt64Timestamps).build();
177+
} else {
178+
this.dataFormatOptions = builder.dataFormatOptions;
179+
}
170180
}
171181

172182
private static class BigQueryDefaults implements ServiceDefaults<BigQuery, BigQueryOptions> {
@@ -215,6 +225,16 @@ public void setThrowNotFound(boolean setThrowNotFound) {
215225
this.setThrowNotFound = setThrowNotFound;
216226
}
217227

228+
/**
229+
* This setter is marked as Obsolete. Prefer {@link
230+
* Builder#setDataFormatOptions(DataFormatOptions)} to set the int64timestamp configuration
231+
* instead.
232+
*
233+
* <p>If useInt64Timestamps is set via DataFormatOptions, then that value will be used.
234+
*
235+
* <p>{@code DataFormatOptions.newBuilder().setUseInt64Timestamp(...).build()}
236+
*/
237+
@ObsoleteApi("Use setDataFormatOptions(DataFormatOptions) instead")
218238
public void setUseInt64Timestamps(boolean useInt64Timestamps) {
219239
this.useInt64Timestamps = useInt64Timestamps;
220240
}
@@ -230,6 +250,11 @@ public boolean getThrowNotFound() {
230250
return setThrowNotFound;
231251
}
232252

253+
/**
254+
* This getter is marked as Obsolete. Prefer {@link #getDataFormatOptions().isUseInt64Timestamp()}
255+
* to set the int64timestamp configuration instead.
256+
*/
257+
@ObsoleteApi("Use getDataFormatOptions().isUseInt64Timestamp() instead")
233258
public boolean getUseInt64Timestamps() {
234259
return useInt64Timestamps;
235260
}
Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.google.cloud.bigquery;
217

18+
import com.google.auto.value.AutoValue;
19+
import org.jspecify.annotations.Nullable;
20+
21+
import java.io.Serializable;
22+
323
/**
424
* Google BigQuery DataFormatOptions. Configures the output format for data types returned from
525
* BigQuery.
626
*/
7-
public class DataFormatOptions {
27+
@AutoValue
28+
public abstract class DataFormatOptions implements Serializable {
829
public enum TimestampFormatOptions {
930
TIMESTAMP_OUTPUT_FORMAT_UNSPECIFIED("TIMESTAMP_OUTPUT_FORMAT_UNSPECIFIED"),
1031
FLOAT64("FLOAT64"),
@@ -23,76 +44,50 @@ public String toString() {
2344
}
2445
}
2546

26-
private final boolean useInt64Timestamp;
27-
private final TimestampFormatOptions timestampFormatOptions;
28-
29-
DataFormatOptions() {
30-
this(new Builder());
31-
}
32-
33-
DataFormatOptions(Builder builder) {
34-
this.useInt64Timestamp = builder.useInt64Timestamp;
35-
this.timestampFormatOptions = builder.timestampFormatOptions;
36-
}
47+
public abstract boolean useInt64Timestamp();
3748

38-
public boolean isUseInt64Timestamp() {
39-
return useInt64Timestamp;
40-
}
41-
42-
public TimestampFormatOptions getTimestampFormatOptions() {
43-
return timestampFormatOptions;
44-
}
49+
@Nullable
50+
public abstract TimestampFormatOptions timestampFormatOptions();
4551

4652
public static Builder newBuilder() {
47-
return new Builder();
53+
return new AutoValue_DataFormatOptions.Builder()
54+
.useInt64Timestamp(false);
4855
}
4956

50-
public Builder toBuilder() {
51-
return new Builder(this);
52-
}
57+
public abstract Builder toBuilder();
5358

54-
public static class Builder {
55-
private boolean useInt64Timestamp;
56-
private TimestampFormatOptions timestampFormatOptions =
57-
TimestampFormatOptions.TIMESTAMP_OUTPUT_FORMAT_UNSPECIFIED;
59+
@AutoValue.Builder
60+
public abstract static class Builder {
61+
public abstract Builder useInt64Timestamp(boolean useInt64Timestamp);
5862

59-
public Builder() {}
63+
public abstract Builder timestampFormatOptions(TimestampFormatOptions timestampFormatOptions);
6064

61-
public Builder(DataFormatOptions dataFormatOptions) {
62-
this.useInt64Timestamp = dataFormatOptions.useInt64Timestamp;
63-
this.timestampFormatOptions = dataFormatOptions.timestampFormatOptions;
64-
}
65+
abstract TimestampFormatOptions timestampFormatOptions();
6566

66-
public Builder setUseInt64Timestamp(boolean useInt64Timestamp) {
67-
this.useInt64Timestamp = useInt64Timestamp;
68-
return this;
69-
}
70-
71-
public Builder setTimestampFormatOptions(TimestampFormatOptions timestampFormatOptions) {
72-
this.timestampFormatOptions = timestampFormatOptions;
73-
return this;
74-
}
67+
abstract DataFormatOptions autoBuild();
7568

7669
public DataFormatOptions build() {
77-
return new DataFormatOptions(this);
70+
if (timestampFormatOptions() == null) {
71+
timestampFormatOptions(TimestampFormatOptions.TIMESTAMP_OUTPUT_FORMAT_UNSPECIFIED);
72+
}
73+
return autoBuild();
7874
}
7975
}
8076

8177
com.google.api.services.bigquery.model.DataFormatOptions toPb() {
8278
com.google.api.services.bigquery.model.DataFormatOptions request =
8379
new com.google.api.services.bigquery.model.DataFormatOptions();
84-
request.setUseInt64Timestamp(useInt64Timestamp);
85-
if (timestampFormatOptions != null) {
86-
request.setTimestampOutputFormat(timestampFormatOptions.toString());
80+
request.setUseInt64Timestamp(useInt64Timestamp());
81+
if (timestampFormatOptions() != null) {
82+
request.setTimestampOutputFormat(timestampFormatOptions().toString());
8783
}
8884
return request;
8985
}
9086

9187
DataFormatOptions fromPb(com.google.api.services.bigquery.model.DataFormatOptions request) {
92-
return new Builder()
93-
.setUseInt64Timestamp(request.getUseInt64Timestamp())
94-
.setTimestampFormatOptions(
95-
TimestampFormatOptions.valueOf(request.getTimestampOutputFormat()))
88+
return new AutoValue_DataFormatOptions.Builder()
89+
.useInt64Timestamp(request.getUseInt64Timestamp())
90+
.timestampFormatOptions(TimestampFormatOptions.valueOf(request.getTimestampOutputFormat()))
9691
.build();
9792
}
9893
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ final class QueryRequestInfo {
4646
private final DataFormatOptions formatOptions;
4747
private final String reservation;
4848

49-
QueryRequestInfo(QueryJobConfiguration config, Boolean useInt64Timestamps) {
49+
QueryRequestInfo(
50+
QueryJobConfiguration config, com.google.cloud.bigquery.DataFormatOptions dataFormatOptions) {
5051
this.config = config;
5152
this.connectionProperties = config.getConnectionProperties();
5253
this.defaultDataset = config.getDefaultDataset();
@@ -61,7 +62,7 @@ final class QueryRequestInfo {
6162
this.useLegacySql = config.useLegacySql();
6263
this.useQueryCache = config.useQueryCache();
6364
this.jobCreationMode = config.getJobCreationMode();
64-
this.formatOptions = new DataFormatOptions().setUseInt64Timestamp(useInt64Timestamps);
65+
this.formatOptions = dataFormatOptions.toPb();
6566
this.reservation = config.getReservation();
6667
}
6768

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public class QueryRequestInfoTest {
140140
.setJobCreationMode(jobCreationModeRequired)
141141
.setReservation(RESERVATION)
142142
.build();
143-
QueryRequestInfo REQUEST_INFO = new QueryRequestInfo(QUERY_JOB_CONFIGURATION, false);
143+
QueryRequestInfo REQUEST_INFO =
144+
new QueryRequestInfo(QUERY_JOB_CONFIGURATION, DataFormatOptions.newBuilder().build());
144145
private static final QueryJobConfiguration QUERY_JOB_CONFIGURATION_SUPPORTED =
145146
QueryJobConfiguration.newBuilder(QUERY)
146147
.setUseQueryCache(USE_QUERY_CACHE)
@@ -156,7 +157,8 @@ public class QueryRequestInfoTest {
156157
.setReservation(RESERVATION)
157158
.build();
158159
QueryRequestInfo REQUEST_INFO_SUPPORTED =
159-
new QueryRequestInfo(QUERY_JOB_CONFIGURATION_SUPPORTED, false);
160+
new QueryRequestInfo(
161+
QUERY_JOB_CONFIGURATION_SUPPORTED, DataFormatOptions.newBuilder().build());
160162

161163
@Test
162164
public void testIsFastQuerySupported() {
@@ -177,17 +179,23 @@ public void testToPb() {
177179
@Test
178180
public void equalTo() {
179181
compareQueryRequestInfo(
180-
new QueryRequestInfo(QUERY_JOB_CONFIGURATION_SUPPORTED, false), REQUEST_INFO_SUPPORTED);
181-
compareQueryRequestInfo(new QueryRequestInfo(QUERY_JOB_CONFIGURATION, false), REQUEST_INFO);
182+
new QueryRequestInfo(
183+
QUERY_JOB_CONFIGURATION_SUPPORTED, DataFormatOptions.newBuilder().build()),
184+
REQUEST_INFO_SUPPORTED);
185+
compareQueryRequestInfo(
186+
new QueryRequestInfo(QUERY_JOB_CONFIGURATION, DataFormatOptions.newBuilder().build()),
187+
REQUEST_INFO);
182188
}
183189

184190
@Test
185191
public void testInt64Timestamp() {
186-
QueryRequestInfo requestInfo = new QueryRequestInfo(QUERY_JOB_CONFIGURATION, false);
192+
QueryRequestInfo requestInfo =
193+
new QueryRequestInfo(QUERY_JOB_CONFIGURATION, DataFormatOptions.newBuilder().build());
187194
QueryRequest requestPb = requestInfo.toPb();
188195
assertFalse(requestPb.getFormatOptions().getUseInt64Timestamp());
189196

190-
QueryRequestInfo requestInfoLosslessTs = new QueryRequestInfo(QUERY_JOB_CONFIGURATION, true);
197+
QueryRequestInfo requestInfoLosslessTs =
198+
new QueryRequestInfo(QUERY_JOB_CONFIGURATION, DataFormatOptions.newBuilder().useInt64Timestamp(true).build());
191199
QueryRequest requestLosslessTsPb = requestInfoLosslessTs.toPb();
192200
assertTrue(requestLosslessTsPb.getFormatOptions().getUseInt64Timestamp());
193201
}

0 commit comments

Comments
 (0)