-
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
Co-authored-by: Jay-code0
- Loading branch information
1 parent
7f72784
commit e1c4cde
Showing
3 changed files
with
215 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" |
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
{% materialization materialized_view, adapter="trino" %} | ||
|
||
{% set full_refresh_mode = (should_full_refresh()) %} | ||
|
||
{%- set target_relation = this %} | ||
{%- set existing_relation = load_relation(this) -%} | ||
|
||
{{ run_hooks(pre_hooks, inside_transaction=False) }} | ||
|
||
-- `BEGIN` happens here: | ||
{{ run_hooks(pre_hooks, inside_transaction=True) }} | ||
|
||
{% set to_drop = [] %} | ||
|
||
{% if existing_relation is none %} | ||
{{ log("No existing materialized view found, creating materialized view...", info=true) }} | ||
{%- set build_sql = create_materialized_view_as(target_relation) %} | ||
|
||
{% elif full_refresh_mode or existing_relation.type != "materializedview" %} | ||
{{ log("Found a " ~ existing_relation.type ~ " with same name. Dropping it...", info=true) }} | ||
{#-- Make sure the backup doesn't exist so we don't encounter issues with the rename below #} | ||
{% set backup_identifier = existing_relation.identifier ~ "__dbt_backup" %} | ||
{% set backup_relation = existing_relation.incorporate(path={"identifier": backup_identifier}) %} | ||
{% do adapter.drop_relation(backup_relation) %} | ||
|
||
{% do adapter.rename_relation(existing_relation, backup_relation) %} | ||
{%- set build_sql = create_materialized_view_as(target_relation) %} | ||
{% do to_drop.append(backup_relation) %} | ||
|
||
{% else %} | ||
{{ log("Refreshing materialized view '" ~ existing_relation.identifier ~ "'...", info=true) }} | ||
{%- set build_sql = refresh_materialized_view(target_relation) %} | ||
{% endif %} | ||
|
||
{% if build_sql %} | ||
|
||
{% call statement("main") %} | ||
{{ build_sql }} | ||
{% endcall %} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=True) }} | ||
|
||
{% do persist_docs(target_relation, model) %} | ||
|
||
-- `COMMIT` happens here | ||
{% do adapter.commit() %} | ||
|
||
{% else %} | ||
|
||
{{ store_result('main', 'SKIP') }} | ||
|
||
{% endif %} | ||
|
||
{% for rel in to_drop %} | ||
{% do adapter.drop_relation(rel) %} | ||
{% endfor %} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=False) }} | ||
|
||
{{ return({'relations': [target_relation]}) }} | ||
|
||
{%- endmaterialization %} | ||
|
||
|
||
{%- macro create_materialized_view_as(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