FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: Add timestamp_precision to Field by lqiu96 · Pull Request #4014 · googleapis/java-bigquery · GitHub

This repository was archived by the owner on Mar 23, 2026. It is now read-only.
/ java-bigquery Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
      • Field.java
      • FieldTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.List;
Expand Down Expand Up @@ -62,6 +63,7 @@ public TableFieldSchema apply(Field field) {
private final Long maxLength;
private final Long scale;
private final Long precision;
private final Long timestampPrecision;
private final String defaultValueExpression;
private final String collation;
private final FieldElementType rangeElementType;
Expand All @@ -88,6 +90,7 @@ public static final class Builder {
private Long maxLength;
private Long scale;
private Long precision;
private Long timestampPrecision;
private String defaultValueExpression;
private String collation;
private FieldElementType rangeElementType;
Expand All @@ -104,6 +107,7 @@ private Builder(Field field) {
this.maxLength = field.maxLength;
this.scale = field.scale;
this.precision = field.precision;
this.timestampPrecision = field.timestampPrecision;
this.defaultValueExpression = field.defaultValueExpression;
this.collation = field.collation;
this.rangeElementType = field.rangeElementType;
Expand Down Expand Up @@ -254,6 +258,19 @@ public Builder setPrecision(Long precision) {
return this;
}

/**
* Specifies the precision for TIMESTAMP types.
*
* <p>The default value is 6. Possible values are 6 (microsecond) or 12 (picosecond).
*/
public Builder setTimestampPrecision(Long timestampPrecision) {
Preconditions.checkArgument(
timestampPrecision == 6L || timestampPrecision == 12L,
"Timestamp Precision must be 6 (microsecond) or 12 (picosecond)");
this.timestampPrecision = timestampPrecision;
return this;
}
Comment thread
lqiu96 marked this conversation as resolved.

/**
* DefaultValueExpression is used to specify the default value of a field using a SQL
* expression. It can only be set for top level fields (columns).
Expand Down Expand Up @@ -317,6 +334,7 @@ private Field(Builder builder) {
this.maxLength = builder.maxLength;
this.scale = builder.scale;
this.precision = builder.precision;
this.timestampPrecision = builder.timestampPrecision;
this.defaultValueExpression = builder.defaultValueExpression;
this.collation = builder.collation;
this.rangeElementType = builder.rangeElementType;
Expand Down Expand Up @@ -370,6 +388,11 @@ public Long getPrecision() {
return precision;
}

/** Returns the precision for TIMESTAMP type. */
public Long getTimestampPrecision() {
return timestampPrecision;
}

/** Return the default value of the field. */
public String getDefaultValueExpression() {
return defaultValueExpression;
Expand Down Expand Up @@ -408,6 +431,7 @@ public String toString() {
.add("maxLength", maxLength)
.add("scale", scale)
.add("precision", precision)
.add("timestampPrecision", timestampPrecision)
.add("defaultValueExpression", defaultValueExpression)
.add("collation", collation)
.add("rangeElementType", rangeElementType)
Expand All @@ -416,7 +440,19 @@ public String toString() {

@Override
public int hashCode() {
return Objects.hash(name, type, mode, description, policyTags, rangeElementType);
return Objects.hash(
name,
type,
mode,
description,
policyTags,
maxLength,
scale,
precision,
timestampPrecision,
defaultValueExpression,
collation,
rangeElementType);
}

@Override
Expand Down Expand Up @@ -490,6 +526,9 @@ TableFieldSchema toPb() {
if (precision != null) {
fieldSchemaPb.setPrecision(precision);
}
if (timestampPrecision != null) {
fieldSchemaPb.setTimestampPrecision(timestampPrecision);
}
if (defaultValueExpression != null) {
fieldSchemaPb.setDefaultValueExpression(defaultValueExpression);
}
Expand Down Expand Up @@ -527,6 +566,9 @@ static Field fromPb(TableFieldSchema fieldSchemaPb) {
if (fieldSchemaPb.getPrecision() != null) {
fieldBuilder.setPrecision(fieldSchemaPb.getPrecision());
}
if (fieldSchemaPb.getTimestampPrecision() != null) {
fieldBuilder.setTimestampPrecision(fieldSchemaPb.getTimestampPrecision());
}
if (fieldSchemaPb.getDefaultValueExpression() != null) {
fieldBuilder.setDefaultValueExpression(fieldSchemaPb.getDefaultValueExpression());
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.bigquery;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

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

@Test
public void setTimestampPrecisionValues() {
Field.Builder builder = Field.newBuilder(FIELD_NAME1, FIELD_TYPE1);

// Value values: 6L or 12L
builder.setTimestampPrecision(6L);
builder.setTimestampPrecision(12L);

assertThrows(IllegalArgumentException.class, () -> builder.setTimestampPrecision(-1L));
assertThrows(IllegalArgumentException.class, () -> builder.setTimestampPrecision(0L));
assertThrows(IllegalArgumentException.class, () -> builder.setTimestampPrecision(5L));
assertThrows(IllegalArgumentException.class, () -> builder.setTimestampPrecision(13L));
}

private void compareFieldSchemas(Field expected, Field value) {
assertEquals(expected, value);
assertEquals(expected.getName(), value.getName());
Expand Down

Back | FazBrowse Home | New Git URL