|
| 1 | +{% macro teradata_get_current_timestamp() %} |
| 2 | + {%- call statement('current_timestamp', fetch_result=True) -%} |
| 3 | + select cast(current_timestamp(0) as timestamp(0)); |
| 4 | + {% endcall %} |
| 5 | + {{ return(load_result('current_timestamp').table.columns['Current TimeStamp(0)'].values()[0] | trim) }} |
| 6 | +{% endmacro %} |
| 7 | + |
1 | 8 | {% macro teradata__get_views_from_relations(relations) -%} |
2 | 9 | {% set view_relations = [] %} |
3 | 10 | {%- for relation in relations -%} |
|
15 | 22 | {%- endmacro %} |
16 | 23 |
|
17 | 24 | {% macro teradata__create_tmp_tables_of_views(view_relations) -%} |
| 25 | + |
| 26 | + {% set fallback_schema = var("fallback_schema", null) %} |
| 27 | + {{ log("fallback_schema set to : " ~ fallback_schema) }} |
| 28 | + |
18 | 29 | {% set view_tmp_tables_mapping = {} %} |
19 | 30 | {%- for relation in view_relations -%} |
20 | | - {% set temp_relation_for_view = relation.identifier ~ '_tmp_viw_tbl' %} |
| 31 | + {% set timestamp = teradata_get_current_timestamp() %} |
| 32 | + {% set rand = range(1, 100000) | random %} |
| 33 | + {% set uuid = timestamp.replace(":", "").replace("-", "").replace(" ","").replace("+","").replace(".","") ~ rand %} |
| 34 | + {% set temp_relation_for_view = relation.identifier ~ '_tmp_viw_tbl_' ~ uuid %} |
21 | 35 | {% set view_tmp_tables_mapping = view_tmp_tables_mapping.update({relation: temp_relation_for_view}) %} |
22 | 36 | {%- endfor %} |
23 | 37 |
|
24 | | - {{ teradata__drop_tmp_tables_of_views(view_tmp_tables_mapping) }} |
25 | 38 |
|
26 | 39 | {%- for relation, temp_relation_for_view in view_tmp_tables_mapping.items() %} |
| 40 | + {% if fallback_schema==null %} |
| 41 | + {% set schema_name = relation.schema %} |
| 42 | + {% else %} |
| 43 | + {% set schema_name = fallback_schema %} |
| 44 | + {% endif %} |
| 45 | + |
| 46 | + {{ teradata__drop_tmp_tables_of_views(schema_name, temp_relation_for_view) }} |
| 47 | + |
27 | 48 | {% call statement('creating_table_from_view', fetch_result=False) %} |
28 | | - CREATE TABLE "{{ relation.schema }}"."{{ temp_relation_for_view }}" AS (SELECT * FROM "{{ relation.schema }}"."{{ relation.identifier }}") WITH NO DATA; |
| 49 | + CREATE TABLE "{{ schema_name }}"."{{ temp_relation_for_view }}" AS (SELECT * FROM "{{ relation.schema }}"."{{ relation.identifier }}") WITH NO DATA; |
29 | 50 | {% endcall %} |
30 | 51 | load_result('creating_table_from_view') |
31 | 52 | {%- endfor %} |
32 | 53 | {{ return(view_tmp_tables_mapping) }} |
33 | 54 | {%- endmacro %} |
34 | 55 |
|
35 | | -{% macro teradata__drop_tmp_tables_of_views(view_tmp_tables_mapping) -%} |
36 | | - {% for relation, temp_table in view_tmp_tables_mapping.items() %} |
37 | | - {% call statement('drop_existing_table', fetch_result=False) %} |
38 | | - DROP table /*+ IF EXISTS */ "{{ relation.schema }}"."{{ temp_table }}"; |
39 | | - {% endcall %} |
40 | | - load_result('drop_existing_table') |
41 | | - {% endfor %} |
| 56 | +{% macro teradata__drop_tmp_tables_of_views(schema_name, temp_relation_for_view) -%} |
| 57 | + {% call statement('drop_existing_table', fetch_result=False) %} |
| 58 | + DROP table /*+ IF EXISTS */ "{{ schema_name }}"."{{ temp_relation_for_view }}"; |
| 59 | + {% endcall %} |
| 60 | + load_result('drop_existing_table') |
42 | 61 | {%- endmacro %} |
43 | 62 |
|
44 | 63 |
|
|
93 | 112 |
|
94 | 113 | {% set catalog_table = load_result('catalog').table %} |
95 | 114 |
|
96 | | - {{ teradata__drop_tmp_tables_of_views(view_tmp_tables_mapping) }} |
| 115 | + {% set fallback_schema = var("fallback_schema", null) %} |
| 116 | + {%- for relation, temp_relation_for_view in view_tmp_tables_mapping.items() %} |
| 117 | + {% if fallback_schema==null %} |
| 118 | + {% set schema_name = relation.schema %} |
| 119 | + {% else %} |
| 120 | + {% set schema_name = fallback_schema %} |
| 121 | + {% endif %} |
| 122 | + {{ teradata__drop_tmp_tables_of_views(schema_name, temp_relation_for_view) }} |
| 123 | + {%- endfor %} |
97 | 124 |
|
98 | 125 | {{ return(catalog_table) }} |
99 | 126 | {%- endmacro %} |
|
194 | 221 | joined AS ( |
195 | 222 | SELECT |
196 | 223 | columns_transformed.table_database, |
197 | | - columns_transformed.table_schema, |
198 | 224 | {% if view_tmp_tables_mapping is not none and view_tmp_tables_mapping|length > 0 %} |
| 225 | + CASE |
| 226 | + {% for relation, temp_table in view_tmp_tables_mapping.items() %} |
| 227 | + WHEN columns_transformed.table_schema = upper('{{ var("fallback_schema", null) }}') THEN '{{ relation.schema }}' |
| 228 | + {% endfor %} |
| 229 | + ELSE columns_transformed.table_schema |
| 230 | + END as table_schema, |
199 | 231 | CASE |
200 | 232 | {% for relation, temp_table in view_tmp_tables_mapping.items() %} |
201 | 233 | WHEN columns_transformed.table_name = '{{ temp_table }}' THEN '{{ relation.identifier }}' |
202 | 234 | {% endfor %} |
203 | 235 | ELSE columns_transformed.table_name |
204 | 236 | END AS table_name, |
205 | 237 | {% else %} |
| 238 | + columns_transformed.table_schema, |
206 | 239 | columns_transformed.table_name, |
207 | 240 | {% endif %} |
208 | 241 | {% if view_tmp_tables_mapping is not none and view_tmp_tables_mapping|length > 0 %} |
|
259 | 292 | {%- for relation in relations -%} |
260 | 293 | {% if relation.schema and relation.identifier %} |
261 | 294 | ( |
262 | | - upper("table_schema") = upper('{{ relation.schema }}') |
263 | 295 | {% if view_tmp_tables_mapping is not none and view_tmp_tables_mapping|length > 0 %} |
| 296 | + |
264 | 297 | {% set temp_table = view_tmp_tables_mapping[relation] %} |
265 | 298 | {% if not temp_table %} |
| 299 | + upper("table_schema") = upper('{{ relation.schema }}') |
266 | 300 | and upper("table_name") = upper('{{ relation.identifier }}') |
267 | 301 | {% else %} |
268 | | - and upper("table_name") = upper('{{ temp_table }}') |
| 302 | + {% if var("fallback_schema", null) != null %} |
| 303 | + upper("table_schema") = upper('{{ var("fallback_schema", null) }}') |
| 304 | + and upper("table_name") = upper('{{ temp_table }}') |
| 305 | + {% else %} |
| 306 | + upper("table_schema") = upper('{{ relation.schema }}') |
| 307 | + and upper("table_name") = upper('{{ temp_table }}') |
| 308 | + {% endif %} |
269 | 309 | {% endif %} |
270 | 310 | {% else %} |
| 311 | + upper("table_schema") = upper('{{ relation.schema }}') |
271 | 312 | and upper("table_name") = upper('{{ relation.identifier }}') |
272 | 313 | {% endif %} |
273 | 314 | ) |
|
0 commit comments