-
Notifications
You must be signed in to change notification settings - Fork 234
/
adapters.sql
164 lines (140 loc) · 5.22 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
{% macro file_format_clause() %}
{%- set file_format = config.get('file_format', validator=validation.any[basestring]) -%}
{%- if file_format is not none %}
using {{ file_format }}
{%- endif %}
{%- endmacro -%}
{% macro location_clause() %}
{%- set location_root = config.get('location_root', validator=validation.any[basestring]) -%}
{%- set identifier = model['alias'] -%}
{%- if location_root is not none %}
location '{{ location_root }}/{{ identifier }}'
{%- endif %}
{%- endmacro -%}
{% macro comment_clause() %}
{%- set raw_persist_docs = config.get('persist_docs', {}) -%}
{%- if raw_persist_docs is mapping -%}
{%- set raw_relation = raw_persist_docs.get('relation', false) -%}
{%- if raw_relation -%}
comment '{{ model.description | replace("'", "\\'") }}'
{% endif %}
{%- else -%}
{{ exceptions.raise_compiler_error("Invalid value provided for 'persist_docs'. Expected dict but got value: " ~ raw_persist_docs) }}
{% endif %}
{%- endmacro -%}
{% macro partition_cols(label, required=false) %}
{%- set cols = config.get('partition_by', validator=validation.any[list, basestring]) -%}
{%- if cols is not none %}
{%- if cols is string -%}
{%- set cols = [cols] -%}
{%- endif -%}
{{ label }} (
{%- for item in cols -%}
{{ item }}
{%- if not loop.last -%},{%- endif -%}
{%- endfor -%}
)
{%- endif %}
{%- endmacro -%}
{% macro clustered_cols(label, required=false) %}
{%- set cols = config.get('clustered_by', validator=validation.any[list, basestring]) -%}
{%- set buckets = config.get('buckets', validator=validation.any[int]) -%}
{%- if (cols is not none) and (buckets is not none) %}
{%- if cols is string -%}
{%- set cols = [cols] -%}
{%- endif -%}
{{ label }} (
{%- for item in cols -%}
{{ item }}
{%- if not loop.last -%},{%- endif -%}
{%- endfor -%}
) into {{ buckets }} buckets
{%- endif %}
{%- endmacro -%}
{% macro fetch_tbl_properties(relation) -%}
{% call statement('list_properties', fetch_result=True) -%}
SHOW TBLPROPERTIES {{ relation }}
{% endcall %}
{% do return(load_result('list_properties').table) %}
{%- endmacro %}
{#-- We can't use temporary tables with `create ... as ()` syntax #}
{% macro create_temporary_view(relation, sql) -%}
create temporary view {{ relation.include(schema=false) }} as
{{ sql }}
{% endmacro %}
{% macro spark__create_table_as(temporary, relation, sql) -%}
{% if temporary -%}
{{ create_temporary_view(relation, sql) }}
{%- else -%}
{% if config.get('file_format', validator=validation.any[basestring]) == 'delta' %}
create or replace table {{ relation }}
{% else %}
create table {{ relation }}
{% endif %}
{{ file_format_clause() }}
{{ partition_cols(label="partitioned by") }}
{{ clustered_cols(label="clustered by") }}
{{ location_clause() }}
{{ comment_clause() }}
as
{{ sql }}
{%- endif %}
{%- endmacro -%}
{% macro spark__create_view_as(relation, sql) -%}
create or replace view {{ relation }}
{{ comment_clause() }}
as
{{ sql }}
{% endmacro %}
{% macro spark__create_schema(relation) -%}
{%- call statement('create_schema') -%}
create schema if not exists {{relation}}
{% endcall %}
{% endmacro %}
{% macro spark__drop_schema(relation) -%}
{%- call statement('drop_schema') -%}
drop schema if exists {{ relation }} cascade
{%- endcall -%}
{% endmacro %}
{% macro spark__get_columns_in_relation(relation) -%}
{% call statement('get_columns_in_relation', fetch_result=True) %}
describe extended {{ relation }}
{% endcall %}
{% do return(load_result('get_columns_in_relation').table) %}
{% endmacro %}
{% macro spark__list_relations_without_caching(relation) %}
{% call statement('list_relations_without_caching', fetch_result=True) -%}
show table extended in {{ relation }} like '*'
{% endcall %}
{% do return(load_result('list_relations_without_caching').table) %}
{% endmacro %}
{% macro spark__list_schemas(database) -%}
{% call statement('list_schemas', fetch_result=True, auto_begin=False) %}
show databases
{% endcall %}
{{ return(load_result('list_schemas').table) }}
{% endmacro %}
{% macro spark__current_timestamp() -%}
current_timestamp()
{%- endmacro %}
{% macro spark__rename_relation(from_relation, to_relation) -%}
{% call statement('rename_relation') -%}
{% if not from_relation.type %}
{% do exceptions.raise_database_error("Cannot rename a relation with a blank type: " ~ from_relation.identifier) %}
{% elif from_relation.type in ('table') %}
alter table {{ from_relation }} rename to {{ to_relation }}
{% elif from_relation.type == 'view' %}
alter view {{ from_relation }} rename to {{ to_relation }}
{% else %}
{% do exceptions.raise_database_error("Unknown type '" ~ from_relation.type ~ "' for relation: " ~ from_relation.identifier) %}
{% endif %}
{%- endcall %}
{% endmacro %}
{% macro spark__drop_relation(relation) -%}
{% call statement('drop_relation', auto_begin=False) -%}
drop {{ relation.type }} if exists {{ relation }}
{%- endcall %}
{% endmacro %}
{% macro spark__generate_database_name(custom_database_name=none, node=none) -%}
{% do return(None) %}
{%- endmacro %}