forked from dbt-labs/dbt-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_model_import_ctes.sql
408 lines (302 loc) · 14.2 KB
/
generate_model_import_ctes.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
{% macro generate_model_import_ctes(model_name, leading_commas = False) %}
{{ return(adapter.dispatch('generate_model_import_ctes', 'codegen')(model_name, leading_commas)) }}
{% endmacro %}
{% macro default__generate_model_import_ctes(model_name, leading_commas) %}
{%- if execute -%}
{%- set nodes = graph.nodes.values() -%}
{%- set model = (nodes
| selectattr('name', 'equalto', model_name)
| selectattr('resource_type', 'equalto', 'model')
| list).pop() -%}
{%- set model_raw_sql = model.raw_sql or model.raw_code -%}
{%- else -%}
{%- set model_raw_sql = '' -%}
{%- endif -%}
{#-
REGEX Explanations
# with_regex
- matches (start of file followed by anything then whitespace
or whitespace
or a comma) followed by the word with then a space
# from_ref
- matches (from or join) followed by some spaces and then {{ref(<something>)}}
# from_source
- matches (from or join) followed by some spaces and then {{source(<something>,<something_else>)}}
# from_var_1
- matches (from or join) followed by some spaces and then {{var(<something>)}}
# from_var_2
- matches (from or join) followed by some spaces and then {{var(<something>,<something_else>)}}
# from_table_1
- matches (from or join) followed by some spaces and then <something>.<something_else>
where each <something> is enclosed by (` or [ or " or ' or nothing)
# from_table_2
- matches (from or join) followed by some spaces and then <something>.<something_else>.<something_different>
where each <something> is enclosed by (` or [ or " or ' or nothing)
# from_table_3
- matches (from or join) followed by some spaces and then <something>
where <something> is enclosed by (` or [ or " or ')
# config block
- matches the start of the file followed by anything and then {{config(<something>)}}
-#}
{%- set re = modules.re -%}
{%- set with_regex = '(?i)(?s)(^.*\s*|\s+|,)with\s' -%}
{%- set does_raw_sql_contain_cte = re.search(with_regex, model_raw_sql) -%}
{%- set from_regexes = {
'from_ref':
'(?ix)
# first matching group
# from or join followed by at least 1 whitespace character
(from|join)\s+
# second matching group
# opening {{, 0 or more whitespace character(s), ref, 0 or more whitespace character(s), an opening parenthesis, 0 or more whitespace character(s), 1 or 0 quotation mark
({{\s*ref\s*\(\s*[\'\"]?)
# third matching group
# at least 1 of anything except a parenthesis or quotation mark
([^)\'\"]+)
# fourth matching group
# 1 or 0 quotation mark, 0 or more whitespace character(s)
([\'\"]?\s*)
# fifth matching group
# a closing parenthesis, 0 or more whitespace character(s), closing }}
(\)\s*}})
',
'from_source':
'(?ix)
# first matching group
# from or join followed by at least 1 whitespace character
(from|join)\s+
# second matching group
# opening {{, 0 or more whitespace character(s), source, 0 or more whitespace character(s), an opening parenthesis, 0 or more whitespace character(s), 1 or 0 quotation mark
({{\s*source\s*\(\s*[\'\"]?)
# third matching group
# at least 1 of anything except a parenthesis or quotation mark
([^)\'\"]+)
# fourth matching group
# 1 or 0 quotation mark, 0 or more whitespace character(s)
([\'\"]?\s*)
# fifth matching group
# a comma
(,)
# sixth matching group
# 0 or more whitespace character(s), 1 or 0 quotation mark
(\s*[\'\"]?)
# seventh matching group
# at least 1 of anything except a parenthesis or quotation mark
([^)\'\"]+)
# eighth matching group
# 1 or 0 quotation mark, 0 or more whitespace character(s)
([\'\"]?\s*)
# ninth matching group
# a closing parenthesis, 0 or more whitespace character(s), closing }}
(\)\s*}})
',
'from_var_1':
'(?ix)
# first matching group
# from or join followed by at least 1 whitespace character
(from|join)\s+
# second matching group
# opening {{, 0 or more whitespace character(s), var, 0 or more whitespace character(s), an opening parenthesis, 0 or more whitespace character(s), 1 or 0 quotation mark
({{\s*var\s*\(\s*[\'\"]?)
# third matching group
# at least 1 of anything except a parenthesis or quotation mark
([^)\'\"]+)
# fourth matching group
# 1 or 0 quotation mark, 0 or more whitespace character(s)
([\'\"]?\s*)
# fifth matching group
# a closing parenthesis, 0 or more whitespace character(s), closing }}
(\)\s*}})
',
'from_var_2':
'(?ix)
# first matching group
# from or join followed by at least 1 whitespace character
(from|join)\s+
# second matching group
# opening {{, 0 or more whitespace character(s), var, 0 or more whitespace character(s), an opening parenthesis, 0 or more whitespace character(s), 1 or 0 quotation mark
({{\s*var\s*\(\s*[\'\"]?)
# third matching group
# at least 1 of anything except a parenthesis or quotation mark
([^)\'\"]+)
# fourth matching group
# 1 or 0 quotation mark, 0 or more whitespace character(s)
([\'\"]?\s*)
# fifth matching group
# a comma
(,)
# sixth matching group
# 0 or more whitespace character(s), 1 or 0 quotation mark
(\s*[\'\"]?)
# seventh matching group
# at least 1 of anything except a parenthesis or quotation mark
([^)\'\"]+)
# eighth matching group
# 1 or 0 quotation mark, 0 or more whitespace character(s)
([\'\"]?\s*)
# ninth matching group
# a closing parenthesis, 0 or more whitespace character(s), closing }}
(\)\s*}})
',
'from_table_1':
'(?ix)
# first matching group
# from or join followed by at least 1 whitespace character
(from|join)\s+
# second matching group
# 1 or 0 of (opening bracket, backtick, or quotation mark)
([\[`\"\']?)
# third matching group
# at least 1 word character
(\w+)
# fouth matching group
# 1 or 0 of (closing bracket, backtick, or quotation mark)
([\]`\"\']?)
# fifth matching group
# a period
(\.)
# sixth matching group
# 1 or 0 of (opening bracket, backtick, or quotation mark)
([\[`\"\']?)
# seventh matching group
# at least 1 word character
(\w+)
# eighth matching group
# 1 or 0 of (closing bracket, backtick, or quotation mark) folowed by a whitespace character or end of string
([\]`\"\']?)(?=\s|$)
',
'from_table_2':
'(?ix)
# first matching group
# from or join followed by at least 1 whitespace character
(from|join)\s+
# second matching group
# 1 or 0 of (opening bracket, backtick, or quotation mark)
([\[`\"\']?)
# third matching group
# at least 1 word character
(\w+)
# fouth matching group
# 1 or 0 of (closing bracket, backtick, or quotation mark)
([\]`\"\']?)
# fifth matching group
# a period
(\.)
# sixth matching group
# 1 or 0 of (opening bracket, backtick, or quotation mark)
([\[`\"\']?)
# seventh matching group
# at least 1 word character
(\w+)
# eighth matching group
# 1 or 0 of (closing bracket, backtick, or quotation mark)
([\]`\"\']?)
# ninth matching group
# a period
(\.)
# tenth matching group
# 1 or 0 of (closing bracket, backtick, or quotation mark)
([\[`\"\']?)
# eleventh matching group
# at least 1 word character
(\w+)
# twelfth matching group
# 1 or 0 of (closing bracket, backtick, or quotation mark) folowed by a whitespace character or end of string
([\]`\"\']?)(?=\s|$)
',
'from_table_3':
'(?ix)
# first matching group
# from or join followed by at least 1 whitespace character
(from|join)\s+
# second matching group
# 1 or 0 of (opening bracket, backtick, or quotation mark)
([\[`\"\'])
# third matching group
# at least 1 word character or space
([\w ]+)
# fourth matching group
# 1 or 0 of (closing bracket, backtick, or quotation mark) folowed by a whitespace character or end of string
([\]`\"\'])(?=\s|$)
',
'config_block':'(?i)(?s)^.*{{\s*config\s*\([^)]+\)\s*}}'
} -%}
{%- set from_list = [] -%}
{%- set config_list = [] -%}
{%- set ns = namespace(model_sql = model_raw_sql) -%}
{%- for regex_name, regex_pattern in from_regexes.items() -%}
{%- set all_regex_matches = re.findall(regex_pattern, model_raw_sql) -%}
{%- for match in all_regex_matches -%}
{%- if regex_name == 'config_block' -%}
{%- set match_tuple = (match|trim, regex_name) -%}
{%- do config_list.append(match_tuple) -%}
{%- elif regex_name == 'from_source' -%}
{%- set full_from_clause = match[1:]|join|trim -%}
{%- set cte_name = 'source_' + match[6]|lower -%}
{%- set match_tuple = (cte_name, full_from_clause, regex_name) -%}
{%- do from_list.append(match_tuple) -%}
{%- elif regex_name == 'from_table_1' -%}
{%- set full_from_clause = match[1:]|join()|trim -%}
{%- set cte_name = match[2]|lower + '_' + match[6]|lower -%}
{%- set match_tuple = (cte_name, full_from_clause, regex_name) -%}
{%- do from_list.append(match_tuple) -%}
{%- elif regex_name == 'from_table_2' -%}
{%- set full_from_clause = match[1:]|join()|trim -%}
{%- set cte_name = match[2]|lower + '_' + match[6]|lower + '_' + match[10]|lower -%}
{%- set match_tuple = (cte_name, full_from_clause, regex_name) -%}
{%- do from_list.append(match_tuple) -%}
{%- else -%}
{%- set full_from_clause = match[1:]|join|trim -%}
{%- set cte_name = match[2]|trim|lower -%}
{%- set match_tuple = (cte_name, full_from_clause, regex_name) -%}
{%- do from_list.append(match_tuple) -%}
{%- endif -%}
{%- endfor -%}
{%- if regex_name == 'config_block' -%}
{%- elif regex_name == 'from_source' -%}
{%- set ns.model_sql = re.sub(regex_pattern, '\g<1> source_\g<7>', ns.model_sql) -%}
{%- elif regex_name == 'from_table_1' -%}
{%- set ns.model_sql = re.sub(regex_pattern, '\g<1> \g<3>_\g<7>', ns.model_sql) -%}
{%- elif regex_name == 'from_table_2' -%}
{%- set ns.model_sql = re.sub(regex_pattern, '\g<1> \g<3>_\g<7>_\g<11>', ns.model_sql) -%}
{%- else -%}
{%- set ns.model_sql = re.sub(regex_pattern, '\g<1> \g<3>', ns.model_sql) -%}
{% endif %}
{%- endfor -%}
{%- if from_list|length > 0 -%}
{%- set model_import_ctes -%}
{%- for config_obj in config_list -%}
{%- set ns.model_sql = ns.model_sql|replace(config_obj[0], '') -%}
{{ config_obj[0] }}
{% endfor -%}
{%- for from_obj in from_list|unique|sort -%}
{%- if loop.first -%}with {% else -%}{%- if leading_commas -%},{%- endif -%}{%- endif -%}{{ from_obj[0] }} as (
select * from {{ from_obj[1] }}
{%- if from_obj[2] == 'from_source' and from_list|length > 1 %}
-- CAUTION: It's best practice to create staging layer for raw sources
{%- elif from_obj[2] == 'from_table_1' or from_obj[2] == 'from_table_2' or from_obj[2] == 'from_table_3' %}
-- CAUTION: It's best practice to use the ref or source function instead of a direct reference
{%- elif from_obj[2] == 'from_var_1' or from_obj[2] == 'from_var_2' %}
-- CAUTION: It's best practice to use the ref or source function instead of a var
{%- endif %}
){%- if ((loop.last and does_raw_sql_contain_cte) or (not loop.last)) and not leading_commas -%},{%- endif %}
{% endfor -%}
{%- if does_raw_sql_contain_cte -%}
{%- if leading_commas -%}
{%- set replace_with = '\g<1>,' -%}
{%- else -%}
{%- set replace_with = '\g<1>' -%}
{%- endif -%}
{{ re.sub(with_regex, replace_with, ns.model_sql, 1)|trim }}
{%- else -%}
{{ ns.model_sql|trim }}
{%- endif -%}
{%- endset -%}
{%- else -%}
{% set model_import_ctes = model_raw_sql %}
{%- endif -%}
{%- if execute -%}
{{ print(model_import_ctes) }}
{% do return(model_import_ctes) %}
{% endif %}
{% endmacro %}