Skip to content

Commit

Permalink
fix create table as macro
Browse files Browse the repository at this point in the history
Create table as cannot insert into relation due to missmatch column order between the result of sql
  • Loading branch information
viplazylmht committed Aug 10, 2022
1 parent d75d0ba commit ff52378
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@

on HASH( {{ get_qouted_csv_with_prefix("DBT_INTERNAL_DEST", unique_key) }} ) = HASH({{ get_qouted_csv_with_prefix("DBT_INTERNAL_SOURCE", unique_key) }})

when matched then update set
{% for column_name in merge_update_columns -%}
{{ adapter.quote(column_name) }} = DBT_INTERNAL_SOURCE.{{ adapter.quote(column_name) }}
{%- if not loop.last %}, {% endif %}
{%- endfor %}
{%- if merge_update_columns is string %}
{%- set merge_update_columns = merge_update_columns.split(',') %}
{% endif %}

{%- if merge_update_columns | length > 0 %}
when matched then update set
{% for column_name in merge_update_columns -%}
{{ adapter.quote(column_name) }} = DBT_INTERNAL_SOURCE.{{ adapter.quote(column_name) }}
{%- if not loop.last %}, {% endif %}
{%- endfor %}
{%- endif %}

when not matched then insert
({{ dest_columns_csv }})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

insert into {{ relation }} ({{ dest_cols_csv }})
(
{{ sql }}
select {{ dest_cols_csv }} from (
select * from {{ sql }}
) as DBT_MASKED_TARGET
);

{% endmacro %}
Expand Down

0 comments on commit ff52378

Please sign in to comment.