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 support for range partitioning in BigQuery #2890

Merged
merged 3 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
121 changes: 121 additions & 0 deletions third_party/terraform/resources/resource_bigquery_table.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package google

import (
"encoding/json"
"errors"
"fmt"
"log"
"regexp"
Expand Down Expand Up @@ -302,6 +303,55 @@ func resourceBigQueryTable() *schema.Resource {
},
},

tristan957 marked this conversation as resolved.
Show resolved Hide resolved
<% unless version == 'ga' -%>
// RangePartitioning: [Optional] If specified, configures range-based
// partitioning for this table.
"range_partitioning": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Field: [Required] The field used to determine how to create a range-based
// partition.
"field": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

// Range: [Required] Information required to partition based on ranges.
"range": &schema.Schema{
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Start: [Required] Start of the range partitioning, inclusive.
"start": {
Type: schema.TypeInt,
Required: true,
},

// End: [Required] End of the range partitioning, exclusive.
"end": {
Type: schema.TypeInt,
Required: true,
},

// Interval: [Required] The width of each range within the partition.
"interval": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
},
},
<% end -%>

tristan957 marked this conversation as resolved.
Show resolved Hide resolved
// Clustering: [Optional] Specifies column names to use for data clustering. Up to four
// top-level columns are allowed, and should be specified in descending priority order.
"clustering": &schema.Schema{
Expand Down Expand Up @@ -464,6 +514,17 @@ func resourceTable(d *schema.ResourceData, meta interface{}) (*bigquery.Table, e
table.TimePartitioning = expandTimePartitioning(v)
}

<% unless version == 'ga' -%>
if v, ok := d.GetOk("range_partitioning"); ok {
tristan957 marked this conversation as resolved.
Show resolved Hide resolved
rangePartitioning, err := expandRangePartitioning(v)
if err != nil {
return nil, err
}

table.RangePartitioning = rangePartitioning
}
<% end -%>

if v, ok := d.GetOk("clustering"); ok {
table.Clustering = &bigquery.Clustering{
Fields: convertStringArr(v.([]interface{})),
Expand Down Expand Up @@ -553,6 +614,14 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {
}
}

<% unless version == 'ga' -%>
if res.RangePartitioning != nil {
if err := d.Set("range_partitioning", flattenRangePartitioning(res.RangePartitioning)); err != nil {
return err
}
}
<% end -%>

if res.Clustering != nil {
d.Set("clustering", res.Clustering.Fields)
}
Expand Down Expand Up @@ -838,6 +907,41 @@ func expandTimePartitioning(configured interface{}) *bigquery.TimePartitioning {
return tp
}

<% unless version == 'ga' -%>
func expandRangePartitioning(configured interface{}) (*bigquery.RangePartitioning, error) {
tristan957 marked this conversation as resolved.
Show resolved Hide resolved
if configured == nil {
return nil, nil
}

rpJson := configured.([]interface{})
if len(rpJson) == 0 || rpJson[0] == nil {
return nil, errors.New("Error casting range partitioning interface to expected structure")
}

rpRaw := rpJson[0].([]map[string]interface{})
tristan957 marked this conversation as resolved.
Show resolved Hide resolved
rprRaw := rpRaw["range"].(map[string]interface{})
rp := &bigquery.RangePartitioning{
Field: rpRaw["field"].(string),
}

if v, ok := rpRaw["range"]; ok && v != nil {
rangeLs := v.([]interface{})
if len(rangeLs) != 1 || rangeLs[0] == nil {
return nil, errors.New("Non-empty range must be given for range partitioning")
}

rangeJson := rangeLs[0].(map[string]interface{})
rp.Range = &bigquery.RangePartitioningRange{
Start: int64(rangeJson["start"].(int)),
End: int64(rangeJson["end"].(int)),
Interval: int64(rangeJson["interval"].(int)),
}
}

return rp, nil
}
<% end -%>

func flattenEncryptionConfiguration(ec *bigquery.EncryptionConfiguration) []map[string]interface{} {
return []map[string]interface{}{{"kms_key_name": ec.KmsKeyName}}
}
Expand All @@ -860,6 +964,23 @@ func flattenTimePartitioning(tp *bigquery.TimePartitioning) []map[string]interfa
return []map[string]interface{}{result}
}

<% unless version == 'ga' -%>
func flattenRangePartitioning(rp *bigquery.RangePartitioning) []map[string]interface{} {
result := map[string]interface{}{
"field": rp.Field,
"range": []map[string]interface{}{
{
"start": rp.Range.Start,
"end": rp.Range.End,
"interval": rp.Range.Interval,
},
},
}

return []map[string]interface{}{result}
}
<% end -%>

func expandView(configured interface{}) *bigquery.ViewDefinition {
raw := configured.([]interface{})[0].(map[string]interface{})
vd := &bigquery.ViewDefinition{Query: raw["query"].(string)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<% autogen_exception -%>
package google

import (
Expand Down Expand Up @@ -65,6 +66,31 @@ func TestAccBigQueryTable_Kms(t *testing.T) {
})
}

<% unless version == 'ga' -%>
func TestAccBigQueryTable_RangePartitioning(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))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBigQueryTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableRangePartitioning(datasetID, tableID),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
<% end -%>

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

Expand Down Expand Up @@ -267,7 +293,7 @@ resource "google_bigquery_table" "test" {

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

encryption_configuration {
Expand Down Expand Up @@ -306,6 +332,44 @@ EOH
`, datasetID, cryptoKeyName, tableID)
}

<% unless version == 'ga' -%>
func testAccBigQueryTableRangePartitioning(datasetID, tableID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
dataset_id = "%s"
}

resource "google_bigquery_table" "test" {
provider = google-beta
tristan957 marked this conversation as resolved.
Show resolved Hide resolved
table_id = "%s"
dataset_id = google_bigquery_dataset.test.dataset_id

range_partitioning {
field = "id"
range {
start = 1
end = 10000
interval = 100
}
}

schema = <<EOH
[
{
"name": "ts",
"type": "TIMESTAMP"
},
{
"name": "id",
tristan957 marked this conversation as resolved.
Show resolved Hide resolved
"type": "INTEGER"
}
]
EOH
}
`, datasetID, tableID)
}
<% end -%>

func testAccBigQueryTableWithView(datasetID, tableID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ The following arguments are supported:
* `time_partitioning` - (Optional) If specified, configures time-based
partitioning for this table. Structure is documented below.

* `range_partitioning` - (Optional, Beta) If specified, configures range-based
partitioning for this table. Structure is documented below.

* `clustering` - (Optional) Specifies column names to use for data clustering.
Up to four top-level columns are allowed, and should be specified in
descending priority order.
Expand Down Expand Up @@ -176,7 +179,7 @@ The `csv_options` block supports:
characters, you must also set the `allow_quoted_newlines` property to true.
The API-side default is `"`, specified in Terraform escaped as `\"`. Due to
limitations with Terraform default values, this value is required to be
explicitly set.
explicitly set.

* `allow_jagged_rows` (Optional) - Indicates if BigQuery should accept rows
that are missing trailing optional columns.
Expand Down Expand Up @@ -220,6 +223,22 @@ The `time_partitioning` block supports:
require a partition filter that can be used for partition elimination to be
specified.

The `range_partitioning` block supports:

* `field` - (Required) The field used to determine how to create a range-based
partition.

* `range` - (Required) Information required to partition based on ranges.
Structure is documented below.

The `range` block supports:

* `start` - (Required) Start of the range partitioning, inclusive.

* `end` - (Required) End of the range partitioning, exclusive.

* `interval` - (Required) The width of each range within the partition.

The `view` block supports:

* `query` - (Required) A query that BigQuery executes when the view is referenced.
Expand Down