Skip to content

Commit 9e84705

Browse files
authored
fix(bigquery): roundtrip readonly fields (#13370)
Previously, the bigquery library would return errors if attempts to roundtrip values for readonly fields was attempted. This PR removes that behavior and silently propagates the values, even though we expect the service to ignore them. Related: internal b/460218341
1 parent 377eb13 commit 9e84705

File tree

5 files changed

+24
-64
lines changed

5 files changed

+24
-64
lines changed

bigquery/dataset.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,23 @@ func (dm *DatasetMetadata) toBQ() (*bq.Dataset, error) {
255255
ds.StorageBillingModel = string(dm.StorageBillingModel)
256256
ds.IsCaseInsensitive = dm.IsCaseInsensitive
257257
ds.Labels = dm.Labels
258-
var err error
259-
ds.Access, err = accessListToBQ(dm.Access)
260-
if err != nil {
261-
return nil, err
262-
}
258+
// These fields are read-only, but we'll populate them should there be a change in the service behavior.
263259
if !dm.CreationTime.IsZero() {
264-
return nil, errors.New("bigquery: Dataset.CreationTime is not writable")
260+
ds.CreationTime = dm.CreationTime.UnixMilli()
265261
}
266262
if !dm.LastModifiedTime.IsZero() {
267-
return nil, errors.New("bigquery: Dataset.LastModifiedTime is not writable")
268-
}
269-
if dm.FullID != "" {
270-
return nil, errors.New("bigquery: Dataset.FullID is not writable")
263+
ds.LastModifiedTime = dm.LastModifiedTime.UnixMilli()
271264
}
272-
if dm.ETag != "" {
273-
return nil, errors.New("bigquery: Dataset.ETag is not writable")
265+
ds.Etag = dm.ETag
266+
ds.Id = dm.FullID
267+
268+
// access list
269+
var err error
270+
ds.Access, err = accessListToBQ(dm.Access)
271+
if err != nil {
272+
return nil, err
274273
}
274+
275275
if dm.DefaultEncryptionConfig != nil {
276276
ds.DefaultEncryptionConfiguration = dm.DefaultEncryptionConfig.toBQ()
277277
}

bigquery/dataset_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -382,19 +382,6 @@ func TestDatasetToBQ(t *testing.T) {
382382
t.Errorf("got=-, want=+:\n%s", diff)
383383
}
384384
}
385-
386-
// Check that non-writeable fields are unset.
387-
aTime := time.Date(2017, 1, 26, 0, 0, 0, 0, time.Local)
388-
for _, dm := range []*DatasetMetadata{
389-
{CreationTime: aTime},
390-
{LastModifiedTime: aTime},
391-
{FullID: "x"},
392-
{ETag: "e"},
393-
} {
394-
if _, err := dm.toBQ(); err == nil {
395-
t.Errorf("%+v: got nil, want error", dm)
396-
}
397-
}
398385
}
399386

400387
func TestBQToDatasetMetadata(t *testing.T) {

bigquery/routine.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package bigquery
1616

1717
import (
1818
"context"
19-
"errors"
2019
"fmt"
2120
"strings"
2221
"time"
@@ -310,14 +309,12 @@ func (rm *RoutineMetadata) toBQ() (*bq.Routine, error) {
310309
r.RemoteFunctionOptions = rm.RemoteFunctionOptions.toBQ()
311310
}
312311
if !rm.CreationTime.IsZero() {
313-
return nil, errors.New("cannot set CreationTime on create")
312+
r.CreationTime = rm.CreationTime.UnixMilli()
314313
}
315314
if !rm.LastModifiedTime.IsZero() {
316-
return nil, errors.New("cannot set LastModifiedTime on create")
317-
}
318-
if rm.ETag != "" {
319-
return nil, errors.New("cannot set ETag on create")
315+
r.LastModifiedTime = rm.LastModifiedTime.UnixMilli()
320316
}
317+
r.Etag = rm.ETag
321318
return r, nil
322319
}
323320

bigquery/table.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -855,32 +855,18 @@ func (tm *TableMetadata) toBQ() (*bq.Table, error) {
855855
t.ExternalDataConfiguration = &edc
856856
}
857857
t.EncryptionConfiguration = tm.EncryptionConfig.toBQ()
858-
if tm.FullID != "" {
859-
return nil, errors.New("cannot set FullID on create")
860-
}
861-
if tm.Type != "" {
862-
return nil, errors.New("cannot set Type on create")
863-
}
858+
// Propagate read-only fields.
859+
t.Id = tm.FullID
860+
t.Type = string(tm.Type)
861+
t.NumBytes = tm.NumBytes
862+
t.NumLongTermBytes = tm.NumLongTermBytes
863+
t.NumRows = tm.NumRows
864+
t.Etag = tm.ETag
864865
if !tm.CreationTime.IsZero() {
865-
return nil, errors.New("cannot set CreationTime on create")
866+
t.CreationTime = tm.CreationTime.UnixMilli()
866867
}
867868
if !tm.LastModifiedTime.IsZero() {
868-
return nil, errors.New("cannot set LastModifiedTime on create")
869-
}
870-
if tm.NumBytes != 0 {
871-
return nil, errors.New("cannot set NumBytes on create")
872-
}
873-
if tm.NumLongTermBytes != 0 {
874-
return nil, errors.New("cannot set NumLongTermBytes on create")
875-
}
876-
if tm.NumRows != 0 {
877-
return nil, errors.New("cannot set NumRows on create")
878-
}
879-
if tm.StreamingBuffer != nil {
880-
return nil, errors.New("cannot set StreamingBuffer on create")
881-
}
882-
if tm.ETag != "" {
883-
return nil, errors.New("cannot set ETag on create")
869+
t.LastModifiedTime = uint64(tm.LastModifiedTime.UnixMilli())
884870
}
885871
t.DefaultCollation = string(tm.DefaultCollation)
886872

bigquery/table_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,6 @@ func TestTableMetadataToBQ(t *testing.T) {
361361
{Schema: sc, ViewQuery: "q"}, // can't have both schema and query
362362
{UseLegacySQL: true}, // UseLegacySQL without query
363363
{UseStandardSQL: true}, // UseStandardSQL without query
364-
// read-only fields
365-
{FullID: "x"},
366-
{Type: "x"},
367-
{CreationTime: aTime},
368-
{LastModifiedTime: aTime},
369-
{NumBytes: 1},
370-
{NumLongTermBytes: 1},
371-
{NumRows: 1},
372-
{StreamingBuffer: &StreamingBuffer{}},
373-
{ETag: "x"},
374364
// expiration time outside allowable range is invalid
375365
// See https://godoc.org/time#Time.UnixNano
376366
{ExpirationTime: time.Date(1677, 9, 21, 0, 12, 43, 145224192, time.UTC).Add(-1)},

0 commit comments

Comments
 (0)