-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add materialized view materialization
- Loading branch information
1 parent
7f72784
commit 8054838
Showing
3 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
kind: Features | ||
body: Add Materialized View materialization | ||
time: 2023-03-07T10:33:24.990266+01:00 | ||
custom: | ||
Author: Jay-code0 damian3031 | ||
Issue: "258" | ||
PR: "260" |
41 changes: 41 additions & 0 deletions
41
dbt/include/trino/macros/materializations/materialized_view.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{% materialization materialized_view, adapter="trino" %} | ||
{%- set target_relation = this %} | ||
{%- set existing_relation = load_relation(this) -%} | ||
|
||
{{ run_hooks(pre_hooks) }} | ||
|
||
{% if existing_relation is none %} | ||
{{ log("No existing materialized view found, creating materialized view...", info=true) }} | ||
{%- set build_sql = build_materialized_view(target_relation) %} | ||
|
||
{% elif existing_relation.type != "materializedview" %} | ||
{{ log("Found a " ~ existing_relation.type ~ " with same name. Dropping it...", info=true) }} | ||
{{ adapter.drop_relation(existing_relation) }} | ||
{%- set build_sql = build_materialized_view(target_relation) %} | ||
|
||
{% elif existing_relation.type == "materializedview" %} | ||
{{ log("Refreshing materialized view '" ~ existing_relation.identifier ~ "'...", info=true) }} | ||
{%- set build_sql = refresh_materialized_view(target_relation) %} | ||
{% endif %} | ||
|
||
{#-- build model #} | ||
{%- call statement('main') -%} | ||
{{ build_sql }} | ||
{% endcall %} | ||
|
||
{{ run_hooks(post_hooks) }} | ||
|
||
{{ return({'relations': [target_relation]}) }} | ||
{% endmaterialization %} | ||
|
||
|
||
{%- macro build_materialized_view(target_relation) -%} | ||
{%- set sqlcode= "CREATE OR REPLACE MATERIALIZED VIEW " ~ target_relation ~ " AS " ~ sql %} | ||
{{ sqlcode }} | ||
{%- endmacro -%} | ||
|
||
|
||
{%- macro refresh_materialized_view(target_relation) -%} | ||
{%- set sqlcode= "REFRESH MATERIALIZED VIEW " ~ target_relation %} | ||
{{ sqlcode }} | ||
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters