Skip to content

Commit

Permalink
Fix: Make union_relations include/exclude case insensitive (#587)
Browse files Browse the repository at this point in the history
* make case insensitive

* Integration test when excluding columns from union_relations

* modify test with casing

Co-authored-by: Doug Beatty <doug.beatty@dbtlabs.com>
  • Loading branch information
jeremyyeo and dbeatty10 authored Sep 14, 2022
1 parent a733c4b commit 1789676
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ After:
## Fixes
- Fix `union_relations` `source_column_name` none option.
- 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 @@ -100,6 +101,7 @@ After:
- [@LewisDavies](https://github.com/LewisDavies) (#554)
- [@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 @@ -168,6 +168,16 @@ models:
- expect_table_columns_to_match_set:
column_list: ["id", "name", "favorite_color", "favorite_number"]

- 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"]) }}

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

0 comments on commit 1789676

Please sign in to comment.