-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate_description_model.sql
125 lines (90 loc) · 3.93 KB
/
create_description_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
{%- macro create_description_model(
resource_type=["model"]
, show_resource_type=True
, files=[]
, exclude_files=[]
) -%}
{%- if execute -%}
{%- set rows_list = metalog.get_rows(resource_type, files, exclude_files) -%}
{%- if rows_list | length == 0 -%}
{{ exceptions.raise_compiler_error("No description found for the provided parameters\nPlease check the descriptions of your resources") }}
{%- endif -%}
{%- for row in rows_list -%}
select "{{ row[0] }}" as resource_name
{%- if show_resource_type -%}
, "{{ row[1] }}" as resource_type
{%- endif -%}
, "{{ row[2] }}" as resource_description
, "{{ row[3] }}" as column_name
, "{{ row[4] }}" as columns_description
{% if not loop.last %}
union all
{% endif %}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{%- macro get_rows(
resource_type_list
, 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 -%}
{%- for column in node.columns.values() -%}
{%- set node_columns_list = [] -%}
{{ node_columns_list.append(node.unique_id.split(".")[2]) }}
{{ node_columns_list.append(node.resource_type) }}
{{ node_columns_list.append(node.description) }}
{{ node_columns_list.append(column.name) }}
{{ node_columns_list.append(column.description) }}
{{ rows_list.append(node_columns_list) }}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- for source in graph.sources.values() if "source" 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 -%}
{%- for column in source.columns.values() -%}
{%- set source_columns_list = [] -%}
{{ source_columns_list.append(source.unique_id.split(".")[2]) }}
{{ source_columns_list.append(source.resource_type) }}
{{ source_columns_list.append(source.description) }}
{{ source_columns_list.append(column.name) }}
{{ source_columns_list.append(column.description) }}
{{ rows_list.append(source_columns_list) }}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{{ return(rows_list) }}
{%- endmacro -%}