Skip to content

Commit 164492d

Browse files
authored
fix(bigquery): move MaxStaleness field to table level (#10066)
1 parent 9cde8ab commit 164492d

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

bigquery/integration_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3336,11 +3336,12 @@ func TestIntegration_MaterializedViewLifecycle(t *testing.T) {
33363336
`, qualified)
33373337

33383338
// Create materialized view
3339-
33403339
wantRefresh := 6 * time.Hour
3340+
maxStaleness := IntervalValueFromDuration(30 * time.Minute)
33413341
matViewID := tableIDs.New()
33423342
view := dataset.Table(matViewID)
33433343
if err := view.Create(ctx, &TableMetadata{
3344+
MaxStaleness: maxStaleness,
33443345
MaterializedView: &MaterializedViewDefinition{
33453346
Query: sql,
33463347
RefreshInterval: wantRefresh,
@@ -3366,6 +3367,10 @@ func TestIntegration_MaterializedViewLifecycle(t *testing.T) {
33663367
t.Errorf("mismatch on refresh time: got %d usec want %d usec", 1000*curMeta.MaterializedView.RefreshInterval.Nanoseconds(), 1000*wantRefresh.Nanoseconds())
33673368
}
33683369

3370+
if curMeta.MaxStaleness.String() != maxStaleness.String() {
3371+
t.Errorf("mismatch on max staleness: got %s want %s", curMeta.MaxStaleness, maxStaleness)
3372+
}
3373+
33693374
// MaterializedView is a TableType constant
33703375
want := MaterializedView
33713376
if curMeta.Type != want {
@@ -3374,7 +3379,9 @@ func TestIntegration_MaterializedViewLifecycle(t *testing.T) {
33743379

33753380
// Update metadata
33763381
wantRefresh = time.Hour // 6hr -> 1hr
3382+
maxStaleness = IntervalValueFromDuration(5 * time.Hour)
33773383
upd := TableMetadataToUpdate{
3384+
MaxStaleness: maxStaleness,
33783385
MaterializedView: &MaterializedViewDefinition{
33793386
Query: sql,
33803387
RefreshInterval: wantRefresh,
@@ -3394,6 +3401,10 @@ func TestIntegration_MaterializedViewLifecycle(t *testing.T) {
33943401
t.Errorf("mismatch on updated refresh time: got %d usec want %d usec", 1000*curMeta.MaterializedView.RefreshInterval.Nanoseconds(), 1000*wantRefresh.Nanoseconds())
33953402
}
33963403

3404+
if newMeta.MaxStaleness.String() != maxStaleness.String() {
3405+
t.Errorf("mismatch on max staleness: got %s want %s", newMeta.MaxStaleness, maxStaleness)
3406+
}
3407+
33973408
// verify implicit setting of false due to partial population of update.
33983409
if newMeta.MaterializedView.EnableRefresh {
33993410
t.Error("expected EnableRefresh to be false, is true")

bigquery/table.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ type TableMetadata struct {
152152
// Present only if the table has primary or foreign keys.
153153
TableConstraints *TableConstraints
154154

155+
// MaxStaleness staleness of data that could be
156+
// returned when the table (or stale MV) is queried.
157+
MaxStaleness *IntervalValue
158+
155159
// The tags associated with this table. Tag
156160
// keys are globally unique. See additional information on tags
157161
// (https://cloud.google.com/iam/docs/tags-access-control#definitions).
@@ -333,6 +337,8 @@ type MaterializedViewDefinition struct {
333337

334338
// MaxStaleness of data that could be returned when materialized
335339
// view is queried.
340+
//
341+
// Deprecated: use Table level MaxStaleness.
336342
MaxStaleness *IntervalValue
337343
}
338344

@@ -817,6 +823,9 @@ func (tm *TableMetadata) toBQ() (*bq.Table, error) {
817823
}
818824
}
819825
}
826+
if tm.MaxStaleness != nil {
827+
t.MaxStaleness = tm.MaxStaleness.String()
828+
}
820829
if tm.ResourceTags != nil {
821830
t.ResourceTags = make(map[string]string)
822831
for k, v := range tm.ResourceTags {
@@ -942,6 +951,9 @@ func bqToTableMetadata(t *bq.Table, c *Client) (*TableMetadata, error) {
942951
ForeignKeys: bqToForeignKeys(t.TableConstraints, c),
943952
}
944953
}
954+
if t.MaxStaleness != "" {
955+
md.MaxStaleness, _ = ParseInterval(t.MaxStaleness)
956+
}
945957
if t.ResourceTags != nil {
946958
md.ResourceTags = make(map[string]string)
947959
for k, v := range t.ResourceTags {
@@ -1122,6 +1134,10 @@ func (tm *TableMetadataToUpdate) toBQ() (*bq.Table, error) {
11221134
t.TableConstraints.ForceSendFields = append(t.TableConstraints.ForceSendFields, "ForeignKeys")
11231135
}
11241136
}
1137+
if tm.MaxStaleness != nil {
1138+
t.MaxStaleness = tm.MaxStaleness.String()
1139+
forceSend("MaxStaleness")
1140+
}
11251141
if tm.ResourceTags != nil {
11261142
t.ResourceTags = make(map[string]string)
11271143
for k, v := range tm.ResourceTags {
@@ -1210,6 +1226,10 @@ type TableMetadataToUpdate struct {
12101226
// such as primary and foreign keys.
12111227
TableConstraints *TableConstraints
12121228

1229+
// MaxStaleness staleness of data that could be
1230+
// returned when the table (or stale MV) is queried.
1231+
MaxStaleness *IntervalValue
1232+
12131233
// The tags associated with this table. Tag
12141234
// keys are globally unique. See additional information on tags
12151235
// (https://cloud.google.com/iam/docs/tags-access-control#definitions).

bigquery/table_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestBQToTableMetadata(t *testing.T) {
2929
aTimeMillis := aTime.UnixNano() / 1e6
3030
aDurationMillis := int64(1800000)
3131
aDuration := time.Duration(aDurationMillis) * time.Millisecond
32-
aStalenessValue, _ := ParseInterval("8:0:0")
32+
aStalenessValue, _ := ParseInterval("0-0 0 8:0:0")
3333
for _, test := range []struct {
3434
in *bq.Table
3535
want *TableMetadata
@@ -53,13 +53,13 @@ func TestBQToTableMetadata(t *testing.T) {
5353
EstimatedRows: 3,
5454
OldestEntryTime: uint64(aTimeMillis),
5555
},
56+
MaxStaleness: "0-0 0 8:0:0",
5657
MaterializedView: &bq.MaterializedViewDefinition{
5758
EnableRefresh: true,
5859
Query: "mat view query",
5960
LastRefreshTime: aTimeMillis,
6061
RefreshIntervalMs: aDurationMillis,
6162
AllowNonIncrementalDefinition: true,
62-
MaxStaleness: "8:0:0",
6363
},
6464
TimePartitioning: &bq.TimePartitioning{
6565
ExpirationMs: 7890,
@@ -118,13 +118,13 @@ func TestBQToTableMetadata(t *testing.T) {
118118
NumBytes: 123,
119119
NumLongTermBytes: 23,
120120
NumRows: 7,
121+
MaxStaleness: aStalenessValue,
121122
MaterializedView: &MaterializedViewDefinition{
122123
EnableRefresh: true,
123124
Query: "mat view query",
124125
LastRefreshTime: aTime,
125126
RefreshInterval: aDuration,
126127
AllowNonIncrementalDefinition: true,
127-
MaxStaleness: aStalenessValue,
128128
},
129129
TimePartitioning: &TimePartitioning{
130130
Type: DayPartitioningType,

0 commit comments

Comments
 (0)