dbt model 对象简单说明

dbt model 属于graph 对象,实际上就是一个node 节点,包含了当前模型的一些信息

主要场景

  • 访问config 设置
  • 访问模型的路径

参考使用

  • 查看模型内容
    可以通过log macro
{{ log(model, info=True) }}

内部实现

实际上就是context 属性信息

@contextproperty("model")
def ctx_model(self) -> Dict[str, Any]:
    model_dct = self.model.to_dict(omit_none=True)
    # Maintain direct use of compiled_sql
    # TODO add depreciation logic[CT-934]
    if "compiled_code" in model_dct:
        model_dct["compiled_sql"] = model_dct["compiled_code"]
 
    if (
        hasattr(self.model, "contract")
        and self.model.contract.alias_types is True
        and "columns" in model_dct
    ):
        for column in model_dct["columns"].values():
            if "data_type" in column:
                orig_data_type = column["data_type"]
                # translate data_type to value in Column.TYPE_LABELS
                new_data_type = self.adapter.Column.translate_type(orig_data_type)
                column["data_type"] = new_data_type
    return model_dct

说明

再一些场景中我们可能需要访问model 的一些信息,比如物化处理上,获取配置信息的,了解一些功能还是很有用的

参考资料

core/dbt/context/providers.py
https://docs.getdbt.com/reference/dbt-jinja-functions/model
https://docs.getdbt.com/docs/build/incremental-models
https://schemas.getdbt.com/dbt/manifest/v11/index.html#tab-pane_nodes_additionalProperties_anyOf_i3

posted on 2024-05-17 06:26  荣锋亮  阅读(10)  评论(0编辑  收藏  举报

导航