Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time partitioning field to google_bigquery_table resource #1240

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions google/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ func resourceBigQueryTable() *schema.Resource {
Required: true,
ValidateFunc: validation.StringInSlice([]string{"DAY"}, false),
},

// Type: [Optional] The field used to determine how to create a time-based
// partition. If time-based partitioning is enabled without this value, the
// table is partitioned based on the load time.
"field": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
Expand Down Expand Up @@ -419,6 +428,10 @@ func expandTimePartitioning(configured interface{}) *bigquery.TimePartitioning {
raw := configured.([]interface{})[0].(map[string]interface{})
tp := &bigquery.TimePartitioning{Type: raw["type"].(string)}

if v, ok := raw["field"]; ok {
tp.Field = v.(string)
}

if v, ok := raw["expiration_ms"]; ok {
tp.ExpirationMs = int64(v.(int))
}
Expand All @@ -429,6 +442,10 @@ func expandTimePartitioning(configured interface{}) *bigquery.TimePartitioning {
func flattenTimePartitioning(tp *bigquery.TimePartitioning) []map[string]interface{} {
result := map[string]interface{}{"type": tp.Type}

if tp.Field != "" {
result["field"] = tp.Field
}

if tp.ExpirationMs != 0 {
result["expiration_ms"] = tp.ExpirationMs
}
Expand Down
18 changes: 14 additions & 4 deletions google/resource_bigquery_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func TestAccBigQueryTable_Basic(t *testing.T) {
t.Parallel()

resourceName := "google_bigquery_table.test"
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(10))

Expand All @@ -24,18 +25,22 @@ func TestAccBigQueryTable_Basic(t *testing.T) {
{
Config: testAccBigQueryTable(datasetID, tableID),
Check: resource.ComposeTestCheckFunc(
testAccBigQueryTableExists(
"google_bigquery_table.test"),
testAccBigQueryTableExists(resourceName),
),
},

{
Config: testAccBigQueryTableUpdated(datasetID, tableID),
Check: resource.ComposeTestCheckFunc(
testAccBigQueryTableExists(
"google_bigquery_table.test"),
testAccBigQueryTableExists(resourceName),
),
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -197,10 +202,15 @@ resource "google_bigquery_table" "test" {

time_partitioning {
type = "DAY"
field = "ts"
}

schema = <<EOH
[
{
"name": "ts",
"type": "TIMESTAMP"
},
{
"name": "city",
"type": "RECORD",
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/bigquery_table.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ The `time_partitioning` block supports:
* `expiration_ms` - (Optional) Number of milliseconds for which to keep the
storage for a partition.

* `field` - (Optional) The field used to determine how to create a time-based
partition. If time-based partitioning is enabled without this value, the
table is partitioned based on the load time.

* `type` - (Required) The only type supported is DAY, which will generate
one partition per day based on data loading time.

Expand Down