From 2c94e9e65051918915e7e563fa425889df42bb9f Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Wed, 13 Feb 2019 11:19:53 -0500 Subject: [PATCH] (fixes #1292) Check for relation type in is_incremental() If the target relation is a non-table (probably a view) then dbt should return False from the is_incremental() macro. The materialization will drop this relation before running the model code as a `create table as` statement, so the incremental filter would likely be invalid. --- .../include/global_project/macros/etc/is_incremental.sql | 2 +- .../035_changing_relation_type_test/models/model.sql | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/dbt/include/global_project/macros/etc/is_incremental.sql b/core/dbt/include/global_project/macros/etc/is_incremental.sql index d28a6a44cb2..f3897294a13 100644 --- a/core/dbt/include/global_project/macros/etc/is_incremental.sql +++ b/core/dbt/include/global_project/macros/etc/is_incremental.sql @@ -5,6 +5,6 @@ {{ return(False) }} {% else %} {% set relation = adapter.get_relation(this.database, this.schema, this.table) %} - {{ return(relation is not none and not flags.FULL_REFRESH) }} + {{ return(relation is not none and relation.type == 'table' and not flags.FULL_REFRESH) }} {% endif %} {% endmacro %} diff --git a/test/integration/035_changing_relation_type_test/models/model.sql b/test/integration/035_changing_relation_type_test/models/model.sql index a2363779a2d..a48f6691e8c 100644 --- a/test/integration/035_changing_relation_type_test/models/model.sql +++ b/test/integration/035_changing_relation_type_test/models/model.sql @@ -1,5 +1,9 @@ -{{ config(materialized=var('materialized'), sql_where='TRUE') }} +{{ config(materialized=var('materialized')) }} select '{{ var("materialized") }}' as materialization + +{% if var('materialized') == 'incremental' and is_incremental() %} + where 'abc' != (select max(materialization) from {{ this }}) +{% endif %}