Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Make union_relations include/exclude case insensitive #587

Merged
merged 3 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

## Fixes
- Enable a negative part_number for `split_part()` ([#557](https://github.com/dbt-labs/dbt-utils/issues/557), [#559](https://github.com/dbt-labs/dbt-utils/pull/559))
- Make `exclude` case insensitive for `union_relations()` ([#578](https://github.com/dbt-labs/dbt-utils/issues/557), [#587](https://github.com/dbt-labs/dbt-utils/issues/587))

## Quality of life
- Documentation about listagg macro ([#544](https://github.com/dbt-labs/dbt-utils/issues/544), [#560](https://github.com/dbt-labs/dbt-utils/pull/560))
Expand All @@ -21,6 +22,7 @@
- [@clausherther](https://github.com/clausherther) (#555)
- [@epapineau](https://github.com/epapineau) (#583)
- [@b-per](https://github.com/b-per) (#559)
- [@dbeatty10](https://github.com/dbeatty10), [@jeremyyeo](https://github.com/jeremyyeo) (#587)

# dbt-utils v0.8.4
## Fixes
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/data/sql/data_union_exclude_expected.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id,favorite_color,favorite_number
1,,pi
2,,e
3,,4
1,"green",7
2,"pink",13
10 changes: 10 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ models:
tests:
- dbt_utils.not_constant

- name: test_union_exclude_lowercase
tests:
- dbt_utils.equality:
compare_model: ref('data_union_exclude_expected')

- name: test_union_exclude_uppercase
tests:
- dbt_utils.equality:
compare_model: ref('data_union_exclude_expected')

- name: test_get_relations_by_pattern
tests:
- dbt_utils.equality:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

{{ dbt_utils.union_relations(
relations=[
ref('data_union_table_1'),
ref('data_union_table_2'),
],
exclude=['name']
) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

{{ dbt_utils.union_relations(
relations=[
ref('data_union_table_1'),
ref('data_union_table_2'),
],
exclude=['NAME']
) }}
4 changes: 4 additions & 0 deletions integration_tests/models/sql/test_union_exclude_lowercase.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select
{{ dbt_utils.star(ref("test_union_exclude_base_lowercase"), except=["_dbt_source_relation"]) }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of #624 and #661, you can pass none into source_column_name to suppress the column from being created, if you'd rather.


from {{ ref("test_union_exclude_base_lowercase") }}
4 changes: 4 additions & 0 deletions integration_tests/models/sql/test_union_exclude_uppercase.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select
{{ dbt_utils.star(ref("test_union_exclude_base_uppercase"), except=["_DBT_SOURCE_RELATION"]) }}

from {{ ref("test_union_exclude_base_uppercase") }}
18 changes: 16 additions & 2 deletions macros/sql/union.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@

{%- set relation_columns = {} -%}
{%- set column_superset = {} -%}
{%- set all_excludes = [] -%}
{%- set all_includes = [] -%}

{%- if exclude -%}
{%- for exc in exclude -%}
{%- do all_excludes.append(exc | lower) -%}
{%- endfor -%}
{%- endif -%}

{%- if include -%}
{%- for inc in include -%}
{%- do all_includes.append(inc | lower) -%}
{%- endfor -%}
{%- endif -%}

{%- for relation in relations -%}

Expand All @@ -28,10 +42,10 @@
{%- for col in cols -%}

{#- If an exclude list was provided and the column is in the list, do nothing -#}
{%- if exclude and col.column in exclude -%}
{%- if exclude and col.column | lower in all_excludes -%}

{#- If an include list was provided and the column is not in the list, do nothing -#}
{%- elif include and col.column not in include -%}
{%- elif include and col.column | lower not in all_includes -%}

{#- Otherwise add the column to the column superset -#}
{%- else -%}
Expand Down