Skip to content

Commit 76be55b

Browse files
feat(bigquery): add option to use Non-incremental materialized views (#8725) (#15813)
Signed-off-by: Modular Magician <[email protected]>
1 parent c596956 commit 76be55b

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

.changelog/8725.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
bigquery: added `allow_non_incremental_definition` to `google_bigquery_table` resource
3+
```

google/services/bigquery/resource_bigquery_table.go

+14
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,14 @@ func ResourceBigQueryTable() *schema.Resource {
839839
Description: `Specifies maximum frequency at which this materialized view will be refreshed. The default is 1800000`,
840840
},
841841

842+
"allow_non_incremental_definition": {
843+
Type: schema.TypeBool,
844+
Default: false,
845+
Optional: true,
846+
ForceNew: true,
847+
Description: `Allow non incremental materialized view definition. The default value is false.`,
848+
},
849+
842850
// Query: [Required] A query whose result is persisted
843851
"query": {
844852
Type: schema.TypeString,
@@ -1984,13 +1992,19 @@ func expandMaterializedView(configured interface{}) *bigquery.MaterializedViewDe
19841992
mvd.ForceSendFields = append(mvd.ForceSendFields, "RefreshIntervalMs")
19851993
}
19861994

1995+
if v, ok := raw["allow_non_incremental_definition"]; ok {
1996+
mvd.AllowNonIncrementalDefinition = v.(bool)
1997+
mvd.ForceSendFields = append(mvd.ForceSendFields, "AllowNonIncrementalDefinition")
1998+
}
1999+
19872000
return mvd
19882001
}
19892002

19902003
func flattenMaterializedView(mvd *bigquery.MaterializedViewDefinition) []map[string]interface{} {
19912004
result := map[string]interface{}{"query": mvd.Query}
19922005
result["enable_refresh"] = mvd.EnableRefresh
19932006
result["refresh_interval_ms"] = mvd.RefreshIntervalMs
2007+
result["allow_non_incremental_definition"] = mvd.AllowNonIncrementalDefinition
19942008

19952009
return []map[string]interface{}{result}
19962010
}

google/services/bigquery/resource_bigquery_table_test.go

+113
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,39 @@ func TestAccBigQueryTable_MaterializedView_DailyTimePartioning_Update(t *testing
489489
})
490490
}
491491

492+
func TestAccBigQueryTable_MaterializedView_NonIncremental_basic(t *testing.T) {
493+
t.Parallel()
494+
495+
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
496+
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
497+
materialized_viewID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
498+
query := fmt.Sprintf("SELECT count(some_string) as count, some_int, ts FROM `%s.%s` WHERE DATE(ts) = '2019-01-01' GROUP BY some_int, ts", datasetID, tableID)
499+
maxStaleness := "0-0 0 10:0:0"
500+
501+
acctest.VcrTest(t, resource.TestCase{
502+
PreCheck: func() { acctest.AccTestPreCheck(t) },
503+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
504+
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
505+
Steps: []resource.TestStep{
506+
{
507+
Config: testAccBigQueryTableWithMatViewNonIncremental_basic(datasetID, tableID, materialized_viewID, query, maxStaleness),
508+
},
509+
{
510+
ResourceName: "google_bigquery_table.test",
511+
ImportState: true,
512+
ImportStateVerify: true,
513+
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection"},
514+
},
515+
{
516+
ResourceName: "google_bigquery_table.mv_test",
517+
ImportState: true,
518+
ImportStateVerify: true,
519+
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection"},
520+
},
521+
},
522+
})
523+
}
524+
492525
func TestAccBigQueryExternalDataTable_parquet(t *testing.T) {
493526
t.Parallel()
494527

@@ -1840,6 +1873,86 @@ resource "google_bigquery_table" "mv_test" {
18401873
`, datasetID, tableID, mViewID, enable_refresh, refresh_interval, query)
18411874
}
18421875

1876+
func testAccBigQueryTableWithMatViewNonIncremental_basic(datasetID, tableID, mViewID, query, maxStaleness string) string {
1877+
return fmt.Sprintf(`
1878+
resource "google_bigquery_dataset" "test" {
1879+
dataset_id = "%s"
1880+
}
1881+
1882+
resource "google_bigquery_table" "test" {
1883+
deletion_protection = false
1884+
table_id = "%s"
1885+
dataset_id = google_bigquery_dataset.test.dataset_id
1886+
1887+
time_partitioning {
1888+
type = "DAY"
1889+
field = "ts"
1890+
require_partition_filter = true
1891+
}
1892+
clustering = ["some_int", "some_string"]
1893+
schema = <<EOH
1894+
[
1895+
{
1896+
"name": "ts",
1897+
"type": "TIMESTAMP"
1898+
},
1899+
{
1900+
"name": "some_string",
1901+
"type": "STRING"
1902+
},
1903+
{
1904+
"name": "some_int",
1905+
"type": "INTEGER"
1906+
},
1907+
{
1908+
"name": "city",
1909+
"type": "RECORD",
1910+
"fields": [
1911+
{
1912+
"name": "id",
1913+
"type": "INTEGER"
1914+
},
1915+
{
1916+
"name": "coord",
1917+
"type": "RECORD",
1918+
"fields": [
1919+
{
1920+
"name": "lon",
1921+
"type": "FLOAT"
1922+
}
1923+
]
1924+
}
1925+
]
1926+
}
1927+
]
1928+
EOH
1929+
1930+
}
1931+
1932+
resource "google_bigquery_table" "mv_test" {
1933+
deletion_protection = false
1934+
table_id = "%s"
1935+
dataset_id = google_bigquery_dataset.test.dataset_id
1936+
1937+
time_partitioning {
1938+
type = "DAY"
1939+
field = "ts"
1940+
}
1941+
1942+
materialized_view {
1943+
query = "%s"
1944+
allow_non_incremental_definition = true
1945+
}
1946+
1947+
depends_on = [
1948+
google_bigquery_table.test,
1949+
]
1950+
1951+
max_staleness = "%s"
1952+
}
1953+
`, datasetID, tableID, mViewID, query, maxStaleness)
1954+
}
1955+
18431956
func testAccBigQueryTableUpdated(datasetID, tableID string) string {
18441957
return fmt.Sprintf(`
18451958
resource "google_bigquery_dataset" "test" {

website/docs/r/bigquery_table.html.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ in Terraform state, a `terraform destroy` or `terraform apply` that would delete
355355
* `refresh_interval_ms` - (Optional) The maximum frequency at which this materialized view will be refreshed.
356356
The default value is 1800000
357357

358+
* `allow_non_incremental_definition` - (Optional) Allow non incremental materialized view definition.
359+
The default value is false.
360+
358361
<a name="nested_encryption_configuration"></a>The `encryption_configuration` block supports the following arguments:
359362

360363
* `kms_key_name` - (Required) The self link or full name of a key which should be used to

0 commit comments

Comments
 (0)