Skip to content

Commit ce8daeb

Browse files
authored
Merge pull request #116 from briamkin/feature/union_src_column
Feature/union src column
2 parents eeea5cc + ca5bc3e commit ce8daeb

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,15 @@ from {{ref('my_model')}}
239239
```
240240

241241
#### union_tables ([source](macros/sql/union.sql))
242-
This macro implements an "outer union." The list of tables provided to this macro will be unioned together, and any columns exclusive to a subset of these tables will be filled with `null` where not present. The `column_override` argument is used to explicitly assign the column type for a set of columns.
242+
This macro implements an "outer union." The list of tables provided to this macro will be unioned together, and any columns exclusive to a subset of these tables will be filled with `null` where not present. The `column_override` argument is used to explicitly assign the column type for a set of columns. The `source_column_name` argument is used to change the name of the`_dbt_source_table` field.
243243

244244
Usage:
245245
```
246246
{{ dbt_utils.union_tables(
247247
tables=[ref('table_1'), ref('table_2')],
248248
column_override={"some_field": "varchar(100)"},
249-
exclude=["some_other_field"]
249+
exclude=["some_other_field"],
250+
source_column_name='custom_source_column_name'
250251
) }}
251252
```
252253

macros/sql/union.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% macro union_tables(tables, column_override=none, exclude=none) -%}
1+
{% macro union_tables(tables, column_override=none, exclude=none, source_column_name=none) -%}
22

33
{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
44
{%- if not execute -%}
@@ -7,6 +7,7 @@
77

88
{%- set exclude = exclude if exclude is not none else [] %}
99
{%- set column_override = column_override if column_override is not none else {} %}
10+
{%- set source_column_name = source_column_name if source_column_name is not none else '_dbt_source_table' %}
1011

1112
{%- set table_columns = {} %}
1213
{%- set column_superset = {} %}
@@ -56,7 +57,7 @@
5657
(
5758
select
5859

59-
cast({{ dbt_utils.string_literal(table) }} as {{ dbt_utils.type_string() }}) as _dbt_source_table,
60+
cast({{ dbt_utils.string_literal(table) }} as {{ dbt_utils.type_string() }}) as {{ source_column_name }},
6061

6162
{% for col_name in ordered_column_names -%}
6263

0 commit comments

Comments
 (0)