Skip to content

Commit

Permalink
feat(bigquery): Add support for AllowNonIncrementalDefinition and Sta…
Browse files Browse the repository at this point in the history
…leness on MaterializedView (#8673)
  • Loading branch information
alvarowolfx committed Jan 16, 2024
1 parent 0500c7a commit 6ec2bb2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
38 changes: 29 additions & 9 deletions bigquery/table.go
Expand Up @@ -326,31 +326,51 @@ type MaterializedViewDefinition struct {
// RefreshInterval defines the maximum frequency, in millisecond precision,
// at which this this materialized view will be refreshed.
RefreshInterval time.Duration

// AllowNonIncrementalDefinition for materialized view definition.
// The default value is false.
AllowNonIncrementalDefinition bool

// MaxStaleness of data that could be returned when materialized
// view is queried.
MaxStaleness *IntervalValue
}

func (mvd *MaterializedViewDefinition) toBQ() *bq.MaterializedViewDefinition {
if mvd == nil {
return nil
}
maxStaleness := ""
if mvd.MaxStaleness != nil {
maxStaleness = mvd.MaxStaleness.String()
}
return &bq.MaterializedViewDefinition{
EnableRefresh: mvd.EnableRefresh,
Query: mvd.Query,
LastRefreshTime: mvd.LastRefreshTime.UnixNano() / 1e6,
RefreshIntervalMs: int64(mvd.RefreshInterval) / 1e6,
EnableRefresh: mvd.EnableRefresh,
Query: mvd.Query,
LastRefreshTime: mvd.LastRefreshTime.UnixNano() / 1e6,
RefreshIntervalMs: int64(mvd.RefreshInterval) / 1e6,
AllowNonIncrementalDefinition: mvd.AllowNonIncrementalDefinition,
MaxStaleness: maxStaleness,
// force sending the bool in all cases due to how Go handles false.
ForceSendFields: []string{"EnableRefresh"},
ForceSendFields: []string{"EnableRefresh", "AllowNonIncrementalDefinition"},
}
}

func bqToMaterializedViewDefinition(q *bq.MaterializedViewDefinition) *MaterializedViewDefinition {
if q == nil {
return nil
}
var maxStaleness *IntervalValue
if q.MaxStaleness != "" {
maxStaleness, _ = ParseInterval(q.MaxStaleness)
}
return &MaterializedViewDefinition{
EnableRefresh: q.EnableRefresh,
Query: q.Query,
LastRefreshTime: unixMillisToTime(q.LastRefreshTime),
RefreshInterval: time.Duration(q.RefreshIntervalMs) * time.Millisecond,
EnableRefresh: q.EnableRefresh,
Query: q.Query,
LastRefreshTime: unixMillisToTime(q.LastRefreshTime),
RefreshInterval: time.Duration(q.RefreshIntervalMs) * time.Millisecond,
AllowNonIncrementalDefinition: q.AllowNonIncrementalDefinition,
MaxStaleness: maxStaleness,
}
}

Expand Down
21 changes: 13 additions & 8 deletions bigquery/table_test.go
Expand Up @@ -29,6 +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")
for _, test := range []struct {
in *bq.Table
want *TableMetadata
Expand All @@ -53,10 +54,12 @@ func TestBQToTableMetadata(t *testing.T) {
OldestEntryTime: uint64(aTimeMillis),
},
MaterializedView: &bq.MaterializedViewDefinition{
EnableRefresh: true,
Query: "mat view query",
LastRefreshTime: aTimeMillis,
RefreshIntervalMs: aDurationMillis,
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 @@ -116,10 +119,12 @@ func TestBQToTableMetadata(t *testing.T) {
NumLongTermBytes: 23,
NumRows: 7,
MaterializedView: &MaterializedViewDefinition{
EnableRefresh: true,
Query: "mat view query",
LastRefreshTime: aTime,
RefreshInterval: aDuration,
EnableRefresh: true,
Query: "mat view query",
LastRefreshTime: aTime,
RefreshInterval: aDuration,
AllowNonIncrementalDefinition: true,
MaxStaleness: aStalenessValue,
},
TimePartitioning: &TimePartitioning{
Type: DayPartitioningType,
Expand Down

0 comments on commit 6ec2bb2

Please sign in to comment.