Created
August 7, 2022 05:42
-
-
Save lifnaja/91ba020860f0d1e4182b6e6f973fefd2 to your computer and use it in GitHub Desktop.
example override get merge query
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
{% macro default__get_merge_sql(target, source, unique_key, dest_columns, predicates) -%} | |
{%- set predicates = [] if predicates is none else [] + predicates -%} | |
{%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%} | |
{%- set update_columns = config.get('merge_update_columns', default = dest_columns | map(attribute="quoted") | list) -%} | |
{%- set sql_header = config.get('sql_header', none) -%} | |
{% if unique_key %} | |
{% if unique_key is sequence and unique_key is not mapping and unique_key is not string %} | |
{% for key in unique_key %} | |
{% set this_key_match %} | |
IFNULL(CAST(DBT_INTERNAL_SOURCE.{{ key }} AS STRING), "null") = IFNULL(CAST(DBT_INTERNAL_DEST.{{ key }} AS STRING), "null") | |
{% endset %} | |
{% do predicates.append(this_key_match) %} | |
{% endfor %} | |
{% else %} | |
{% set unique_key_match %} | |
IFNULL(CAST(DBT_INTERNAL_SOURCE.{{ key }} AS STRING), "null") = IFNULL(CAST(DBT_INTERNAL_DEST.{{ key }} AS STRING), "null") | |
{% endset %} | |
{% do predicates.append(unique_key_match) %} | |
{% endif %} | |
{% else %} | |
{% do predicates.append('FALSE') %} | |
{% endif %} | |
{{ sql_header if sql_header is not none }} | |
merge into {{ target }} as DBT_INTERNAL_DEST | |
using {{ source }} as DBT_INTERNAL_SOURCE | |
on {{ predicates | join(' and ') }} | |
{% if unique_key %} | |
when matched then update set | |
{% for column_name in update_columns -%} | |
{{ column_name }} = DBT_INTERNAL_SOURCE.{{ column_name }} | |
{%- if not loop.last %}, {%- endif %} | |
{%- endfor %} | |
{% endif %} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment