dbt is_incremental macro 简单说明

is_incremental macro 实际上就是一个判断,以下是实现的简单说明,官方文档也有说明

参考处理

  • 逻辑
    模型必须存在(数据库中),目的表也存在(数据库中),full-refresh 没有传递,模型配置了materialized='incremental'
  • macro 实现
    结合上边的逻辑发现是可以对应上的
{% macro is_incremental() %}
    {#-- do not run introspective queries in parsing #}
    {% if not execute %}
        {{ return(False) }}
    {% else %}
        {% set relation = adapter.get_relation(this.database, this.schema, this.table) %}
        {{ return(relation is not none
                  and relation.type == 'table'
                  and model.config.materialized == 'incremental'
                  and not should_full_refresh()) }}
    {% endif %}
{% endmacro %}
  • 参考使用
{{
    config(
        materialized='incremental'
    )
}}
 
select
    *,
    my_slow_function(my_column)
 
from {{ ref('app_data_events') }}
 
{% if is_incremental() %}
 
  -- this filter will only be applied on an incremental run
  -- (uses >= to include records whose timestamp occurred since the last run of this model)
  where event_time >= (select max(event_time) from {{ this }})
 
{% endif %}

说明

结合源码以及官方文档进行dbt 的学习还是比较方便的

参考资料

https://docs.getdbt.com/docs/build/incremental-models

posted on 2024-04-24 07:15  荣锋亮  阅读(5)  评论(0编辑  收藏  举报

导航