Skip to content

Commit

Permalink
Add allow_non_incremental_definition
Browse files Browse the repository at this point in the history
Signed-off-by: Yu Ishikawa <[email protected]>
  • Loading branch information
yu-iskw committed Nov 29, 2024
1 parent 5501cd3 commit e52e651
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
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"
33 changes: 32 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,34 @@ 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 +124,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) # type: ignore
return options
Expand All @@ -115,6 +144,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 @@ -140,6 +170,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
3 changes: 2 additions & 1 deletion tests/functional/adapter/materialized_view_tests/_files.py
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

0 comments on commit e52e651

Please sign in to comment.