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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用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