-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate_metadata_model.sql
247 lines (175 loc) · 8.21 KB
/
create_metadata_model.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
{%- macro create_metadata_model(
metadata
, granularity=[]
, resource_type=["model"]
, show_resource_type=True
, undefined="Undefined"
, undefined_as_null=False
, files=[]
, exclude_files=[]
) -%}
{%- if execute -%}
{%- set rows_list = metalog.get_metadata(metadata, granularity, resource_type, undefined, files, exclude_files) -%}
{%- if rows_list | length == 0 -%}
{{ exceptions.raise_compiler_error("No metadata found for the provided parameters\nPlease check the metadata and resource type provided") }}
{%- endif -%}
{%- for row in rows_list -%}
select "{{ row[0] }}" as resource_name
{%- if show_resource_type -%}
, "{{ row[1] }}" as resource_type
{%- endif -%}
{%- for i in range(metadata | length) -%}
{%- if undefined_as_null and row[i+2] == undefined -%}
, null as {{metadata[i]}}
{%- else -%}
, "{{ row[i+2] }}" as {{metadata[i]}}
{%- endif -%}
{%- endfor -%}
{% if not loop.last %}
union all
{% endif %}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{%- macro get_metadata(
metadata_list
, granularity_list
, resource_type_list
, undefined
, files_list
, exclude_files_list
) -%}
{% set re = modules.re %}
{%- set rows_list = [] -%}
{%- for node in graph.nodes.values() if node.resource_type in resource_type_list -%}
{# "Check if node is in the provided files" #}
{%- set valid_files = [] -%}
{%- if files_list -%}
{%- for file in files_list if re.match(file, node.original_file_path, re.IGNORECASE) -%}
{%- if exclude_files_list -%}
{%- for file_exclude in exclude_files_list if not re.match(file_exclude, node.original_file_path, re.IGNORECASE) -%}
{{ valid_files.append(1) }}
{%- endfor -%}
{%- else -%}
{{ valid_files.append(1) }}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{ valid_files.append(1) }}
{%- endif -%}
{%- if valid_files -%}
{%- set granularity_values_list = [] -%}
{%- for metadata in granularity_list -%}
{%- set values_list = [] -%}
{# "If the provided metadata in granularity is a string" #}
{# "just append the string into values_list" #}
{%- if node.meta[metadata] is string -%}
{{ values_list.append(node.meta[metadata]) }}
{# "If the provided metadata in granularity is a list" #}
{# "then append each value into values_list" #}
{%- else -%}
{%- for item in node.meta[metadata] -%}
{{ values_list.append(item) }}
{%- endfor -%}
{%- endif -%}
{# "If the model has no metadata from the granularity list" #}
{# "append the undefined argument string" #}
{%- if values_list == [] -%}
{%- set values_list = [undefined] -%}
{%- endif -%}
{{ granularity_values_list.append(values_list) }}
{%- endfor -%}
{%- set all_combinations = metalog.combinations(granularity_values_list) -%}
{# "The if block below is used to get a length of 1 for all_combinations
if there is none combination" #}
{%- if all_combinations == [] -%}
{%- set all_combinations = [[]] -%}
{%- endif -%}
{%- for c in range(all_combinations | length) -%}
{%- set node_row = [] -%}
{%- set unique_id_splitted = node.unique_id.split(".") -%}
{{ node_row.append(unique_id_splitted[2]) }}
{{ node_row.append(node.resource_type) }}
{%- for metadata in metadata_list -%}
{%- if not node.meta[metadata] or not node.meta -%}
{{ node_row.append(undefined) }}
{%- else -%}
{%- if metadata not in granularity_list -%}
{{ node_row.append(node.meta[metadata] | string) }}
{%- else -%}
{%- set idx = granularity_list.index(metadata) -%}
{{ node_row.append(all_combinations[c][idx] | string) }}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ rows_list.append(node_row) }}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- for source in graph.sources.values() if source.resource_type in resource_type_list -%}
{# "Check if source is in the provided files" #}
{%- set valid_files = [] -%}
{%- if files_list -%}
{%- for file in files_list if re.match(file, source.original_file_path, re.IGNORECASE) -%}
{%- if exclude_files_list -%}
{%- for file_exclude in exclude_files_list if not re.match(file_exclude, source.original_file_path, re.IGNORECASE) -%}
{{ valid_files.append(1) }}
{%- endfor -%}
{%- else -%}
{{ valid_files.append(1) }}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{ valid_files.append(1) }}
{%- endif -%}
{%- if valid_files -%}
{%- set granularity_values_list = [] -%}
{%- for metadata in granularity_list -%}
{%- set values_list = [] -%}
{# "If the provided metadata in granularity is a string" #}
{# "just append the string into values_list" #}
{%- if source.meta[metadata] is string -%}
{{ values_list.append(source.meta[metadata]) }}
{# "If the provided metadata in granularity is a list" #}
{# "then append each value into values_list" #}
{%- else -%}
{%- for item in source.meta[metadata] -%}
{{ values_list.append(item) }}
{%- endfor -%}
{%- endif -%}
{# "If the model has no metadata from the granularity list" #}
{# "append the undefined argument string" #}
{%- if values_list == [] -%}
{%- set values_list = [undefined] -%}
{%- endif -%}
{{ granularity_values_list.append(values_list) }}
{%- endfor -%}
{%- set all_combinations = metalog.combinations(granularity_values_list) -%}
{# "The if block below is used to get a length of 1 for all_combinations
if there is none combination" #}
{%- if all_combinations == [] -%}
{%- set all_combinations = [[]] -%}
{%- endif -%}
{%- for c in range(all_combinations | length) -%}
{%- set source_row = [] -%}
{%- set unique_id_splitted = source.unique_id.split(".") -%}
{{ source_row.append(unique_id_splitted[2]) }}
{{ source_row.append(source.resource_type) }}
{%- for metadata in metadata_list -%}
{%- if not source.meta[metadata] or not source.meta -%}
{{ source_row.append(undefined) }}
{%- else -%}
{%- if metadata not in granularity_list -%}
{{ source_row.append(source.meta[metadata] | string) }}
{%- else -%}
{%- set idx = granularity_list.index(metadata) -%}
{{ source_row.append(all_combinations[c][idx] | string) }}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ rows_list.append(source_row) }}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{{ return(rows_list) }}
{%- endmacro -%}