/
incremental.sql
73 lines (58 loc) · 2.62 KB
/
incremental.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
{% materialization incremental, adapter='athena' -%}
{% set unique_key = config.get('unique_key') %}
{% set overwrite_msg -%}
Athena adapter does not support 'unique_key'
{%- endset %}
{% if unique_key is not none %}
{% do exceptions.raise_compiler_error(overwrite_msg) %}
{% endif %}
{% set raw_strategy = config.get('incremental_strategy', default='insert_overwrite') %}
{% set strategy = validate_get_incremental_strategy(raw_strategy) %}
{% set partitioned_by = config.get('partitioned_by', default=none) %}
{% set target_relation = this.incorporate(type='table') %}
{% set existing_relation = load_relation(this) %}
{% set tmp_relation = make_temp_relation(this) %}
{{ run_hooks(pre_hooks, inside_transaction=False) }}
-- `BEGIN` happens here:
{{ run_hooks(pre_hooks, inside_transaction=True) }}
{% set to_drop = [] %}
{% if existing_relation is none %}
{% set build_sql = create_table_as(False, target_relation, sql) %}
{% elif existing_relation.is_view or should_full_refresh() %}
{% do adapter.drop_relation(existing_relation) %}
{% set build_sql = create_table_as(False, target_relation, sql) %}
{% elif partitioned_by is not none and strategy == 'insert_overwrite' %}
{% set tmp_relation = make_temp_relation(target_relation) %}
{% if tmp_relation is not none %}
{% do adapter.drop_relation(tmp_relation) %}
{% endif %}
{% do run_query(create_table_as(True, tmp_relation, sql)) %}
{% do delete_overlapping_partitions(target_relation, tmp_relation, partitioned_by) %}
{% set build_sql = incremental_insert(tmp_relation, target_relation) %}
{% do to_drop.append(tmp_relation) %}
{% else %}
{% set tmp_relation = make_temp_relation(target_relation) %}
{% if tmp_relation is not none %}
{% do adapter.drop_relation(tmp_relation) %}
{% endif %}
{% do run_query(create_table_as(True, tmp_relation, sql)) %}
{% set build_sql = incremental_insert(tmp_relation, target_relation) %}
{% do to_drop.append(tmp_relation) %}
{% endif %}
{% call statement("main") %}
{{ build_sql }}
{% endcall %}
-- set table properties
{% if not to_drop %}
{{ set_table_classification(target_relation, 'parquet') }}
{% endif %}
{% do persist_docs(target_relation, model) %}
{{ run_hooks(post_hooks, inside_transaction=True) }}
-- `COMMIT` happens here
{% do adapter.commit() %}
{% for rel in to_drop %}
{% do adapter.drop_relation(rel) %}
{% endfor %}
{{ run_hooks(post_hooks, inside_transaction=False) }}
{{ return({'relations': [target_relation]}) }}
{%- endmaterialization %}