|
12 | 12 | {% else %}
|
13 | 13 | {%- set target_relation = this -%}
|
14 | 14 | {% endif %}
|
| 15 | + |
15 | 16 | {%- set existing_relation = load_relation(this) -%}
|
16 | 17 | {%- set tmp_relation = make_temp_relation(target_relation) -%}
|
17 | 18 |
|
|
20 | 21 |
|
21 | 22 | {%- set min_max_ranks = automate_dv.get_min_max_ranks(rank_column, rank_source_models) | as_native -%}
|
22 | 23 |
|
| 24 | + {#- Raise the errors/warnings in this order so that we do not get both -#} |
| 25 | + {% if min_max_ranks.max_rank | int > 100000 %} |
| 26 | + {{ automate_dv.max_iterations_error(func_name='vault_insert_by_rank') }} |
| 27 | + {% endif %} |
| 28 | + |
| 29 | + {{ automate_dv.experimental_not_recommended_warning(func_name='vault_insert_by_rank') }} |
| 30 | + |
| 31 | + |
23 | 32 | {%- set to_drop = [] -%}
|
24 | 33 |
|
25 | 34 | {%- do automate_dv.check_placeholder(sql, "__RANK_FILTER__") -%}
|
|
46 | 55 |
|
47 | 56 | {% elif full_refresh_mode %}
|
48 | 57 | {% set filtered_sql = automate_dv.replace_placeholder_with_rank_filter(sql, rank_column, 1) %}
|
49 |
| - {% if target.type == "postgres" %} |
| 58 | + {% if target.type in ['postgres', 'sqlserver'] %} |
50 | 59 | {{ automate_dv.drop_temporary_special(target_relation) }}
|
51 | 60 | {% endif %}
|
52 | 61 | {% set build_sql = create_table_as(False, target_relation, filtered_sql) %}
|
53 | 62 | {% else %}
|
54 | 63 |
|
55 |
| - {% if min_max_ranks.max_rank | int > 100000 %} |
56 |
| - {%- set error_message -%} |
57 |
| - 'Max iterations is 100,000. Consider using a different rank column |
58 |
| - or loading a smaller amount of data. |
59 |
| - vault_insert_by materialisations are not intended for this purpose, |
60 |
| - please see https://automate-dv.readthedocs.io/en/latest/materialisations/' |
61 |
| - {%- endset -%} |
62 |
| - |
63 |
| - {{- exceptions.raise_compiler_error(error_message) -}} |
64 |
| - {% else %} |
65 |
| - {% set target_columns = adapter.get_columns_in_relation(target_relation) %} |
66 |
| - {%- set target_cols_csv = target_columns | map(attribute='quoted') | join(', ') -%} |
67 |
| - {%- set loop_vars = {'sum_rows_inserted': 0} -%} |
68 |
| - |
69 |
| - {% for i in range(min_max_ranks.max_rank | int ) -%} |
70 |
| - |
71 |
| - {%- set iteration_number = i + 1 -%} |
72 |
| - |
73 |
| - {%- set filtered_sql = automate_dv.replace_placeholder_with_rank_filter(sql, rank_column, iteration_number) -%} |
74 |
| - |
75 |
| - {{ dbt_utils.log_info("Running for {} {} of {} on column '{}' [{}]".format('rank', iteration_number, min_max_ranks.max_rank, rank_column, model.unique_id)) }} |
76 |
| - |
77 |
| - {% set tmp_relation = make_temp_relation(target_relation) %} |
78 |
| - |
79 |
| - {# This call statement drops and then creates a temporary table #} |
80 |
| - {# but MSSQL will fail to drop any temporary table created by a previous loop iteration #} |
81 |
| - {# See MSSQL note and drop code below #} |
82 |
| - {% call statement() -%} |
83 |
| - {{ create_table_as(True, tmp_relation, filtered_sql) }} |
84 |
| - {%- endcall %} |
85 |
| - |
86 |
| - {{ adapter.expand_target_column_types(from_relation=tmp_relation, |
87 |
| - to_relation=target_relation) }} |
88 |
| - |
89 |
| - {%- set insert_query_name = 'main-' ~ i -%} |
90 |
| - {% call statement(insert_query_name, fetch_result=True) -%} |
91 |
| - INSERT INTO {{ target_relation }} ({{ target_cols_csv }}) |
92 |
| - ( |
93 |
| - SELECT {{ target_cols_csv }} |
94 |
| - FROM {{ tmp_relation.include(schema=True) }} |
95 |
| - ); |
96 |
| - {%- endcall %} |
97 |
| - |
98 |
| - {% set result = load_result(insert_query_name) %} |
99 |
| - {% if 'response' in result.keys() %} {# added in v0.19.0 #} |
100 |
| - {# Investigate for Databricks #} |
101 |
| - {%- if result['response']['rows_affected'] == None %} |
102 |
| - {% set rows_inserted = 0 %} |
103 |
| - {%- else %} |
104 |
| - {% set rows_inserted = result['response']['rows_affected'] %} |
105 |
| - {%- endif %} |
106 |
| - |
107 |
| - {% else %} {# older versions #} |
108 |
| - {% set rows_inserted = result['status'].split(" ")[2] | int %} |
109 |
| - {% endif %} |
110 |
| - |
111 |
| - {%- set sum_rows_inserted = loop_vars['sum_rows_inserted'] + rows_inserted -%} |
112 |
| - {%- do loop_vars.update({'sum_rows_inserted': sum_rows_inserted}) %} |
113 |
| - |
114 |
| - {{ dbt_utils.log_info("Ran for {} {} of {}; {} records inserted [{}]".format('rank', iteration_number, |
115 |
| - min_max_ranks.max_rank, |
116 |
| - rows_inserted, |
117 |
| - model.unique_id)) }} |
118 |
| - |
119 |
| - {# In databricks and sqlserver a temporary view/table can only be dropped by #} |
120 |
| - {# the connection or session that created it so drop it now before the commit below closes this session #} model.unique_id)) }} |
121 |
| - {% if target.type in ['databricks', 'sqlserver'] %} |
122 |
| - {{ automate_dv.drop_temporary_special(tmp_relation) }} |
123 |
| - {% else %} |
124 |
| - {% do to_drop.append(tmp_relation) %} |
125 |
| - {% endif %} |
126 |
| - |
127 |
| - {% do adapter.commit() %} |
128 |
| - |
129 |
| - {% endfor %} |
130 |
| - {% call noop_statement('main', "INSERT {}".format(loop_vars['sum_rows_inserted']) ) -%} |
131 |
| - {{ filtered_sql }} |
| 64 | + {% set target_columns = adapter.get_columns_in_relation(target_relation) %} |
| 65 | + {%- set target_cols_csv = target_columns | map(attribute='quoted') | join(', ') -%} |
| 66 | + {%- set loop_vars = {'sum_rows_inserted': 0} -%} |
| 67 | + |
| 68 | + {% for i in range(min_max_ranks.max_rank | int ) -%} |
| 69 | + |
| 70 | + {%- set iteration_number = i + 1 -%} |
| 71 | + |
| 72 | + {%- set filtered_sql = automate_dv.replace_placeholder_with_rank_filter(sql, rank_column, iteration_number) -%} |
| 73 | + |
| 74 | + {{ dbt_utils.log_info("Running for {} {} of {} on column '{}' [{}]".format('rank', iteration_number, min_max_ranks.max_rank, rank_column, model.unique_id)) }} |
| 75 | + |
| 76 | + {% set tmp_relation = make_temp_relation(target_relation) %} |
| 77 | + |
| 78 | + {# This call statement drops and then creates a temporary table #} |
| 79 | + {# but MSSQL will fail to drop any temporary table created by a previous loop iteration #} |
| 80 | + {# See MSSQL note and drop code below #} |
| 81 | + {% call statement() -%} |
| 82 | + {{ create_table_as(True, tmp_relation, filtered_sql) }} |
132 | 83 | {%- endcall %}
|
133 |
| - {% endif %} |
134 | 84 |
|
| 85 | + {{ adapter.expand_target_column_types(from_relation=tmp_relation, |
| 86 | + to_relation=target_relation) }} |
| 87 | + |
| 88 | + {%- set insert_query_name = 'main-' ~ i -%} |
| 89 | + {% call statement(insert_query_name, fetch_result=True) -%} |
| 90 | + INSERT INTO {{ target_relation }} ({{ target_cols_csv }}) |
| 91 | + ( |
| 92 | + SELECT {{ target_cols_csv }} |
| 93 | + FROM {{ tmp_relation.include(schema=True) }} |
| 94 | + ); |
| 95 | + {%- endcall %} |
| 96 | + |
| 97 | + {% set result = load_result(insert_query_name) %} |
| 98 | + {% if 'response' in result.keys() %} {# added in v0.19.0 #} |
| 99 | + {# Investigate for Databricks #} |
| 100 | + {%- if result['response']['rows_affected'] == None %} |
| 101 | + {% set rows_inserted = 0 %} |
| 102 | + {%- else %} |
| 103 | + {% set rows_inserted = result['response']['rows_affected'] %} |
| 104 | + {%- endif %} |
| 105 | + |
| 106 | + {% else %} {# older versions #} |
| 107 | + {% set rows_inserted = result['status'].split(" ")[2] | int %} |
| 108 | + {% endif %} |
| 109 | + |
| 110 | + {%- set sum_rows_inserted = loop_vars['sum_rows_inserted'] + rows_inserted -%} |
| 111 | + {%- do loop_vars.update({'sum_rows_inserted': sum_rows_inserted}) %} |
| 112 | + |
| 113 | + {{ dbt_utils.log_info("Ran for {} {} of {}; {} records inserted [{}]".format('rank', iteration_number, |
| 114 | + min_max_ranks.max_rank, |
| 115 | + rows_inserted, |
| 116 | + model.unique_id)) }} |
| 117 | + |
| 118 | + {# In databricks and sqlserver a temporary view/table can only be dropped by #} |
| 119 | + {# the connection or session that created it so drop it now before the commit below closes this session #} model.unique_id)) }} |
| 120 | + {% if target.type in ['databricks', 'sqlserver'] %} |
| 121 | + {{ automate_dv.drop_temporary_special(tmp_relation) }} |
| 122 | + {% else %} |
| 123 | + {% do to_drop.append(tmp_relation) %} |
| 124 | + {% endif %} |
| 125 | + |
| 126 | + {% do adapter.commit() %} |
| 127 | + |
| 128 | + {% endfor %} |
| 129 | + {% call noop_statement('main', "INSERT {}".format(loop_vars['sum_rows_inserted']) ) -%} |
| 130 | + {{ filtered_sql }} |
| 131 | + {%- endcall %} |
135 | 132 | {% endif %}
|
136 | 133 |
|
137 | 134 | {% if build_sql is defined %}
|
|
0 commit comments