-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathadapters.sql
259 lines (227 loc) · 9.33 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
{% macro postgres__create_table_as(temporary, relation, sql) -%}
{%- set unlogged = config.get('unlogged', default=false) -%}
{%- set sql_header = config.get('sql_header', none) -%}
{{ sql_header if sql_header is not none }}
create {% if temporary -%}
temporary
{%- elif unlogged -%}
unlogged
{%- endif %} table {{ relation }}
{% set contract_config = config.get('contract') %}
{% if contract_config.enforced %}
{{ get_assert_columns_equivalent(sql) }}
{% endif -%}
{% if contract_config.enforced and (not temporary) -%}
{{ get_table_columns_and_constraints() }} ;
insert into {{ relation }} (
{{ adapter.dispatch('get_column_names', 'dbt')() }}
)
{%- set sql = get_select_subquery(sql) %}
{% else %}
as
{% endif %}
(
{{ sql }}
);
{%- endmacro %}
{% macro postgres__get_create_index_sql(relation, index_dict) -%}
{%- set index_config = adapter.parse_index(index_dict) -%}
{%- set comma_separated_columns = ", ".join(index_config.columns) -%}
{%- set index_name = index_config.render(relation) -%}
create {% if index_config.unique -%}
unique
{%- endif %} index if not exists
"{{ index_name }}"
on {{ relation }} {% if index_config.type -%}
using {{ index_config.type }}
{%- endif %}
({{ comma_separated_columns }})
{%- endmacro %}
{% macro postgres__create_schema(relation) -%}
{% if relation.database -%}
{{ adapter.verify_database(relation.database) }}
{%- endif -%}
{%- call statement('create_schema') -%}
create schema if not exists {{ relation.without_identifier().include(database=False) }}
{%- endcall -%}
{% endmacro %}
{% macro postgres__drop_schema(relation) -%}
{% if relation.database -%}
{{ adapter.verify_database(relation.database) }}
{%- endif -%}
{%- call statement('drop_schema') -%}
drop schema if exists {{ relation.without_identifier().include(database=False) }} cascade
{%- endcall -%}
{% endmacro %}
{% macro postgres__get_columns_in_relation(relation) -%}
{% call statement('get_columns_in_relation', fetch_result=True) %}
select
column_name,
data_type,
character_maximum_length,
numeric_precision,
numeric_scale
from {{ relation.information_schema('columns') }}
where table_name = '{{ relation.identifier }}'
{% if relation.schema %}
and table_schema = '{{ relation.schema }}'
{% endif %}
order by ordinal_position
{% endcall %}
{% set table = load_result('get_columns_in_relation').table %}
{{ return(sql_convert_columns_in_relation(table)) }}
{% endmacro %}
{% macro postgres__list_relations_without_caching(schema_relation) %}
{% call statement('list_relations_without_caching', fetch_result=True) -%}
select
'{{ schema_relation.database }}' as database,
tablename as name,
schemaname as schema,
'table' as type
from pg_tables
where schemaname ilike '{{ schema_relation.schema }}'
union all
select
'{{ schema_relation.database }}' as database,
viewname as name,
schemaname as schema,
'view' as type
from pg_views
where schemaname ilike '{{ schema_relation.schema }}'
union all
select
'{{ schema_relation.database }}' as database,
matviewname as name,
schemaname as schema,
'materialized_view' as type
from pg_matviews
where schemaname ilike '{{ schema_relation.schema }}'
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
{% endmacro %}
{% macro postgres__information_schema_name(database) -%}
{% if database_name -%}
{{ adapter.verify_database(database_name) }}
{%- endif -%}
information_schema
{%- endmacro %}
{% macro postgres__list_schemas(database) %}
{% if database -%}
{{ adapter.verify_database(database) }}
{%- endif -%}
{% call statement('list_schemas', fetch_result=True, auto_begin=False) %}
select distinct nspname from pg_namespace
{% endcall %}
{{ return(load_result('list_schemas').table) }}
{% endmacro %}
{% macro postgres__check_schema_exists(information_schema, schema) -%}
{% if information_schema.database -%}
{{ adapter.verify_database(information_schema.database) }}
{%- endif -%}
{% call statement('check_schema_exists', fetch_result=True, auto_begin=False) %}
select count(*) from pg_namespace where nspname = '{{ schema }}'
{% endcall %}
{{ return(load_result('check_schema_exists').table) }}
{% endmacro %}
{#
Postgres tables have a maximum length of 63 characters, anything longer is silently truncated.
Temp and backup relations add a lot of extra characters to the end of table names to ensure uniqueness.
To prevent this going over the character limit, the base_relation name is truncated to ensure
that name + suffix + uniquestring is < 63 characters.
#}
{% macro postgres__make_relation_with_suffix(base_relation, suffix, dstring) %}
{% if dstring %}
{% set dt = modules.datetime.datetime.now() %}
{% set dtstring = dt.strftime("%H%M%S%f") %}
{% set suffix = suffix ~ dtstring %}
{% endif %}
{% set suffix_length = suffix|length %}
{% set relation_max_name_length = base_relation.relation_max_name_length() %}
{% if suffix_length > relation_max_name_length %}
{% do exceptions.raise_compiler_error('Relation suffix is too long (' ~ suffix_length ~ ' characters). Maximum length is ' ~ relation_max_name_length ~ ' characters.') %}
{% endif %}
{% set identifier = base_relation.identifier[:relation_max_name_length - suffix_length] ~ suffix %}
{{ return(base_relation.incorporate(path={"identifier": identifier })) }}
{% endmacro %}
{% macro postgres__make_intermediate_relation(base_relation, suffix) %}
{{ return(postgres__make_relation_with_suffix(base_relation, suffix, dstring=False)) }}
{% endmacro %}
{% macro postgres__make_temp_relation(base_relation, suffix) %}
{% set temp_relation = postgres__make_relation_with_suffix(base_relation, suffix, dstring=True) %}
{{ return(temp_relation.incorporate(path={"schema": none,
"database": none})) }}
{% endmacro %}
{% macro postgres__make_backup_relation(base_relation, backup_relation_type, suffix) %}
{% set backup_relation = postgres__make_relation_with_suffix(base_relation, suffix, dstring=False) %}
{{ return(backup_relation.incorporate(type=backup_relation_type)) }}
{% endmacro %}
{#
By using dollar-quoting like this, users can embed anything they want into their comments
(including nested dollar-quoting), as long as they do not use this exact dollar-quoting
label. It would be nice to just pick a new one but eventually you do have to give up.
#}
{% macro postgres_escape_comment(comment) -%}
{% if comment is not string %}
{% do exceptions.raise_compiler_error('cannot escape a non-string: ' ~ comment) %}
{% endif %}
{%- set magic = '$dbt_comment_literal_block$' -%}
{%- if magic in comment -%}
{%- do exceptions.raise_compiler_error('The string ' ~ magic ~ ' is not allowed in comments.') -%}
{%- endif -%}
{{ magic }}{{ comment }}{{ magic }}
{%- endmacro %}
{% macro postgres__alter_relation_comment(relation, comment) %}
{% set escaped_comment = postgres_escape_comment(comment) %}
{% if relation.type == 'materialized_view' -%}
{% set relation_type = "materialized view" %}
{%- else -%}
{%- set relation_type = relation.type -%}
{%- endif -%}
comment on {{ relation_type }} {{ relation }} is {{ escaped_comment }};
{% endmacro %}
{% macro postgres__alter_column_comment(relation, column_dict) %}
{% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %}
{% for column_name in column_dict if (column_name in existing_columns) %}
{% set comment = column_dict[column_name]['description'] %}
{% set escaped_comment = postgres_escape_comment(comment) %}
comment on column {{ relation }}.{{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }} is {{ escaped_comment }};
{% endfor %}
{% endmacro %}
{%- macro postgres__get_show_grant_sql(relation) -%}
select grantee, privilege_type
from {{ relation.information_schema('role_table_grants') }}
where grantor = current_role
and grantee != current_role
and table_schema = '{{ relation.schema }}'
and table_name = '{{ relation.identifier }}'
{%- endmacro -%}
{% macro postgres__copy_grants() %}
{{ return(False) }}
{% endmacro %}
{% macro postgres__get_show_indexes_sql(relation) %}
select
i.relname as name,
m.amname as method,
ix.indisunique as "unique",
array_to_string(array_agg(a.attname), ',') as column_names
from pg_index ix
join pg_class i
on i.oid = ix.indexrelid
join pg_am m
on m.oid=i.relam
join pg_class t
on t.oid = ix.indrelid
join pg_namespace n
on n.oid = t.relnamespace
join pg_attribute a
on a.attrelid = t.oid
and a.attnum = ANY(ix.indkey)
where t.relname = '{{ relation.identifier }}'
and n.nspname = '{{ relation.schema }}'
and t.relkind in ('r', 'm')
group by 1, 2, 3
order by 1, 2, 3
{% endmacro %}
{%- macro postgres__get_drop_index_sql(relation, index_name) -%}
drop index if exists "{{ relation.schema }}"."{{ index_name }}"
{%- endmacro -%}