|
65 | 65 | )
|
66 | 66 |
|
67 | 67 | {% endmacro %}
|
| 68 | + |
| 69 | +{% macro redshift__get_incremental_microbatch_sql(arg_dict) %} |
| 70 | + {#- |
| 71 | + Technically this function could just call out to the default implementation of delete_insert. |
| 72 | + However, the default implementation requires a unique_id, which we actually do not want or |
| 73 | + need. Thus we re-implement delete insert here without the unique_id requirement |
| 74 | + -#} |
| 75 | + |
| 76 | + {%- set target = arg_dict["target_relation"] -%} |
| 77 | + {%- set source = arg_dict["temp_relation"] -%} |
| 78 | + {%- set dest_columns = arg_dict["dest_columns"] -%} |
| 79 | + {%- set predicates = [] -%} |
| 80 | + |
| 81 | + {%- set incremental_predicates = [] if arg_dict.get('incremental_predicates') is none else arg_dict.get('incremental_predicates') -%} |
| 82 | + {%- for pred in incremental_predicates -%} |
| 83 | + {% if "DBT_INTERNAL_DEST." in pred %} |
| 84 | + {%- set pred = pred | replace("DBT_INTERNAL_DEST.", target ~ "." ) -%} |
| 85 | + {% endif %} |
| 86 | + {% if "dbt_internal_dest." in pred %} |
| 87 | + {%- set pred = pred | replace("dbt_internal_dest.", target ~ "." ) -%} |
| 88 | + {% endif %} |
| 89 | + {% do predicates.append(pred) %} |
| 90 | + {% endfor %} |
| 91 | + |
| 92 | + {% if not model.config.get("__dbt_internal_microbatch_event_time_start") or not model.config.get("__dbt_internal_microbatch_event_time_end") -%} |
| 93 | + {% do exceptions.raise_compiler_error('dbt could not compute the start and end timestamps for the running batch') %} |
| 94 | + {% endif %} |
| 95 | + |
| 96 | + {#-- Add additional incremental_predicates to filter for batch --#} |
| 97 | + {% do predicates.append(model.config.event_time ~ " >= TIMESTAMP '" ~ model.config.__dbt_internal_microbatch_event_time_start ~ "'") %} |
| 98 | + {% do predicates.append(model.config.event_time ~ " < TIMESTAMP '" ~ model.config.__dbt_internal_microbatch_event_time_end ~ "'") %} |
| 99 | + {% do arg_dict.update({'incremental_predicates': predicates}) %} |
| 100 | + |
| 101 | + delete from {{ target }} |
| 102 | + where ( |
| 103 | + {% for predicate in predicates %} |
| 104 | + {%- if not loop.first %}and {% endif -%} {{ predicate }} |
| 105 | + {% endfor %} |
| 106 | + ); |
| 107 | + |
| 108 | + {%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%} |
| 109 | + insert into {{ target }} ({{ dest_cols_csv }}) |
| 110 | + ( |
| 111 | + select {{ dest_cols_csv }} |
| 112 | + from {{ source }} |
| 113 | + ) |
| 114 | +{% endmacro %} |
0 commit comments