From f0f8dfae3c3ce76b1d1643443bf3d327edb89586 Mon Sep 17 00:00:00 2001 From: John Paul Alcala Date: Thu, 12 Sep 2024 14:41:35 +0800 Subject: [PATCH 1/2] delete+insert requires incremental_predicates if there's no unique_key This allows us to perform fine-grained optimizations that does not rely on the unique key. This optimization is important for data warehouses like Redshift, where there is no concept of primary/unique keys and indexes. --- .../materializations/models/incremental/merge.sql | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dbt/include/global_project/macros/materializations/models/incremental/merge.sql b/dbt/include/global_project/macros/materializations/models/incremental/merge.sql index ca972c9f..68dcc8f5 100644 --- a/dbt/include/global_project/macros/materializations/models/incremental/merge.sql +++ b/dbt/include/global_project/macros/materializations/models/incremental/merge.sql @@ -89,6 +89,18 @@ {%- endif -%}; {% endif %} + {% else %} + {% if not incremental_predicates %} + {{ exceptions.raise_compiler_error("incremental_predicates is required when there is no unique_key") }} + {% endif %} + + delete from {{ target }} + where ( + {% for predicate in incremental_predicates %} + {{ predicate|replace('DBT_INTERNAL_DEST', target)|replace('DBT_INTERNAL_SOURCE', source) }} + {{ "and " if not loop.last}} + {% endfor %} + ); {% endif %} insert into {{ target }} ({{ dest_cols_csv }}) From b11c5f5852579505c4daa4bd262fafc3beb6e030 Mon Sep 17 00:00:00 2001 From: John Paul Alcala Date: Thu, 12 Sep 2024 15:40:58 +0800 Subject: [PATCH 2/2] Added changelog --- .changes/unreleased/Features-20240912-153932.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Features-20240912-153932.yaml diff --git a/.changes/unreleased/Features-20240912-153932.yaml b/.changes/unreleased/Features-20240912-153932.yaml new file mode 100644 index 00000000..8ef2d27d --- /dev/null +++ b/.changes/unreleased/Features-20240912-153932.yaml @@ -0,0 +1,6 @@ +kind: Features +body: delete+insert requires incremental_predicates if there's no unique_key +time: 2024-09-12T15:39:32.654501+08:00 +custom: + Author: dlord + Issue: https://github.com/dbt-labs/dbt-core/issues/10655