-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_column_names_only.sql
75 lines (55 loc) · 1.94 KB
/
get_column_names_only.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
74
75
{% macro default__get_column_names_only(table_name, schema_name, database_name=target.database) %}
{% set query %}
select
lower(column_name)
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() %}}
{% for col in results_list %}
{% if col in ('id','name') %}
{% do jinja_macro.append(' ' ~ col ~ ' as ' ~ table_name ~ '_' ~ col ~ (',' if not loop.last)) %}
{% else %}
{% do jinja_macro.append(' ' ~ col ~ (',' if not loop.last)) %}
{% endif %}
{% endfor %}
{% if execute %}
{% set joined = jinja_macro | join ('\n') %}
{{ log(joined, info=True) }}
{% do return(joined) %}
{% endif %}
{{ return(results_list) }}
{% endmacro %}
{% macro bigquery__get_column_names_only(table_name, schema_name, database_name=target.database) %}
{% set query %}
select
lower(column_name)
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 jinja_macro=[] %}
{% set results = run_query(query) %}
{% set results_list = results.columns[0].values() %}}
{% for col in results_list %}
{% if col in ('id','name') %}
{% do jinja_macro.append(' ' ~ col ~ ' as ' ~ table_name ~ '_' ~ col ~ (',' if not loop.last)) %}
{% else %}
{% do jinja_macro.append(' ' ~ col ~ (',' if not loop.last)) %}
{% endif %}
{% endfor %}
{% if execute %}
{% set joined = jinja_macro | join ('\n') %}
{{ log(joined, info=True) }}
{% do return(joined) %}
{% endif %}
{{ log(joined, info=True)}}
{{ return(joined) }}
{% endmacro %}
{% macro get_column_names_only(table_name, schema_name, database_name) -%}
{{ return(adapter.dispatch('get_column_names_only')(table_name, schema_name, database_name)) }}
{%- endmacro %}