Skip to content

Commit

Permalink
Add where parameter to union_relations (dbt-labs#554)
Browse files Browse the repository at this point in the history
* Add where parameter

* Add union where test

* Update changelog
  • Loading branch information
LewisDavies authored May 12, 2022
1 parent c5ea35a commit 7e3da7c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
## New features
- Add an optional `where` clause parameter to `get_column_values()` to filter values returned ([#511](https://github.com/dbt-labs/dbt-utils/issues/511), [#583](https://github.com/dbt-labs/dbt-utils/pull/583))

## New features
- Add `where` parameter to `union_relations` macro ([#554](https://github.com/dbt-labs/dbt-utils/pull/554))
## 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))
- Fix links to macro section in table of contents ([#555](https://github.com/dbt-labs/dbt-utils/pull/555))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ final query. Note the `include` and `exclude` arguments are mutually exclusive.
e.g. `{"some_field": "varchar(100)"}`.``
* `source_column_name` (optional, `default="_dbt_source_relation"`): The name of
the column that records the source of this row.
* `where` (optional): Filter conditions to include in the `where` clause.

#### generate_series ([source](macros/sql/generate_series.sql))
This macro implements a cross-database mechanism to generate an arbitrarily long list of numbers. Specify the maximum number you'd like in your list and it will create a 1-indexed SQL result set.
Expand Down
10 changes: 10 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ models:
- dbt_utils.equality:
compare_model: ref('data_union_expected')

- name: test_union_where
columns:
- name: id
tests:
- dbt_utils.expression_is_true:
expression: "= 1"
- name: favorite_number
tests:
- dbt_utils.not_constant

- name: test_get_relations_by_pattern
tests:
- dbt_utils.equality:
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/models/sql/test_union_where.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
select
id,
favorite_number
from
{{ ref('test_union_where_base') }}
4 changes: 4 additions & 0 deletions integration_tests/models/sql/test_union_where_base.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ dbt_utils.union_relations(
[ref('data_union_table_1'), ref('data_union_table_2')],
where="id = 1"
) }}
10 changes: 7 additions & 3 deletions macros/sql/union.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{%- macro union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation') -%}
{{ return(adapter.dispatch('union_relations', 'dbt_utils')(relations, column_override, include, exclude, source_column_name)) }}
{%- macro union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation', where=none) -%}
{{ return(adapter.dispatch('union_relations', 'dbt_utils')(relations, column_override, include, exclude, source_column_name, where)) }}
{% endmacro %}

{%- macro default__union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation') -%}
{%- macro default__union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation', where=none) -%}

{%- if exclude and include -%}
{{ exceptions.raise_compiler_error("Both an exclude and include list were provided to the `union` macro. Only one is allowed") }}
Expand Down Expand Up @@ -92,6 +92,10 @@
{%- endfor %}

from {{ relation }}

{% if where -%}
where {{ where }}
{%- endif %}
)

{% if not loop.last -%}
Expand Down

0 comments on commit 7e3da7c

Please sign in to comment.