|
| 1 | + |
| 2 | +{% materialization incremental, adapter='fabric' -%} |
| 3 | + |
| 4 | + {%- set full_refresh_mode = (should_full_refresh()) -%} |
| 5 | + {% set target_relation = this.incorporate(type='table') %} |
| 6 | + {%- set relations_list = fabric__get_relation_without_caching(target_relation) -%} |
| 7 | + |
| 8 | + {%- set existing_relation = none %} |
| 9 | + {% if (relations_list|length == 1) and (relations_list[0][2] == target_relation.schema) |
| 10 | + and (relations_list[0][1] == target_relation.identifier) and (relations_list[0][3] == target_relation.type)%} |
| 11 | + {% set existing_relation = target_relation %} |
| 12 | + {% elif (relations_list|length == 1) and (relations_list[0][2] == target_relation.schema) |
| 13 | + and (relations_list[0][1] == target_relation.identifier) and (relations_list[0][3] != target_relation.type) %} |
| 14 | + {% set existing_relation = get_or_create_relation(relations_list[0][0], relations_list[0][2] , relations_list[0][1] , relations_list[0][3])[1] %} |
| 15 | + {% endif %} |
| 16 | + |
| 17 | + {{ log("Full refresh mode" ~ full_refresh_mode)}} |
| 18 | + {{ log("existing relation : "~existing_relation ~ " type "~ existing_relation.type ~ " is view? "~existing_relation.is_view) }} |
| 19 | + {{ log("target relation: " ~target_relation ~ " type "~ target_relation.type ~ " is view? "~target_relation.is_view) }} |
| 20 | + |
| 21 | + -- configs |
| 22 | + {%- set unique_key = config.get('unique_key') -%} |
| 23 | + {% set incremental_strategy = config.get('incremental_strategy') or 'default' %} |
| 24 | + {%- set temp_relation = make_temp_relation(target_relation)-%} |
| 25 | + |
| 26 | + {% set grant_config = config.get('grants') %} |
| 27 | + {%- set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') -%} |
| 28 | + |
| 29 | + {{ run_hooks(pre_hooks, inside_transaction=True) }} |
| 30 | + |
| 31 | + {% if existing_relation is none %} |
| 32 | + |
| 33 | + {%- call statement('main') -%} |
| 34 | + {{ fabric__create_table_as(False, target_relation, sql)}} |
| 35 | + {%- endcall -%} |
| 36 | + |
| 37 | + {% elif existing_relation.is_view %} |
| 38 | + |
| 39 | + {#-- Can't overwrite a view with a table - we must drop --#} |
| 40 | + {{ log("Dropping relation " ~ target_relation ~ " because it is a view and this model is a table.") }} |
| 41 | + {{ drop_relation_if_exists(existing_relation) }} |
| 42 | + {%- call statement('main') -%} |
| 43 | + {{ fabric__create_table_as(False, target_relation, sql)}} |
| 44 | + {%- endcall -%} |
| 45 | + |
| 46 | + {% elif full_refresh_mode %} |
| 47 | + |
| 48 | + {%- call statement('main') -%} |
| 49 | + {{ fabric__create_table_as(False, target_relation, sql)}} |
| 50 | + {%- endcall -%} |
| 51 | + |
| 52 | + {% else %} |
| 53 | + |
| 54 | + {%- call statement('create_tmp_relation') -%} |
| 55 | + {{ fabric__create_table_as(True, temp_relation, sql)}} |
| 56 | + {%- endcall -%} |
| 57 | + {% do adapter.expand_target_column_types( |
| 58 | + from_relation=temp_relation, |
| 59 | + to_relation=target_relation) %} |
| 60 | + {#-- Process schema changes. Returns dict of changes if successful. Use source columns for upserting/merging --#} |
| 61 | + {% set dest_columns = process_schema_changes(on_schema_change, temp_relation, existing_relation) %} |
| 62 | + {% if not dest_columns %} |
| 63 | + {% set dest_columns = adapter.get_columns_in_relation(existing_relation) %} |
| 64 | + {% endif %} |
| 65 | + |
| 66 | + {#-- Get the incremental_strategy, the macro to use for the strategy, and build the sql --#} |
| 67 | + {% set incremental_predicates = config.get('predicates', none) or config.get('incremental_predicates', none) %} |
| 68 | + {% set strategy_sql_macro_func = adapter.get_incremental_strategy_macro(context, incremental_strategy) %} |
| 69 | + {% set strategy_arg_dict = ({'target_relation': target_relation, 'temp_relation': temp_relation, 'unique_key': unique_key, 'dest_columns': dest_columns, 'incremental_predicates': incremental_predicates }) %} |
| 70 | + {%- call statement('main') -%} |
| 71 | + {{ strategy_sql_macro_func(strategy_arg_dict) }} |
| 72 | + {%- endcall -%} |
| 73 | + {% endif %} |
| 74 | + |
| 75 | + {% do drop_relation_if_exists(temp_relation) %} |
| 76 | + {{ run_hooks(post_hooks, inside_transaction=True) }} |
| 77 | + |
| 78 | + {% set target_relation = target_relation.incorporate(type='table') %} |
| 79 | + |
| 80 | + {% set should_revoke = should_revoke(existing_relation, full_refresh_mode) %} |
| 81 | + {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %} |
| 82 | + |
| 83 | + {% do persist_docs(target_relation, model) %} |
| 84 | + {% do adapter.commit() %} |
| 85 | + {{ return({'relations': [target_relation]}) }} |
| 86 | + |
| 87 | +{%- endmaterialization %} |
0 commit comments