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 allow_non_incremental_definition #1417

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20241119-164147.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Support non-incremental materialized view
time: 2024-11-19T16:41:47.458086+09:00
custom:
Author: yu-iskw
Issue: "672"
29 changes: 28 additions & 1 deletion dbt/adapters/bigquery/relation_configs/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BigQueryOptionsConfig(BigQueryBaseRelationConfig):
refresh_interval_minutes: Optional[float] = 30
expiration_timestamp: Optional[datetime] = None
max_staleness: Optional[str] = None
allow_non_incremental_definition: Optional[bool] = False
kms_key_name: Optional[str] = None
description: Optional[str] = None
labels: Optional[Dict[str, str]] = None
Expand Down Expand Up @@ -58,6 +59,7 @@ def array(x):
"refresh_interval_minutes": numeric,
"expiration_timestamp": interval,
"max_staleness": interval,
"allow_non_incremental_definition": boolean,
"kms_key_name": string,
"description": escaped_string,
"labels": array,
Expand All @@ -78,13 +80,30 @@ def formatted_option(name: str) -> Optional[Any]:

return options

def as_alter_dict(self) -> Dict[str, Any]:
"""
Return a dict of options that can be used in an ALTER MATERIALIZED VIEW statement.

According to our research, some options are not available for ALTER statements,
even though the documentation implies that they are. For instance,
`allow_non_incremental_definition` is not available for ALTER as of writing.
If we want to change the `allow_non_incremental_definition` option, we need to refresh
the dbt Model.

SEE https://cloud.google.com/bigquery/docs/materialized-views-manage
"""
non_available_options = ["allow_non_incremental_definition"]
options = {k: v for k, v in self.as_ddl_dict().items() if k not in non_available_options}
return options

@classmethod
def from_dict(cls, config_dict: Dict[str, Any]) -> Self:
setting_formatters = {
"enable_refresh": bool_setting,
"refresh_interval_minutes": float_setting,
"expiration_timestamp": None,
"max_staleness": None,
"allow_non_incremental_definition": None,
"kms_key_name": None,
"description": None,
"labels": None,
Expand All @@ -101,7 +120,13 @@ def formatted_setting(name: str) -> Any:
# avoid picking up defaults on dependent options
# e.g. don't set `refresh_interval_minutes` = 30 when the user has `enable_refresh` = False
if kwargs_dict["enable_refresh"] is False:
kwargs_dict.update({"refresh_interval_minutes": None, "max_staleness": None})
kwargs_dict.update(
{
"refresh_interval_minutes": None,
"max_staleness": None,
"allow_non_incremental_definition": None,
}
)

options: Self = super().from_dict(kwargs_dict)
return options
Expand All @@ -115,6 +140,7 @@ def parse_relation_config(cls, relation_config: RelationConfig) -> Dict[str, Any
"refresh_interval_minutes",
"expiration_timestamp",
"max_staleness",
"allow_non_incremental_definition",
"kms_key_name",
"description",
"labels",
Expand All @@ -138,6 +164,7 @@ def parse_bq_table(cls, table: BigQueryTable) -> Dict[str, Any]:
"refresh_interval_minutes": table.mview_refresh_interval.seconds / 60,
"expiration_timestamp": table.expires,
"max_staleness": None,
"allow_non_incremental_definition": None,
"description": table.description,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% else %}

alter materialized view {{ relation }}
set {{ bigquery_options(configuration_changes.options.context.as_ddl_dict()) }}
set {{ bigquery_options(configuration_changes.options.context.as_alter_dict()) }}

{%- endif %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
cluster_by=["id", "value"],
enable_refresh=True,
refresh_interval_minutes=60,
max_staleness="INTERVAL 45 MINUTE"
max_staleness="INTERVAL 45 MINUTE",
allow_non_incremental_definition=True
) }}
select
id,
Expand Down