|
| 1 | +{% materialization incremental, adapter='iris', supported_languages=['sql', 'python'] -%} |
| 2 | + |
| 3 | + {%- set language = model['language'] -%} |
| 4 | + |
| 5 | + -- relations |
| 6 | + {%- set existing_relation = load_cached_relation(this) -%} |
| 7 | + {%- set target_relation = this.incorporate(type='table') -%} |
| 8 | + {%- set temp_relation = make_temp_relation(target_relation)-%} |
| 9 | + {%- set intermediate_relation = make_intermediate_relation(target_relation)-%} |
| 10 | + {%- set backup_relation_type = 'table' if existing_relation is none else existing_relation.type -%} |
| 11 | + {%- set backup_relation = make_backup_relation(target_relation, backup_relation_type) -%} |
| 12 | + |
| 13 | + -- configs |
| 14 | + {%- set unique_key = config.get('unique_key') -%} |
| 15 | + {%- set full_refresh_mode = (should_full_refresh() or existing_relation.is_view) -%} |
| 16 | + {%- set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') -%} |
| 17 | + |
| 18 | + -- the temp_ and backup_ relations should not already exist in the database; get_relation |
| 19 | + -- will return None in that case. Otherwise, we get a relation that we can drop |
| 20 | + -- later, before we try to use this name for the current operation. This has to happen before |
| 21 | + -- BEGIN, in a separate transaction |
| 22 | + {%- set preexisting_intermediate_relation = load_cached_relation(intermediate_relation)-%} |
| 23 | + {%- set preexisting_backup_relation = load_cached_relation(backup_relation) -%} |
| 24 | + -- grab current tables grants config for comparision later on |
| 25 | + {% set grant_config = config.get('grants') %} |
| 26 | + {{ drop_relation_if_exists(preexisting_intermediate_relation) }} |
| 27 | + {{ drop_relation_if_exists(preexisting_backup_relation) }} |
| 28 | + |
| 29 | + {{ run_hooks(pre_hooks, inside_transaction=False) }} |
| 30 | + |
| 31 | + -- `BEGIN` happens here: |
| 32 | + {{ run_hooks(pre_hooks, inside_transaction=True) }} |
| 33 | + |
| 34 | + {% set to_drop = [] %} |
| 35 | + |
| 36 | + {% if existing_relation is none %} |
| 37 | + {%- call statement('main', language=language) -%} |
| 38 | + {{ create_table_as(False, target_relation, compiled_code, language) }} |
| 39 | + {%- endcall -%} |
| 40 | + {% elif full_refresh_mode %} |
| 41 | + {%- call statement('main', language=language) -%} |
| 42 | + {{ create_table_as(False, intermediate_relation, compiled_code, language) }} |
| 43 | + {%- endcall -%} |
| 44 | + {% set need_swap = true %} |
| 45 | + {% else %} |
| 46 | + {%- call statement('main', language=language) -%} |
| 47 | + {{ create_table_as(True, temp_relation, compiled_code, language) }} |
| 48 | + {%- endcall -%} |
| 49 | + |
| 50 | + {% do adapter.expand_target_column_types( |
| 51 | + from_relation=temp_relation, |
| 52 | + to_relation=target_relation) %} |
| 53 | + {#-- Process schema changes. Returns dict of changes if successful. Use source columns for upserting/merging --#} |
| 54 | + {% set dest_columns = process_schema_changes(on_schema_change, temp_relation, existing_relation) %} |
| 55 | + {% if not dest_columns %} |
| 56 | + {% set dest_columns = adapter.get_columns_in_relation(existing_relation) %} |
| 57 | + {% endif %} |
| 58 | + |
| 59 | + {#-- Get the incremental_strategy, the macro to use for the strategy, and build the sql --#} |
| 60 | + {% set incremental_strategy = config.get('incremental_strategy') or 'default' %} |
| 61 | + {% set incremental_predicates = config.get('incremental_predicates', none) %} |
| 62 | + {% set strategy_sql_macro_func = adapter.get_incremental_strategy_macro(context, incremental_strategy) %} |
| 63 | + {% set strategy_arg_dict = ({'target_relation': target_relation, 'temp_relation': temp_relation, 'unique_key': unique_key, 'dest_columns': dest_columns, 'predicates': incremental_predicates }) %} |
| 64 | + |
| 65 | + {%- call statement('main') -%} |
| 66 | + {{ strategy_sql_macro_func(strategy_arg_dict) }} |
| 67 | + {%- endcall -%} |
| 68 | + {% endif %} |
| 69 | + |
| 70 | + {% if need_swap %} |
| 71 | + {% do adapter.rename_relation(target_relation, backup_relation) %} |
| 72 | + {% do adapter.rename_relation(intermediate_relation, target_relation) %} |
| 73 | + {% do to_drop.append(backup_relation) %} |
| 74 | + {% endif %} |
| 75 | + |
| 76 | + {% set should_revoke = should_revoke(existing_relation, full_refresh_mode) %} |
| 77 | + {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %} |
| 78 | + |
| 79 | + {% do persist_docs(target_relation, model) %} |
| 80 | + |
| 81 | + {% if existing_relation is none or existing_relation.is_view or should_full_refresh() %} |
| 82 | + {% do create_indexes(target_relation) %} |
| 83 | + {% endif %} |
| 84 | + |
| 85 | + {{ run_hooks(post_hooks, inside_transaction=True) }} |
| 86 | + |
| 87 | + -- `COMMIT` happens here |
| 88 | + {% do adapter.commit() %} |
| 89 | + |
| 90 | + {% for rel in to_drop %} |
| 91 | + {% do adapter.drop_relation(rel) %} |
| 92 | + {% endfor %} |
| 93 | + |
| 94 | + {{ run_hooks(post_hooks, inside_transaction=False) }} |
| 95 | + |
| 96 | + {{ return({'relations': [target_relation]}) }} |
| 97 | + |
| 98 | +{%- endmaterialization %} |
0 commit comments