Skip to content

Commit

Permalink
Merge pull request dbt-labs#116 from briamkin/feature/union_src_column
Browse files Browse the repository at this point in the history
Feature/union src column
  • Loading branch information
drewbanin authored Jan 21, 2019
2 parents eeea5cc + ca5bc3e commit ce8daeb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,15 @@ from {{ref('my_model')}}
```

#### union_tables ([source](macros/sql/union.sql))
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.
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.

Usage:
```
{{ dbt_utils.union_tables(
tables=[ref('table_1'), ref('table_2')],
column_override={"some_field": "varchar(100)"},
exclude=["some_other_field"]
exclude=["some_other_field"],
source_column_name='custom_source_column_name'
) }}
```

Expand Down
5 changes: 3 additions & 2 deletions macros/sql/union.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% macro union_tables(tables, column_override=none, exclude=none) -%}
{% macro union_tables(tables, column_override=none, exclude=none, source_column_name=none) -%}

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

{%- set exclude = exclude if exclude is not none else [] %}
{%- set column_override = column_override if column_override is not none else {} %}
{%- set source_column_name = source_column_name if source_column_name is not none else '_dbt_source_table' %}

{%- set table_columns = {} %}
{%- set column_superset = {} %}
Expand Down Expand Up @@ -56,7 +57,7 @@
(
select

cast({{ dbt_utils.string_literal(table) }} as {{ dbt_utils.type_string() }}) as _dbt_source_table,
cast({{ dbt_utils.string_literal(table) }} as {{ dbt_utils.type_string() }}) as {{ source_column_name }},

{% for col_name in ordered_column_names -%}

Expand Down

0 comments on commit ce8daeb

Please sign in to comment.