-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathget_columns_spec_ddl.sql
37 lines (33 loc) · 1.37 KB
/
get_columns_spec_ddl.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
{% macro bigquery__format_column(column) -%}
{% set data_type = column.data_type %}
{% set formatted = column.column.lower() ~ " " ~ data_type %}
{{ return({'name': column.name, 'data_type': data_type, 'formatted': formatted}) }}
{%- endmacro -%}
{% macro bigquery__get_empty_schema_sql(columns) %}
{%- set col_err = [] -%}
{% for col in columns.values() %}
{%- if col['data_type'] is not defined -%}
{{ col_err.append(col['name']) }}
{%- endif -%}
{%- endfor -%}
{%- if (col_err | length) > 0 -%}
{{ exceptions.column_type_missing(column_names=col_err) }}
{%- endif -%}
{%- set columns = adapter.nest_column_data_types(columns) -%}
{{ return(dbt.default__get_empty_schema_sql(columns)) }}
{% endmacro %}
{% macro bigquery__get_select_subquery(sql) %}
select {{ adapter.dispatch('get_column_names')() }}
from (
{{ sql }}
) as model_subq
{%- endmacro %}
{% macro bigquery__get_column_names() %}
{#- loop through nested user_provided_columns to get column names -#}
{%- set user_provided_columns = adapter.nest_column_data_types(model['columns']) -%}
{%- for i in user_provided_columns %}
{%- set col = user_provided_columns[i] -%}
{%- set col_name = adapter.quote(col['name']) if col.get('quote') else col['name'] -%}
{{ col_name }}{{ ", " if not loop.last }}
{%- endfor -%}
{% endmacro %}