Skip to content

Commit

Permalink
feat(bigquery): add option to use Non-incremental materialized views
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinpro committed Aug 21, 2023
1 parent 3997459 commit 71e6162
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,13 @@ func ResourceBigQueryTable() *schema.Resource {
Description: `Specifies maximum frequency at which this materialized view will be refreshed. The default is 1800000`,
},

"allow_non_incremental_definition": {
Type: schema.TypeBool,
Default: false,
Optional: true,
Description: `Allow non incremental materialized view definition. The default value is false.`,
},

// Query: [Required] A query whose result is persisted
"query": {
Type: schema.TypeString,
Expand Down Expand Up @@ -1982,13 +1989,19 @@ func expandMaterializedView(configured interface{}) *bigquery.MaterializedViewDe
mvd.ForceSendFields = append(mvd.ForceSendFields, "RefreshIntervalMs")
}

if v, ok := raw["allow_non_incremental_definition"]; ok {
mvd.AllowNonIncrementalDefinition = v.(bool)
mvd.ForceSendFields = append(mvd.ForceSendFields, "AllowNonIncrementalDefinition")
}

return mvd
}

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

return []map[string]interface{}{result}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,39 @@ func TestAccBigQueryTable_MaterializedView_DailyTimePartioning_Update(t *testing
})
}

func TestAccBigQueryTable_MaterializedView_NonIncremental_basic(t *testing.T) {
t.Parallel()

datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
materialized_viewID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
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)
maxStaleness := "0-0 0 10:0:0"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableWithMatViewNonIncremental_basic(datasetID, tableID, materialized_viewID, query, maxStaleness),
},
{
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection"},
},
{
ResourceName: "google_bigquery_table.mv_test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection"},
},
},
})
}

func TestAccBigQueryExternalDataTable_parquet(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1838,6 +1871,86 @@ resource "google_bigquery_table" "mv_test" {
`, datasetID, tableID, mViewID, enable_refresh, refresh_interval, query)
}

func testAccBigQueryTableWithMatViewNonIncremental_basic(datasetID, tableID, mViewID, query, maxStaleness string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
dataset_id = "%s"
}
resource "google_bigquery_table" "test" {
deletion_protection = false
table_id = "%s"
dataset_id = google_bigquery_dataset.test.dataset_id
time_partitioning {
type = "DAY"
field = "ts"
require_partition_filter = true
}
clustering = ["some_int", "some_string"]
schema = <<EOH
[
{
"name": "ts",
"type": "TIMESTAMP"
},
{
"name": "some_string",
"type": "STRING"
},
{
"name": "some_int",
"type": "INTEGER"
},
{
"name": "city",
"type": "RECORD",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "coord",
"type": "RECORD",
"fields": [
{
"name": "lon",
"type": "FLOAT"
}
]
}
]
}
]
EOH
}
resource "google_bigquery_table" "mv_test" {
deletion_protection = false
table_id = "%s"
dataset_id = google_bigquery_dataset.test.dataset_id
time_partitioning {
type = "DAY"
field = "ts"
}
materialized_view {
query = "%s"
allow_non_incremental_definition = true
}
depends_on = [
google_bigquery_table.test,
]
max_staleness = "%s"
}
`, datasetID, tableID, mViewID, query, maxStaleness)
}

func testAccBigQueryTableUpdated(datasetID, tableID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
Expand Down

0 comments on commit 71e6162

Please sign in to comment.