Skip to content

Commit

Permalink
ADAP-814: Refactor prep for MV updates (#8459)
Browse files Browse the repository at this point in the history
* apply reformatting changes only for #8449
* add logging back to get_create_materialized_view_as_sql
* changie
  • Loading branch information
mikealfare authored and peterallenwebb committed Aug 30, 2023
1 parent 45c8d6d commit 7bb45d5
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 137 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Under the Hood-20230821-134801.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Under the Hood
body: 'Re-organize jinja macros: relation-specific in /macros/adapters/relations/<relation>,
relation agnostic in /macros/relations'
time: 2023-08-21T13:48:01.474731-04:00
custom:
Author: mikealfare
Issue: "8449"
44 changes: 0 additions & 44 deletions core/dbt/include/global_project/macros/adapters/drop_relation.sql

This file was deleted.

12 changes: 0 additions & 12 deletions core/dbt/include/global_project/macros/adapters/relation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,6 @@
{% endmacro %}


{% macro rename_relation(from_relation, to_relation) -%}
{{ return(adapter.dispatch('rename_relation', 'dbt')(from_relation, to_relation)) }}
{% endmacro %}

{% macro default__rename_relation(from_relation, to_relation) -%}
{% set target_name = adapter.quote_as_configured(to_relation.identifier, 'identifier') %}
{% call statement('rename_relation') -%}
alter table {{ from_relation }} rename to {{ target_name }}
{%- endcall %}
{% endmacro %}


{% macro get_or_create_relation(database, schema, identifier, type) -%}
{{ return(adapter.dispatch('get_or_create_relation', 'dbt')(database, schema, identifier, type)) }}
{% endmacro %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{# /*
This only exists for backwards compatibility for 1.6.0. In later versions, the general `get_replace_sql`
macro is called as replace is inherently not limited to a single relation (it takes in two relations).
*/ #}

{% macro get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) %}
{{- log('Applying REPLACE to: ' ~ relation) -}}
{{- adapter.dispatch('get_replace_materialized_view_as_sql', 'dbt')(relation, sql, existing_relation, backup_relation, intermediate_relation) -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@


{% macro default__get_create_materialized_view_as_sql(relation, sql) -%}
{{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }}
{{ exceptions.raise_compiler_error(
"`get_create_materialized_view_as_sql` has not been implemented for this adapter."
) }}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{# /*
This was already implemented. Instead of creating a new macro that aligns with the standard,
this was reused and the default was maintained. This gets called by `drop_relation`, which
actually executes the drop, and `get_drop_sql`, which returns the template.
*/ #}

{% macro drop_materialized_view(relation) -%}
{{ return(adapter.dispatch('drop_materialized_view', 'dbt')(relation)) }}
{%- endmacro %}

{% macro default__drop_materialized_view(relation) -%}
drop materialized view if exists {{ relation }} cascade
{%- endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@


{% macro default__refresh_materialized_view(relation) %}
{{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }}
{{ exceptions.raise_compiler_error("`refresh_materialized_view` has not been implemented for this adapter.") }}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{# /*
This was already implemented. Instead of creating a new macro that aligns with the standard,
this was reused and the default was maintained. This gets called by `drop_relation`, which
actually executes the drop, and `get_drop_sql`, which returns the template.
*/ #}

{% macro drop_table(relation) -%}
{{ return(adapter.dispatch('drop_table', 'dbt')(relation)) }}
{%- endmacro %}

{% macro default__drop_table(relation) -%}
drop table if exists {{ relation }} cascade
{%- endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{# /*
This was already implemented. Instead of creating a new macro that aligns with the standard,
this was reused and the default was maintained. This gets called by `drop_relation`, which
actually executes the drop, and `get_drop_sql`, which returns the template.
*/ #}

{% macro drop_view(relation) -%}
{{ return(adapter.dispatch('drop_view', 'dbt')(relation)) }}
{%- endmacro %}

{% macro default__drop_view(relation) -%}
drop view if exists {{ relation }} cascade
{%- endmacro %}
17 changes: 17 additions & 0 deletions core/dbt/include/global_project/macros/relations/drop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% macro drop_relation(relation) -%}
{{ return(adapter.dispatch('drop_relation', 'dbt')(relation)) }}
{% endmacro %}

{% macro default__drop_relation(relation) -%}
{% call statement('drop_relation', auto_begin=False) -%}
{%- if relation.is_table -%}
{{- drop_table(relation) -}}
{%- elif relation.is_view -%}
{{- drop_view(relation) -}}
{%- elif relation.is_materialized_view -%}
{{- drop_materialized_view(relation) -}}
{%- else -%}
drop {{ relation.type }} if exists {{ relation }} cascade
{%- endif -%}
{%- endcall %}
{% endmacro %}
10 changes: 10 additions & 0 deletions core/dbt/include/global_project/macros/relations/rename.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% macro rename_relation(from_relation, to_relation) -%}
{{ return(adapter.dispatch('rename_relation', 'dbt')(from_relation, to_relation)) }}
{% endmacro %}

{% macro default__rename_relation(from_relation, to_relation) -%}
{% set target_name = adapter.quote_as_configured(to_relation.identifier, 'identifier') %}
{% call statement('rename_relation') -%}
alter table {{ from_relation }} rename to {{ target_name }}
{%- endcall %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -1,84 +1,5 @@
{% macro postgres__get_alter_materialized_view_as_sql(
relation,
configuration_changes,
sql,
existing_relation,
backup_relation,
intermediate_relation
) %}

-- apply a full refresh immediately if needed
{% if configuration_changes.requires_full_refresh %}

{{ get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) }}

-- otherwise apply individual changes as needed
{% else %}

{{ postgres__update_indexes_on_materialized_view(relation, configuration_changes.indexes) }}

{%- endif -%}

{% endmacro %}


{% macro postgres__get_create_materialized_view_as_sql(relation, sql) %}
create materialized view if not exists {{ relation }} as {{ sql }};

{% for _index_dict in config.get('indexes', []) -%}
{{- get_create_index_sql(relation, _index_dict) -}}
{%- endfor -%}

{% endmacro %}


{% macro postgres__get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) %}
{{- get_create_materialized_view_as_sql(intermediate_relation, sql) -}}

{% if existing_relation is not none %}
alter materialized view {{ existing_relation }} rename to {{ backup_relation.include(database=False, schema=False) }};
{% endif %}

alter materialized view {{ intermediate_relation }} rename to {{ relation.include(database=False, schema=False) }};

{% endmacro %}


{% macro postgres__get_materialized_view_configuration_changes(existing_relation, new_config) %}
{% set _existing_materialized_view = postgres__describe_materialized_view(existing_relation) %}
{% set _configuration_changes = existing_relation.get_materialized_view_config_change_collection(_existing_materialized_view, new_config) %}
{% do return(_configuration_changes) %}
{% endmacro %}


{% macro postgres__refresh_materialized_view(relation) %}
refresh materialized view {{ relation }}
{% endmacro %}


{%- macro postgres__update_indexes_on_materialized_view(relation, index_changes) -%}
{{- log("Applying UPDATE INDEXES to: " ~ relation) -}}

{%- for _index_change in index_changes -%}
{%- set _index = _index_change.context -%}

{%- if _index_change.action == "drop" -%}

{{ postgres__get_drop_index_sql(relation, _index.name) }};

{%- elif _index_change.action == "create" -%}

{{ postgres__get_create_index_sql(relation, _index.as_node_config) }}

{%- endif -%}

{%- endfor -%}

{%- endmacro -%}


{% macro postgres__describe_materialized_view(relation) %}
-- for now just get the indexes, we don't need the name or the query yet
{% set _indexes = run_query(get_show_indexes_sql(relation)) %}
{% do return({'indexes': _indexes}) %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{# /*
This only exists for backwards compatibility for 1.6.0. In later versions, the general `get_replace_sql`
macro is called as replace is inherently not limited to a single relation (it takes in two relations).
*/ #}


{% macro postgres__get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) %}
{{- get_create_materialized_view_as_sql(intermediate_relation, sql) -}}

{% if existing_relation is not none %}
alter materialized view {{ existing_relation }} rename to {{ backup_relation.include(database=False, schema=False) }};
{% endif %}

alter materialized view {{ intermediate_relation }} rename to {{ relation.include(database=False, schema=False) }};

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% macro postgres__get_alter_materialized_view_as_sql(
relation,
configuration_changes,
sql,
existing_relation,
backup_relation,
intermediate_relation
) %}

-- apply a full refresh immediately if needed
{% if configuration_changes.requires_full_refresh %}

{{ get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) }}

-- otherwise apply individual changes as needed
{% else %}

{{ postgres__update_indexes_on_materialized_view(relation, configuration_changes.indexes) }}

{%- endif -%}

{% endmacro %}


{%- macro postgres__update_indexes_on_materialized_view(relation, index_changes) -%}
{{- log("Applying UPDATE INDEXES to: " ~ relation) -}}

{%- for _index_change in index_changes -%}
{%- set _index = _index_change.context -%}

{%- if _index_change.action == "drop" -%}

{{ postgres__get_drop_index_sql(relation, _index.name) }};

{%- elif _index_change.action == "create" -%}

{{ postgres__get_create_index_sql(relation, _index.as_node_config) }}

{%- endif -%}

{%- endfor -%}

{%- endmacro -%}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% macro postgres__get_create_materialized_view_as_sql(relation, sql) %}
create materialized view if not exists {{ relation }} as {{ sql }};

{% for _index_dict in config.get('indexes', []) -%}
{{- get_create_index_sql(relation, _index_dict) -}}
{%- endfor -%}

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% macro postgres__describe_materialized_view(relation) %}
-- for now just get the indexes, we don't need the name or the query yet
{% set _indexes = run_query(get_show_indexes_sql(relation)) %}
{% do return({'indexes': _indexes}) %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro postgres__refresh_materialized_view(relation) %}
refresh materialized view {{ relation }}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro postgres__get_rename_materialized_view_sql(relation, new_name) %}
alter materialized view {{ relation }} rename to {{ new_name }}
{% endmacro %}

0 comments on commit 7bb45d5

Please sign in to comment.