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 materialized view configuration for BigQuery #4578

Merged
merged 12 commits into from
Dec 13, 2023
99 changes: 99 additions & 0 deletions website/docs/reference/resource-configs/bigquery-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,102 @@ Views with this configuration will be able to select from objects in `project_1.

The `grant_access_to` config is not thread-safe when multiple views need to be authorized for the same dataset. The initial `dbt run` operation after a new `grant_access_to` config is added should therefore be executed in a single thread. Subsequent runs using the same configuration will not attempt to re-apply existing access grants, and can make use of multiple threads.

<VersionBlock firstVersion="1.7">

## Materialized views

The BigQuery adapter supports [materialized views](https://cloud.google.com/bigquery/docs/materialized-views-intro)
with the following configuration parameters:

| Parameter | Type | Required | Default | Change Monitoring Support | Reference |
|------------------------------|----------------------|----------|-----------|---------------------------|--------------------------------------------------|
| `on_configuration_change` | STRING | NO | `'apply'` | N/A | |
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
| `cluster_by` | LIST[STRING] | NO | | DROP/CREATE | [Clustering](#clustering-clause) |
| `partition_by` | DICT | NO | | DROP/CREATE | [Partitioning](#partition-clause) |
| `enable_refresh` | BOOLEAN | NO | `True` | ALTER | [Auto-refresh](#auto-refresh) |
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
| `refresh_interval_minutes` | FLOAT | NO | `30` | ALTER | [Auto-refresh](#auto-refresh) |
| `max_staleness` (in Preview) | INTERVAL | NO | | ALTER | [Auto-refresh](#auto-refresh) |
| `description` | STRING | NO | | ALTER | |
| `labels` | DICT[STRING, STRING] | NO | | ALTER | [Labels](#specifying-labels) |
| `hours_to_expiration` | INTEGER | NO | | ALTER | [Table expiration](controlling-table-expiration) |
| `kms_key_name` | STRING | NO | | ALTER | [KMS encryption](#using-kms-encryption) |
mikealfare marked this conversation as resolved.
Show resolved Hide resolved

#### Sample model file:

<File name='bigquery_materialized_view.sql'>

```sql
{{ config(
materialized='materialized_view',
on_configuration_change='{ apply | continue | fail }'
cluster_by=['<field_name>', ...],
partition_by={
'field': '<field_name>',
'data_type': '<timestamp | date | datetime | int64>',

# only if `data_type` is not 'int64'
'granularity': '<hour | day | month | year>'

# only if `data_type` is 'int64'
'range': {
'start': <int>,
'end': <int>,
'interval': <int>,
}
},

# auto-refresh options
enable_refresh=<bool>,
refresh_interval_minutes=<float>,
max_staleness='<interval>',

# additional options
description='<long description>',
labels={'<label_name>': '<label_value>', ...},
hours_to_expiration=<int>,
kms_key_name='<path_to_key>',
) }}

select * from {{ ref('my_base_table') }}
```

</File>

Many of these parameters correspond to their table counterparts and have been linked above.
The set of parameters which are unique to materialized views covers auto-refresh functionality, which is covered [below](#auto-refresh).
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved

Find more information about these parameters in the BigQuery docs:
- [CREATE MATERIALIZED VIEW statement](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_materialized_view_statement)
- [materialized_view_option_list](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#materialized_view_option_list)

### Auto-refresh

| Parameter | Type | Required | Default | Change Monitoring Support |
|-------------------------------|----------|----------|---------|---------------------------|
| `enable_refresh` | BOOLEAN | NO | `True` | ALTER |
| `refresh_interval_minutes` | FLOAT | NO | `30` | ALTER |
| `max_staleness` (in Preview) | INTERVAL | NO | | ALTER |

BigQuery supports [automatic refresh](https://cloud.google.com/bigquery/docs/materialized-views-manage#automatic_refresh) configuration for materialized views.
By default, a materialized view will automatically refresh within 5 minutes of changes in the base table, but no more frequently than every 30 minutes.
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
BigQuery only officially supports configuration of the frequency (the "30 minutes" part);
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
however, there is a feature in preview that allows for configuration of the staleness (the "5 minutes" part).
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
dbt will monitor these parameters for changes and apply them using an `ALTER` statement.

Find more information about these parameters in the BigQuery docs:
- [materialized_view_option_list](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#materialized_view_option_list)
- [max_staleness](https://cloud.google.com/bigquery/docs/materialized-views-create#max_staleness)

### Limitations

As with most data platforms, there are limitations associated with materialized views. Some worth noting include:

- Materialized view SQL has a [limited feature set](https://cloud.google.com/bigquery/docs/materialized-views-create#supported-mvs)
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
- Materialized view SQL cannot be updated; the materialized view must go through a `--full-refresh` (DROP/CREATE)
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
- The `partition_by` clause on a materialized view must match that of the underlying base table
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
- While materialized views can have descriptions, materialized view *columns* cannot
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
- Recreating/dropping the base table requires recreating/dropping the materialized view
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved

Find more information about materialized view limitations in Google's BigQuery [docs](https://cloud.google.com/bigquery/docs/materialized-views-intro#limitations).

</VersionBlock>
Loading