dbt adapter 的get_relation 简单说明

dbt 的adapter.get_relation 可以方便的获取存在的relation 信息,以下是一个简单说明

参考实现

  • 内部处理
@available.parse_none
def get_relation(self, database: str, schema: str, identifier: str) -> Optional[BaseRelation]:
    relations_list = self.list_relations(database, schema)
 
    matches = self._make_match(relations_list, database, schema, identifier)
 
    if len(matches) > 1:
        kwargs = {
            "identifier": identifier,
            "schema": schema,
            "database": database,
        }
        raise RelationReturnedMultipleResultsError(kwargs, matches)
 
    elif matches:
        return matches[0]
 
    return None
  • 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 %}

同时还有一个快速使用的包装 load_relation
内部参考处理

{% macro load_cached_relation(relation) %}
  {% do return(adapter.get_relation(
    database=relation.database,
    schema=relation.schema,
    identifier=relation.identifier
  )) -%}
{% endmacro %}
 
-- old name for backwards compatibility
{% macro load_relation(relation) %}
    {{ return(load_cached_relation(relation)) }}
{% endmacro %}

说明

很多时候我们需要进行relation时候存在进行判断(比如增量处理,快照处理,物化处理)adapter.get_relation 以及load_relation 就是一个
很不错的帮助工具

参考资料

dbt/adapters/base/impl.py
https://docs.getdbt.com/reference/dbt-jinja-functions/adapter

posted on   荣锋亮  阅读(14)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-05-19 一次k8s 数据卷异常问题的解决
2021-05-19 cube.js 最新版本一些变动
2021-05-19 新版本浏览器跨站cookie 获取问题
2020-05-19 dgraph 20.03 支持native graphql 查询了
2020-05-19 grafana 7.0 支持分布式追踪框架的dashboard 展示
2020-05-19 graphql mesh graphql 模式使用HAProxy Data Plane API 的流程
2020-05-19 通过graphql mesh 暴露HAProxy Data Plane API graphql api

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示