-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_columns_for_macro.sql
73 lines (55 loc) · 2.18 KB
/
get_columns_for_macro.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
73
{% macro default__get_columns_for_macro(table_name, schema_name, database_name=target.database) %}
{% set query %}
select
concat(
'{"name": "',
lower(column_name),
'", "datatype": ',
case
when lower(data_type) like '%timestamp%' then 'dbt.type_timestamp()'
when lower(data_type) = 'text' then 'dbt.type_string()'
when lower(data_type) = 'boolean' then 'dbt.type_boolean()'
when lower(data_type) like '%num%' then 'dbt.type_numeric()'
when lower(data_type) = 'float' then 'dbt.type_float()'
when lower(data_type) = 'date' then '"date"'
end,
'}')
from {{ database_name }}.information_schema.columns
where lower(table_name) = '{{ table_name }}'
and lower(table_schema) = '{{ schema_name }}'
order by 1
{% endset %}
{% set results = run_query(query) %}
{% set results_list = results.columns[0].values() %}}
{{ return(results_list) }}
{% endmacro %}
{% macro bigquery__get_columns_for_macro(table_name, schema_name, database_name=target.database) %}
{% set query %}
select
concat(
'{"name": "',
lower(column_name),
'", "datatype": ',
case
when lower(data_type) like '%timestamp%' then 'dbt.type_timestamp()'
when lower(data_type) = 'string' then 'dbt.type_string()'
when lower(data_type) = 'bool' then 'dbt.type_boolean()'
when lower(data_type) like '%num%' then 'dbt.type_numeric()'
when lower(data_type) = 'float64' then 'dbt.type_float()'
when lower(data_type) = 'int64' then 'dbt.type_int()'
when lower(data_type) = 'date' then '"date"'
when lower(data_type) = 'datetime' then '"datetime"'
end,
'}')
from `{{ database_name }}`.{{ schema_name }}.INFORMATION_SCHEMA.COLUMNS
where lower(table_name) = '{{ table_name }}'
and lower(table_schema) = '{{ schema_name }}'
order by 1
{% endset %}
{% set results = run_query(query) %}
{% set results_list = results.columns[0].values() %}}
{{ return(results_list) }}
{% endmacro %}
{% macro get_columns_for_macro(table_name, schema_name, database_name) -%}
{{ return(adapter.dispatch('get_columns_for_macro')(table_name, schema_name, database_name)) }}
{%- endmacro %}