Skip to content

Commit 06b5aad

Browse files
Add support for clustering and automatic clustering in materialized views
1 parent 13c4318 commit 06b5aad

File tree

5 files changed

+61
-33
lines changed

5 files changed

+61
-33
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# dbt_dataengineers_materializations Changelog
22

3+
4+
## 0.2.10.3
5+
6+
* Bug fix for `materialized_view` macro to ensure that the correct SQL statements are generated for applying clustering and enabling automatic clustering
7+
38
## 0.2.10.2
49

510
* Bug fix for `stored_procedure` materialization to ensure that the parameters are correctly set up when no parameters are passed in
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% macro apply_clusters(relation, config) -%}
2+
{%- set cluster_by_keys = config.get('cluster_by', default=none) -%}
3+
4+
{%- if cluster_by_keys is not none and cluster_by_keys is string -%}
5+
{%- set cluster_by_keys = [cluster_by_keys] -%}
6+
{%- endif -%}
7+
{%- if cluster_by_keys is not none -%}
8+
{%- set cluster_by_string = cluster_by_keys|join(", ")-%}
9+
{% else %}
10+
{%- set cluster_by_string = none -%}
11+
{%- endif -%}
12+
13+
{% if cluster_by_string is not none and not temporary -%}
14+
ALTER MATERIALIZED VIEW {{relation}} CLUSTER BY ({{cluster_by_string}});
15+
{%- endif -%}
16+
17+
{% endmacro %}
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
{% macro create_materialized_view_as(relation, sql, config) -%}
22
{%- set secure = config.get('secure', default=false) -%}
3-
{%- set cluster_by_keys = config.get('cluster_by', default=none) -%}
4-
{%- set enable_automatic_clustering = config.get('automatic_clustering', default=false) -%}
5-
6-
{%- if cluster_by_keys is not none and cluster_by_keys is string -%}
7-
{%- set cluster_by_keys = [cluster_by_keys] -%}
8-
{%- endif -%}
9-
{%- if cluster_by_keys is not none -%}
10-
{%- set cluster_by_string = cluster_by_keys|join(", ")-%}
11-
{% else %}
12-
{%- set cluster_by_string = none -%}
13-
{%- endif -%}
143

154
CREATE OR REPLACE
16-
{% if secure -%} SECURE {%- endif %}
5+
{% if secure -%} SECURE {%- endif %}
176
MATERIALIZED VIEW {{relation}}
187
AS (
198
{{sql}}
209
);
21-
22-
{% if cluster_by_string is not none and not temporary -%}
23-
ALTER MATERIALIZED VIEW {{relation}} CLUSTER BY ({{cluster_by_string}});
24-
{%- endif -%}
25-
{% if enable_automatic_clustering and cluster_by_string is not none and not temporary -%}
26-
ALTER MATERIALIZED VIEW {{relation}} RESUME RECLUSTER;
27-
{%- endif -%}
2810

2911
{% endmacro %}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% macro enable_automatic_clustering(relation, config) -%}
2+
{%- set cluster_by_keys = config.get('cluster_by', default=none) -%}
3+
{%- set enable_automatic_clustering = config.get('automatic_clustering', default=false) -%}
4+
5+
{%- if cluster_by_keys is not none and cluster_by_keys is string -%}
6+
{%- set cluster_by_keys = [cluster_by_keys] -%}
7+
{%- endif -%}
8+
{%- if cluster_by_keys is not none -%}
9+
{%- set cluster_by_string = cluster_by_keys|join(", ")-%}
10+
{% else %}
11+
{%- set cluster_by_string = none -%}
12+
{%- endif -%}
13+
14+
{% if enable_automatic_clustering and cluster_by_string is not none and not temporary -%}
15+
ALTER MATERIALIZED VIEW {{relation}} RESUME RECLUSTER;
16+
{%- endif -%}
17+
{% endmacro %}

macros/materialized-views/snowflake/materialized_view.sql

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,33 @@
55
{% set full_refresh_mode = (should_full_refresh()) %}
66

77
{% set target_relation = this %}
8-
{% set existing_relation = load_relation(this) %}
9-
{% set tmp_relation = make_temp_relation(this) %}
8+
{%- set existing_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
9+
{% set statements = []%}
1010

1111
{{ run_hooks(pre_hooks) }}
12+
{% if existing_relation.is_table %}
13+
{{ log("Dropping relation " ~ target_relation ~ " because it is a " ~ existing_relation.type ~ " and this model is a materialized view.") }}
14+
{% do adapter.drop_relation(existing_relation) %}
15+
{% endif %}
1216

13-
{% if (existing_relation is none or full_refresh_mode) %}
14-
{% set build_sql = dbt_dataengineers_materializations.create_materialized_view_as(target_relation, sql, config) %}
15-
{% elif existing_relation.is_table %}
16-
{#-- Can't overwrite a view with a table - we must drop --#}
17-
{{ log("Dropping relation " ~ target_relation ~ " because it is a " ~ existing_relation.type ~ " and this model is a materialized view.") }}
18-
{% do adapter.drop_relation(existing_relation) %}
19-
{% set build_sql = dbt_dataengineers_materializations.create_materialized_view_as(target_relation, sql, config) %}
17+
{% if (existing_relation is none or full_refresh_mode or existing_relation.is_table) %}
18+
{% do statements.append(dbt_dataengineers_materializations.create_materialized_view_as(target_relation, sql, config)) %}
19+
{% do statements.append(dbt_dataengineers_materializations.apply_clusters(target_relation, config)) %}
20+
{% do statements.append(dbt_dataengineers_materializations.enable_automatic_clustering(target_relation, config)) %}
2021
{% else %}
2122
{# noop #}
2223
{% endif %}
23-
24-
{% if build_sql %}
25-
{% call statement("main") %}
26-
{{ build_sql }}
27-
{% endcall %}
24+
25+
{% if statements | length > 0 %}
26+
{% for statement in statements %}
27+
{% set statement = statement | trim %}
28+
{% if statement == '' %}
29+
{% continue %}
30+
{% endif %}
31+
{% call statement("main") %}
32+
{{ statement }}
33+
{% endcall %}
34+
{% endfor %}
2835
{% else %}
2936
{{ store_result('main', 'SKIP') }}
3037
{% endif %}

0 commit comments

Comments
 (0)