Skip to content

Commit 52020af

Browse files
authored
feat(bigquery): add timestamp precision support to schema (#13421)
towards internal b/447623020
1 parent 9e84705 commit 52020af

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

bigquery/schema.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ type FieldSchema struct {
157157
// values of NUMERIC and BIGNUMERIC type.
158158
// If unspecified, default value is RoundHalfAwayFromZero.
159159
RoundingMode RoundingMode
160+
161+
// Precision (maximum number of total digits in
162+
// base 10) for seconds of TIMESTAMP type. Possible values include:
163+
// * 6 (Default, for TIMESTAMP type with microsecond precision)
164+
// * 12 (For TIMESTAMP type with picosecond precision)
165+
TimestampPrecision int64
160166
}
161167

162168
func (fs *FieldSchema) toBQ() *bq.TableFieldSchema {
@@ -174,6 +180,10 @@ func (fs *FieldSchema) toBQ() *bq.TableFieldSchema {
174180
RoundingMode: string(fs.RoundingMode),
175181
}
176182

183+
if fs.TimestampPrecision > 0 {
184+
tfs.TimestampPrecision = &fs.TimestampPrecision
185+
}
186+
177187
if fs.Repeated {
178188
tfs.Mode = "REPEATED"
179189
} else if fs.Required {
@@ -261,6 +271,9 @@ func bqToFieldSchema(tfs *bq.TableFieldSchema) *FieldSchema {
261271
RangeElementType: bqToRangeElementType(tfs.RangeElementType),
262272
RoundingMode: RoundingMode(tfs.RoundingMode),
263273
}
274+
if tfs.TimestampPrecision != nil {
275+
fs.TimestampPrecision = *tfs.TimestampPrecision
276+
}
264277

265278
for _, f := range tfs.Fields {
266279
fs.Schema = append(fs.Schema, bqToFieldSchema(f))

bigquery/schema_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,26 @@ func TestSchemaConversion(t *testing.T) {
249249
fieldSchema("desc", "name", "TIMESTAMP", false, false, nil),
250250
},
251251
},
252+
{
253+
// Timestamp with precision
254+
bqSchema: &bq.TableSchema{
255+
Fields: []*bq.TableFieldSchema{
256+
{
257+
Description: "desc",
258+
Name: "name",
259+
Type: "TIMESTAMP",
260+
TimestampPrecision: int64ptr(12),
261+
},
262+
}},
263+
schema: Schema{
264+
{
265+
Description: "desc",
266+
Name: "name",
267+
Type: "TIMESTAMP",
268+
TimestampPrecision: 12,
269+
},
270+
},
271+
},
252272
{
253273
// civil times
254274
bqSchema: &bq.TableSchema{

bigquery/table_integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func TestIntegration_TableCreateWithConstraints(t *testing.T) {
9595
{Name: "bytes_col", Type: BytesFieldType, MaxLength: 150},
9696
{Name: "num_col", Type: NumericFieldType, Precision: 20},
9797
{Name: "bignumeric_col", Type: BigNumericFieldType, Precision: 30, Scale: 5},
98+
{Name: "timestamp_col", Type: TimestampFieldType, TimestampPrecision: 12},
9899
}
99100
err := table.Create(context.Background(), &TableMetadata{
100101
Schema: schema,

bigquery/value.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
bq "google.golang.org/api/bigquery/v2"
2929
)
3030

31+
// Utility functions to deal with byproducts of wrapper types in discovery.
32+
func int64ptr(v int64) *int64 { return &v }
33+
3134
// Value stores the contents of a single cell from a BigQuery result.
3235
type Value interface{}
3336

0 commit comments

Comments
 (0)