-
Notifications
You must be signed in to change notification settings - Fork 161
/
adapters.sql
224 lines (160 loc) · 6.2 KB
/
adapters.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{% macro partition_by(partition_config) -%}
{%- if partition_config is none -%}
{% do return('') %}
{%- elif partition_config.data_type | lower in ('date','timestamp','datetime') -%}
partition by {{ partition_config.render() }}
{%- elif partition_config.data_type | lower in ('int64') -%}
{%- set range = partition_config.range -%}
partition by range_bucket(
{{ partition_config.field }},
generate_array({{ range.start}}, {{ range.end }}, {{ range.interval }})
)
{%- endif -%}
{%- endmacro -%}
{% macro cluster_by(raw_cluster_by) %}
{%- if raw_cluster_by is not none -%}
cluster by {% if raw_cluster_by is string -%}
{% set raw_cluster_by = [raw_cluster_by] %}
{%- endif -%}
{%- for cluster in raw_cluster_by -%}
{{ cluster }}
{%- if not loop.last -%}, {% endif -%}
{%- endfor -%}
{% endif %}
{%- endmacro -%}
{% macro bigquery_options(opts) %}
{% set options -%}
OPTIONS({% for opt_key, opt_val in opts.items() %}
{{ opt_key }}={{ opt_val }}{{ "," if not loop.last }}
{% endfor %})
{%- endset %}
{%- do return(options) -%}
{%- endmacro -%}
{% macro bigquery_table_options(config, node, temporary) %}
{% set opts = adapter.get_table_options(config, node, temporary) %}
{%- do return(bigquery_options(opts)) -%}
{%- endmacro -%}
{% macro bigquery__create_table_as(temporary, relation, sql) -%}
{%- set raw_partition_by = config.get('partition_by', none) -%}
{%- set raw_cluster_by = config.get('cluster_by', none) -%}
{%- set sql_header = config.get('sql_header', none) -%}
{%- set partition_config = adapter.parse_partition_by(raw_partition_by) -%}
{{ sql_header if sql_header is not none }}
create or replace table {{ relation }}
{{ partition_by(partition_config) }}
{{ cluster_by(raw_cluster_by) }}
{{ bigquery_table_options(config, model, temporary) }}
as (
{{ sql }}
);
{%- endmacro -%}
{% macro bigquery_view_options(config, node) %}
{% set opts = adapter.get_view_options(config, node) %}
{%- do return(bigquery_options(opts)) -%}
{%- endmacro -%}
{% macro bigquery__create_view_as(relation, sql) -%}
{%- set sql_header = config.get('sql_header', none) -%}
{{ sql_header if sql_header is not none }}
create or replace view {{ relation }}
{{ bigquery_view_options(config, model) }}
as {{ sql }};
{% endmacro %}
{% macro bigquery__create_schema(relation) -%}
{{ adapter.create_schema(relation) }}
{% endmacro %}
{% macro bigquery__drop_schema(relation) -%}
{{ adapter.drop_schema(relation) }}
{% endmacro %}
{% macro bigquery__drop_relation(relation) -%}
{% call statement('drop_relation') -%}
drop {{ relation.type }} if exists {{ relation }}
{%- endcall %}
{% endmacro %}
{% macro bigquery__get_columns_in_relation(relation) -%}
{{ return(adapter.get_columns_in_relation(relation)) }}
{% endmacro %}
{% macro bigquery__list_relations_without_caching(schema_relation) -%}
{{ return(adapter.list_relations_without_caching(schema_relation)) }}
{%- endmacro %}
{% macro bigquery__current_timestamp() -%}
CURRENT_TIMESTAMP()
{%- endmacro %}
{% macro bigquery__snapshot_string_as_time(timestamp) -%}
{%- set result = 'TIMESTAMP("' ~ timestamp ~ '")' -%}
{{ return(result) }}
{%- endmacro %}
{% macro bigquery__list_schemas(database) -%}
{{ return(adapter.list_schemas(database)) }}
{% endmacro %}
{% macro bigquery__check_schema_exists(information_schema, schema) %}
{{ return(adapter.check_schema_exists(information_schema.database, schema)) }}
{% endmacro %}
{#-- relation-level macro is not implemented. This is handled in the CTAs statement #}
{% macro bigquery__persist_docs(relation, model, for_relation, for_columns) -%}
{% if for_columns and config.persist_column_docs() and model.columns %}
{% do alter_column_comment(relation, model.columns) %}
{% endif %}
{% endmacro %}
{% macro bigquery__alter_column_comment(relation, column_dict) -%}
{% do adapter.update_columns(relation, column_dict) %}
{% endmacro %}
{% macro bigquery__rename_relation(from_relation, to_relation) -%}
{% do adapter.rename_relation(from_relation, to_relation) %}
{% endmacro %}
{% macro bigquery__alter_relation_add_columns(relation, add_columns) %}
{% set sql -%}
alter {{ relation.type }} {{ relation }}
{% for column in add_columns %}
add column {{ column.name }} {{ column.data_type }}{{ ',' if not loop.last }}
{% endfor %}
{%- endset -%}
{{ return(run_query(sql)) }}
{% endmacro %}
{% macro bigquery__alter_relation_drop_columns(relation, drop_columns) %}
{% set sql -%}
alter {{ relation.type }} {{ relation }}
{% for column in drop_columns %}
drop column {{ column.name }}{{ ',' if not loop.last }}
{% endfor %}
{%- endset -%}
{{ return(run_query(sql)) }}
{% endmacro %}
{% macro bigquery__alter_column_type(relation, column_name, new_column_type) -%}
{#-- Changing a column's data type using a query requires you to scan the entire table.
The query charges can be significant if the table is very large.
https://cloud.google.com/bigquery/docs/manually-changing-schemas#changing_a_columns_data_type
#}
{% set relation_columns = get_columns_in_relation(relation) %}
{% set sql %}
select
{%- for col in relation_columns -%}
{% if col.column == column_name %}
CAST({{ col.quoted }} AS {{ new_column_type }}) AS {{ col.quoted }}
{%- else %}
{{ col.quoted }}
{%- endif %}
{%- if not loop.last %},{% endif -%}
{%- endfor %}
from {{ relation }}
{% endset %}
{% call statement('alter_column_type') %}
{{ create_table_as(False, relation, sql)}}
{%- endcall %}
{% endmacro %}
{% macro bigquery__test_unique(model, column_name) %}
with dbt_test__target as (
select {{ column_name }} as unique_field
from {{ model }}
where {{ column_name }} is not null
)
select
unique_field,
count(*) as n_records
from dbt_test__target
group by unique_field
having count(*) > 1
{% endmacro %}
{% macro bigquery__upload_file(local_file_path, database, table_schema, table_name) %}
{{ log("kwargs: " ~ kwargs) }}
{% do adapter.upload_file(local_file_path, database, table_schema, table_name, kwargs=kwargs) %}
{% endmacro %}