Skip to content

Commit

Permalink
Merge pull request #114 from greg-finley/greg-dupe2
Browse files Browse the repository at this point in the history
Loop twice
  • Loading branch information
fivetran-joemarkiewicz committed Sep 27, 2023
2 parents a03b219 + 1677fd5 commit 2d74ab4
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion macros/remove_duplicate_and_prefix_from_columns.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{% macro remove_duplicate_and_prefix_from_columns(columns, prefix='', exclude=[]) %}

{# Loop once to find duplicates #}
{%- set duplicate_exclude = [] -%}
{% for col in columns if col.name not in exclude %}
{%- for dupe in columns if col.name[prefix|length:]|lower == dupe.name|lower -%}
{%- do duplicate_exclude.append(col.name) -%}
{%- do duplicate_exclude.append(dupe.name) -%}
, coalesce({{ col.name }}, {{dupe.name}}) as {{ col.name[prefix|length:] }}
{%- endfor %}
{% endfor %}

{# Loop again to find non-duplicates #}
{% for col in columns if col.name not in exclude %}
{%- if col.name|lower not in duplicate_exclude|lower -%}
{% if col.name[:prefix|length]|lower == prefix %}
, {{ col.name }} as {{ col.name[prefix|length:] }}
Expand All @@ -16,4 +21,4 @@
{%- endif -%}
{% endfor %}

{% endmacro %}
{% endmacro %}

0 comments on commit 2d74ab4

Please sign in to comment.