Skip to content

Commit 162a15c

Browse files
Feature: Add option to remove the source_column_name on the union_relations macro (#624)
* Changed the union macro to function with `none` as an argument * Updated the README with the new functionality * Added to the CHANGELOG * Update README.md * Added integration test for omitting source column on union_relations macro * Update integration_tests/tests/generic/expect_table_columns_to_match_set.sql
1 parent fbbc0fb commit 162a15c

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
## Contributors:
99
--->
1010

11+
# Unreleased
12+
13+
## New features
14+
- New feature to omit the `source_column_name` column on the `union_relations` macro ([#331](https://github.com/dbt-labs/dbt-utils/issues/331), [#624](https://github.com/dbt-labs/dbt-utils/pull/624))
15+
16+
## Contributors:
17+
- [@christineberger](https://github.com/christineberger) (#624)
18+
1119
# dbt-utils v0.8.6
1220

1321
## New features

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ final query. Note the `include` and `exclude` arguments are mutually exclusive.
886886
* `column_override` (optional): A dictionary of explicit column type overrides,
887887
e.g. `{"some_field": "varchar(100)"}`.``
888888
* `source_column_name` (optional, `default="_dbt_source_relation"`): The name of
889-
the column that records the source of this row.
889+
the column that records the source of this row. Pass `None` to omit this column from the results.
890890
* `where` (optional): Filter conditions to include in the `where` clause.
891891

892892
#### generate_series ([source](macros/sql/generate_series.sql))

integration_tests/models/sql/schema.yml

+5
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ models:
162162
- name: favorite_number
163163
tests:
164164
- dbt_utils.not_constant
165+
166+
- name: test_union_no_source_column
167+
tests:
168+
- expect_table_columns_to_match_set:
169+
column_list: ["id", "name", "favorite_color", "favorite_number"]
165170

166171
- name: test_get_relations_by_pattern
167172
tests:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{ dbt_utils.union_relations([
2+
ref('data_union_table_1'),
3+
ref('data_union_table_2')
4+
],
5+
source_column_name = none
6+
) }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{#
2+
This macro is copied and slightly edited from the dbt_expectations package.
3+
At the time of this addition, dbt_expectations couldn't be added because
4+
integration_tests is installing dbt_utils from local without a hard-coded
5+
path. dbt is not able to resolve duplicate dependencies of dbt_utils
6+
due to this.
7+
#}
8+
9+
{%- test expect_table_columns_to_match_set(model, column_list, transform="upper") -%}
10+
{%- if execute -%}
11+
{%- set column_list = column_list | map(transform) | list -%}
12+
13+
{# Replaces dbt_expectations._get_column_list() #}
14+
{%- set relation_column_names = adapter.get_columns_in_relation(model)
15+
| map(attribute="name")
16+
| map(transform)
17+
| list
18+
-%}
19+
20+
{# Replaces dbt_expectations._list_intersect() #}
21+
{%- set matching_columns = [] -%}
22+
{%- for itm in column_list -%}
23+
{%- if itm in relation_column_names -%}
24+
{%- do matching_columns.append(itm) -%}
25+
{%- endif -%}
26+
{%- endfor -%}
27+
28+
with relation_columns as (
29+
30+
{% for col_name in relation_column_names %}
31+
select cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as relation_column
32+
{% if not loop.last %}union all{% endif %}
33+
{% endfor %}
34+
),
35+
input_columns as (
36+
37+
{% for col_name in column_list %}
38+
select cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as input_column
39+
{% if not loop.last %}union all{% endif %}
40+
{% endfor %}
41+
)
42+
select *
43+
from
44+
relation_columns r
45+
full outer join
46+
input_columns i on r.relation_column = i.input_column
47+
where
48+
-- catch any column in input list that is not in the list of table columns
49+
-- or any table column that is not in the input list
50+
r.relation_column is null or
51+
i.input_column is null
52+
53+
{%- endif -%}
54+
{%- endtest -%}

macros/sql/union.sql

+3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@
8585
(
8686
select
8787

88+
{%- if source_column_name != none %}
8889
cast({{ dbt_utils.string_literal(relation) }} as {{ dbt_utils.type_string() }}) as {{ source_column_name }},
90+
{%- endif %}
91+
8992
{% for col_name in ordered_column_names -%}
9093

9194
{%- set col = column_superset[col_name] %}

0 commit comments

Comments
 (0)