2525import com .google .api .services .bigquery .model .TableFieldSchema ;
2626import com .google .common .base .Function ;
2727import com .google .common .base .MoreObjects ;
28+ import com .google .common .base .Preconditions ;
2829import com .google .common .collect .Lists ;
2930import java .io .Serializable ;
3031import 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 }
0 commit comments