Skip to content

Commit 7e3da7c

Browse files
authored
Add where parameter to union_relations (#554)
* Add where parameter * Add union where test * Update changelog
1 parent c5ea35a commit 7e3da7c

File tree

6 files changed

+29
-3
lines changed

6 files changed

+29
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
## New features
33
- 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))
44

5+
## New features
6+
- Add `where` parameter to `union_relations` macro ([#554](https://github.com/dbt-labs/dbt-utils/pull/554))
57
## Quality of life
68
- Documentation about listagg macro ([#544](https://github.com/dbt-labs/dbt-utils/issues/544), [#560](https://github.com/dbt-labs/dbt-utils/pull/560))
79
- Fix links to macro section in table of contents ([#555](https://github.com/dbt-labs/dbt-utils/pull/555))

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ final query. Note the `include` and `exclude` arguments are mutually exclusive.
854854
e.g. `{"some_field": "varchar(100)"}`.``
855855
* `source_column_name` (optional, `default="_dbt_source_relation"`): The name of
856856
the column that records the source of this row.
857+
* `where` (optional): Filter conditions to include in the `where` clause.
857858

858859
#### generate_series ([source](macros/sql/generate_series.sql))
859860
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.

integration_tests/models/sql/schema.yml

+10
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ models:
153153
- dbt_utils.equality:
154154
compare_model: ref('data_union_expected')
155155

156+
- name: test_union_where
157+
columns:
158+
- name: id
159+
tests:
160+
- dbt_utils.expression_is_true:
161+
expression: "= 1"
162+
- name: favorite_number
163+
tests:
164+
- dbt_utils.not_constant
165+
156166
- name: test_get_relations_by_pattern
157167
tests:
158168
- dbt_utils.equality:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
select
2+
id,
3+
favorite_number
4+
from
5+
{{ ref('test_union_where_base') }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{ dbt_utils.union_relations(
2+
[ref('data_union_table_1'), ref('data_union_table_2')],
3+
where="id = 1"
4+
) }}

macros/sql/union.sql

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{%- macro union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation') -%}
2-
{{ return(adapter.dispatch('union_relations', 'dbt_utils')(relations, column_override, include, exclude, source_column_name)) }}
1+
{%- macro union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation', where=none) -%}
2+
{{ return(adapter.dispatch('union_relations', 'dbt_utils')(relations, column_override, include, exclude, source_column_name, where)) }}
33
{% endmacro %}
44

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

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

9494
from {{ relation }}
95+
96+
{% if where -%}
97+
where {{ where }}
98+
{%- endif %}
9599
)
96100

97101
{% if not loop.last -%}

0 commit comments

Comments
 (0)