Skip to content

Commit 57ffe1d

Browse files
feat: Add timestamp_precision to Field (#4014)
* feat: Add timestamp_precision to Field * Apply suggestion from @gemini-code-assist[bot] Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * chore: Address GCA PR feedback * chore: Fix typo * chore: Remove default value * chore: Address PR feedback --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 6dcc900 commit 57ffe1d

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.api.services.bigquery.model.TableFieldSchema;
2626
import com.google.common.base.Function;
2727
import com.google.common.base.MoreObjects;
28+
import com.google.common.base.Preconditions;
2829
import com.google.common.collect.Lists;
2930
import java.io.Serializable;
3031
import java.util.List;
@@ -62,6 +63,7 @@ public TableFieldSchema apply(Field field) {
6263
private final Long maxLength;
6364
private final Long scale;
6465
private final Long precision;
66+
private final Long timestampPrecision;
6567
private final String defaultValueExpression;
6668
private final String collation;
6769
private final FieldElementType rangeElementType;
@@ -88,6 +90,7 @@ public static final class Builder {
8890
private Long maxLength;
8991
private Long scale;
9092
private Long precision;
93+
private Long timestampPrecision;
9194
private String defaultValueExpression;
9295
private String collation;
9396
private FieldElementType rangeElementType;
@@ -104,6 +107,7 @@ private Builder(Field field) {
104107
this.maxLength = field.maxLength;
105108
this.scale = field.scale;
106109
this.precision = field.precision;
110+
this.timestampPrecision = field.timestampPrecision;
107111
this.defaultValueExpression = field.defaultValueExpression;
108112
this.collation = field.collation;
109113
this.rangeElementType = field.rangeElementType;
@@ -254,6 +258,19 @@ public Builder setPrecision(Long precision) {
254258
return this;
255259
}
256260

261+
/**
262+
* Specifies the precision for TIMESTAMP types.
263+
*
264+
* <p>The default value is 6. Possible values are 6 (microsecond) or 12 (picosecond).
265+
*/
266+
public Builder setTimestampPrecision(Long timestampPrecision) {
267+
Preconditions.checkArgument(
268+
timestampPrecision == 6L || timestampPrecision == 12L,
269+
"Timestamp Precision must be 6 (microsecond) or 12 (picosecond)");
270+
this.timestampPrecision = timestampPrecision;
271+
return this;
272+
}
273+
257274
/**
258275
* DefaultValueExpression is used to specify the default value of a field using a SQL
259276
* expression. It can only be set for top level fields (columns).
@@ -317,6 +334,7 @@ private Field(Builder builder) {
317334
this.maxLength = builder.maxLength;
318335
this.scale = builder.scale;
319336
this.precision = builder.precision;
337+
this.timestampPrecision = builder.timestampPrecision;
320338
this.defaultValueExpression = builder.defaultValueExpression;
321339
this.collation = builder.collation;
322340
this.rangeElementType = builder.rangeElementType;
@@ -370,6 +388,11 @@ public Long getPrecision() {
370388
return precision;
371389
}
372390

391+
/** Returns the precision for TIMESTAMP type. */
392+
public Long getTimestampPrecision() {
393+
return timestampPrecision;
394+
}
395+
373396
/** Return the default value of the field. */
374397
public String getDefaultValueExpression() {
375398
return defaultValueExpression;
@@ -408,6 +431,7 @@ public String toString() {
408431
.add("maxLength", maxLength)
409432
.add("scale", scale)
410433
.add("precision", precision)
434+
.add("timestampPrecision", timestampPrecision)
411435
.add("defaultValueExpression", defaultValueExpression)
412436
.add("collation", collation)
413437
.add("rangeElementType", rangeElementType)
@@ -416,7 +440,19 @@ public String toString() {
416440

417441
@Override
418442
public int hashCode() {
419-
return Objects.hash(name, type, mode, description, policyTags, rangeElementType);
443+
return Objects.hash(
444+
name,
445+
type,
446+
mode,
447+
description,
448+
policyTags,
449+
maxLength,
450+
scale,
451+
precision,
452+
timestampPrecision,
453+
defaultValueExpression,
454+
collation,
455+
rangeElementType);
420456
}
421457

422458
@Override
@@ -490,6 +526,9 @@ TableFieldSchema toPb() {
490526
if (precision != null) {
491527
fieldSchemaPb.setPrecision(precision);
492528
}
529+
if (timestampPrecision != null) {
530+
fieldSchemaPb.setTimestampPrecision(timestampPrecision);
531+
}
493532
if (defaultValueExpression != null) {
494533
fieldSchemaPb.setDefaultValueExpression(defaultValueExpression);
495534
}
@@ -527,6 +566,9 @@ static Field fromPb(TableFieldSchema fieldSchemaPb) {
527566
if (fieldSchemaPb.getPrecision() != null) {
528567
fieldBuilder.setPrecision(fieldSchemaPb.getPrecision());
529568
}
569+
if (fieldSchemaPb.getTimestampPrecision() != null) {
570+
fieldBuilder.setTimestampPrecision(fieldSchemaPb.getTimestampPrecision());
571+
}
530572
if (fieldSchemaPb.getDefaultValueExpression() != null) {
531573
fieldBuilder.setDefaultValueExpression(fieldSchemaPb.getDefaultValueExpression());
532574
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.bigquery;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertThrows;
2021

2122
import java.io.ByteArrayInputStream;
2223
import java.io.ByteArrayOutputStream;
@@ -213,6 +214,20 @@ public void testSubFieldWithClonedType() throws Exception {
213214
Field.of("field", clonedRecord, Field.of("subfield", LegacySQLTypeName.BOOLEAN));
214215
}
215216

217+
@Test
218+
public void setTimestampPrecisionValues() {
219+
Field.Builder builder = Field.newBuilder(FIELD_NAME1, FIELD_TYPE1);
220+
221+
// Value values: 6L or 12L
222+
builder.setTimestampPrecision(6L);
223+
builder.setTimestampPrecision(12L);
224+
225+
assertThrows(IllegalArgumentException.class, () -> builder.setTimestampPrecision(-1L));
226+
assertThrows(IllegalArgumentException.class, () -> builder.setTimestampPrecision(0L));
227+
assertThrows(IllegalArgumentException.class, () -> builder.setTimestampPrecision(5L));
228+
assertThrows(IllegalArgumentException.class, () -> builder.setTimestampPrecision(13L));
229+
}
230+
216231
private void compareFieldSchemas(Field expected, Field value) {
217232
assertEquals(expected, value);
218233
assertEquals(expected.getName(), value.getName());

0 commit comments

Comments
 (0)