Skip to content

Commit

Permalink
fix(bigquery): move MaxStaleness field to table level (#10066)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarowolfx authored Feb 3, 2025
1 parent 9cde8ab commit 164492d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
13 changes: 12 additions & 1 deletion bigquery/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3336,11 +3336,12 @@ func TestIntegration_MaterializedViewLifecycle(t *testing.T) {
`, qualified)

// Create materialized view

wantRefresh := 6 * time.Hour
maxStaleness := IntervalValueFromDuration(30 * time.Minute)
matViewID := tableIDs.New()
view := dataset.Table(matViewID)
if err := view.Create(ctx, &TableMetadata{
MaxStaleness: maxStaleness,
MaterializedView: &MaterializedViewDefinition{
Query: sql,
RefreshInterval: wantRefresh,
Expand All @@ -3366,6 +3367,10 @@ func TestIntegration_MaterializedViewLifecycle(t *testing.T) {
t.Errorf("mismatch on refresh time: got %d usec want %d usec", 1000*curMeta.MaterializedView.RefreshInterval.Nanoseconds(), 1000*wantRefresh.Nanoseconds())
}

if curMeta.MaxStaleness.String() != maxStaleness.String() {
t.Errorf("mismatch on max staleness: got %s want %s", curMeta.MaxStaleness, maxStaleness)
}

// MaterializedView is a TableType constant
want := MaterializedView
if curMeta.Type != want {
Expand All @@ -3374,7 +3379,9 @@ func TestIntegration_MaterializedViewLifecycle(t *testing.T) {

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

if newMeta.MaxStaleness.String() != maxStaleness.String() {
t.Errorf("mismatch on max staleness: got %s want %s", newMeta.MaxStaleness, maxStaleness)
}

// verify implicit setting of false due to partial population of update.
if newMeta.MaterializedView.EnableRefresh {
t.Error("expected EnableRefresh to be false, is true")
Expand Down
20 changes: 20 additions & 0 deletions bigquery/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ type TableMetadata struct {
// Present only if the table has primary or foreign keys.
TableConstraints *TableConstraints

// MaxStaleness staleness of data that could be
// returned when the table (or stale MV) is queried.
MaxStaleness *IntervalValue

// The tags associated with this table. Tag
// keys are globally unique. See additional information on tags
// (https://cloud.google.com/iam/docs/tags-access-control#definitions).
Expand Down Expand Up @@ -333,6 +337,8 @@ type MaterializedViewDefinition struct {

// MaxStaleness of data that could be returned when materialized
// view is queried.
//
// Deprecated: use Table level MaxStaleness.
MaxStaleness *IntervalValue
}

Expand Down Expand Up @@ -817,6 +823,9 @@ func (tm *TableMetadata) toBQ() (*bq.Table, error) {
}
}
}
if tm.MaxStaleness != nil {
t.MaxStaleness = tm.MaxStaleness.String()
}
if tm.ResourceTags != nil {
t.ResourceTags = make(map[string]string)
for k, v := range tm.ResourceTags {
Expand Down Expand Up @@ -942,6 +951,9 @@ func bqToTableMetadata(t *bq.Table, c *Client) (*TableMetadata, error) {
ForeignKeys: bqToForeignKeys(t.TableConstraints, c),
}
}
if t.MaxStaleness != "" {
md.MaxStaleness, _ = ParseInterval(t.MaxStaleness)
}
if t.ResourceTags != nil {
md.ResourceTags = make(map[string]string)
for k, v := range t.ResourceTags {
Expand Down Expand Up @@ -1122,6 +1134,10 @@ func (tm *TableMetadataToUpdate) toBQ() (*bq.Table, error) {
t.TableConstraints.ForceSendFields = append(t.TableConstraints.ForceSendFields, "ForeignKeys")
}
}
if tm.MaxStaleness != nil {
t.MaxStaleness = tm.MaxStaleness.String()
forceSend("MaxStaleness")
}
if tm.ResourceTags != nil {
t.ResourceTags = make(map[string]string)
for k, v := range tm.ResourceTags {
Expand Down Expand Up @@ -1210,6 +1226,10 @@ type TableMetadataToUpdate struct {
// such as primary and foreign keys.
TableConstraints *TableConstraints

// MaxStaleness staleness of data that could be
// returned when the table (or stale MV) is queried.
MaxStaleness *IntervalValue

// The tags associated with this table. Tag
// keys are globally unique. See additional information on tags
// (https://cloud.google.com/iam/docs/tags-access-control#definitions).
Expand Down
6 changes: 3 additions & 3 deletions bigquery/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestBQToTableMetadata(t *testing.T) {
aTimeMillis := aTime.UnixNano() / 1e6
aDurationMillis := int64(1800000)
aDuration := time.Duration(aDurationMillis) * time.Millisecond
aStalenessValue, _ := ParseInterval("8:0:0")
aStalenessValue, _ := ParseInterval("0-0 0 8:0:0")
for _, test := range []struct {
in *bq.Table
want *TableMetadata
Expand All @@ -53,13 +53,13 @@ func TestBQToTableMetadata(t *testing.T) {
EstimatedRows: 3,
OldestEntryTime: uint64(aTimeMillis),
},
MaxStaleness: "0-0 0 8:0:0",
MaterializedView: &bq.MaterializedViewDefinition{
EnableRefresh: true,
Query: "mat view query",
LastRefreshTime: aTimeMillis,
RefreshIntervalMs: aDurationMillis,
AllowNonIncrementalDefinition: true,
MaxStaleness: "8:0:0",
},
TimePartitioning: &bq.TimePartitioning{
ExpirationMs: 7890,
Expand Down Expand Up @@ -118,13 +118,13 @@ func TestBQToTableMetadata(t *testing.T) {
NumBytes: 123,
NumLongTermBytes: 23,
NumRows: 7,
MaxStaleness: aStalenessValue,
MaterializedView: &MaterializedViewDefinition{
EnableRefresh: true,
Query: "mat view query",
LastRefreshTime: aTime,
RefreshInterval: aDuration,
AllowNonIncrementalDefinition: true,
MaxStaleness: aStalenessValue,
},
TimePartitioning: &TimePartitioning{
Type: DayPartitioningType,
Expand Down

0 comments on commit 164492d

Please sign in to comment.