forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
union.sql
72 lines (44 loc) · 2.14 KB
/
union.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{% macro union_tables(tables, column_override=none, exclude=none) -%}
{%- set exclude = exclude if exclude is not none else [] %}
{%- set column_override = column_override if column_override is not none else {} %}
{%- set table_columns = {} %}
{%- set column_superset = {} %}
{%- for table in tables -%}
{%- set _ = table_columns.update({table: []}) %}
{%- if table.name -%}
{%- set schema, table_name = table.schema, table.name -%}
{%- else -%}
{%- set schema, table_name = (table | string).split(".") -%}
{%- endif -%}
{%- set cols = adapter.get_columns_in_table(schema, table_name) %}
{%- for col in cols -%}
{%- if col.column not in exclude %}
{# update the list of columns in this table #}
{%- set _ = table_columns[table].append(col.column) %}
{%- if col.column in column_superset -%}
{%- set stored = column_superset[col.column] %}
{%- if col.is_string() and stored.is_string() and col.string_size() > stored.string_size() -%}
{%- set _ = column_superset.update({col.column: col}) %}
{%- endif %}
{%- else -%}
{%- set _ = column_superset.update({col.column: col}) %}
{%- endif -%}
{%- endif -%}
{%- endfor %}
{%- endfor %}
{%- set ordered_column_names = column_superset.keys() %}
{%- for table in tables -%}
(
select
'{{ table }}'::text as _dbt_source_table,
{% for col_name in ordered_column_names -%}
{%- set col = column_superset[col_name] %}
{%- set col_type = column_override.get(col.column, col.data_type) %}
{%- set col_name = adapter.quote(col_name) if col_name in table_columns[table] else 'null' %}
{{ col_name }}::{{ col_type }} as {{ col.quoted }} {% if not loop.last %},{% endif %}
{%- endfor %}
from {{ table }}
)
{% if not loop.last %} union all {% endif %}
{%- endfor %}
{%- endmacro %}