dbt this macro 处理简单说明
dbt this macro提供了一种方便的对于当前模型展现的方法,可以使用在增量模型以及pre&post hooks 中
this 实际是就类似ref('<the_current_model>')
是一个relation 包含了database,schema 以及模型标识
使用示例
一个增量处理的,基于this 可以方便的引用模型
{{ config(materialized='incremental') }}
select
*,
my_slow_function(my_column)
from raw_app_data.events
{% if is_incremental() %}
where event_time > (select max(event_time) from {{ this }})
{% endif %}
内部实现
基于dbt 标准的装饰器 (ModelContext 类中的属性)
@contextproperty()
def this(self) -> Optional[RelationProxy]:
"""`this` makes available schema information about the currently
executing model. It's is useful in any context in which you need to
write code that references the current model, for example when defining
a `sql_where` clause for an incremental model and for writing pre- and
post-model hooks that operate on the model in some way. Developers have
options for how to use `this`:
|------------------|------------------|
| dbt Model Syntax | Output |
|------------------|------------------|
| {{this}} | "schema"."table" |
|------------------|------------------|
| {{this.schema}} | schema |
|------------------|------------------|
| {{this.table}} | table |
|------------------|------------------|
| {{this.name}} | table |
|------------------|------------------|
Here's an example of how to use `this` in `dbt_project.yml` to grant
select rights on a table to a different db user.
> example.yml:
models:
project-name:
post-hook:
- "grant select on {{ this }} to db_reader"
"""
if self.model.resource_type == NodeType.Operation:
return None
# 结合方便的模型配置,通过db_wrapper 进行relation 的创建
return self.db_wrapper.Relation.create_from(self.config, self.model)
说明
dbt this macro 实现了方便的对于当前模型的使用,可以提升模型的灵活性
参考资料
core/dbt/context/providers.py
https://docs.getdbt.com/reference/dbt-jinja-functions/this
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2023-03-31 nginx 1.23.4 发布
2022-03-31 mimir grafana 部署模式
2022-03-31 部署时创建minio bucket 的方法
2022-03-31 mimir grafana 时序存储参考试用
2022-03-31 mimir grafana 团队开源的时序数据存储服务
2022-03-31 signoz 开源apm 工具试用
2021-03-31 opendistro elasticsearch cube.js driver 开发说明