首页 > 其他分享 >dbt doc 函数内部处理简单说明

dbt doc 函数内部处理简单说明

时间:2024-04-21 09:13:25浏览次数:17  
标签:name package doc self manifest docs dbt 函数

dbt 提供了一个方便的doc 函数,可以方便的使用类似ref 模式进行docs block 定义的引用

引用参考处理

  • 示例
version: 2
models:
  - name: events
    description: '{{ doc("table_events") }}'
    columns:
      - name: event_id
        description: This is a unique identifier for the event
        tests:
            - unique
            - not_null

参考处理

主要是通过DocsRuntimeContext

class DocsRuntimeContext(SchemaYamlContext):
    def __init__(
        self,
        config: RuntimeConfig,
        node: Union[Macro, ResultNode],
        manifest: Manifest,
        current_project: str,
    ) -> None:
        super().__init__(config, current_project, None)
        self.node = node
        self.manifest = manifest
   # contextmember 标记的方法
    @contextmember
    def doc(self, *args: str) -> str:
        """The `doc` function is used to reference docs blocks in schema.yml
        files. It is analogous to the `ref` function. For more information,
        consult the Documentation guide.
 
        > orders.md:
 
            {% docs orders %}
            # docs
            - go
            - here
            {% enddocs %}
 
        > schema.yml
 
            version: 2
                models:
                  - name: orders
                    description: "{{ doc('orders') }}"
        """
        # when you call doc(), this is what happens at runtime
        if len(args) == 1:
            doc_package_name = None
            doc_name = args[0]
        elif len(args) == 2:
            doc_package_name, doc_name = args
        else:
            raise DocArgsError(self.node, args)
 
        # Documentation, 实际上是通过解析manifest 中的resolve_doc
        target_doc = self.manifest.resolve_doc(
            doc_name,
            doc_package_name,
            self._project_name,
            self.node.package_name,
        )
        if target_doc:
            file_id = target_doc.file_id
            if file_id in self.manifest.files:
                source_file = self.manifest.files[file_id]
                # TODO CT-211
                source_file.add_node(self.node.unique_id)  # type: ignore[union-attr]
        else:
            raise DocTargetNotFoundError(
                node=self.node, target_doc_name=doc_name, target_doc_package=doc_package_name
            )
 
        return target_doc.block_contents
   
def generate_runtime_docs_context(
    config: RuntimeConfig,
    target: Any,
    manifest: Manifest,
    current_project: str,
) -> Dict[str, Any]:
    ctx = DocsRuntimeContext(config, target, manifest, current_project)
    # This is not a Mashumaro to_dict call
    return ctx.to_dict()
  • resolve_doc 处理

核心是返回一个Documentation 类

def resolve_doc(
    self,
    name: str,
    package: Optional[str],
    current_project: str,
    node_package: str,
) -> Optional[Documentation]:
    """Resolve the given documentation. This follows the same algorithm as
    resolve_ref except the is_enabled checks are unnecessary as docs are
    always enabled.
    """
    candidates = _search_packages(current_project, node_package, package)
 
    for pkg in candidates:
        result = self.doc_lookup.find(name, pkg, self)
        if result is not None:
            return result
    return None
  • doc_lookup 处理

内部是通过DocLookup 类处理的,实际内部也是manifest 的docs

def perform_lookup(self, unique_id: UniqueID, manifest) -> Documentation:
    if unique_id not in manifest.docs:
        raise dbt.exceptions.DbtInternalError(
            f"Doc {unique_id} found in cache but not found in manifest"
        )
    return manifest.docs[unique_id]
  • 实际上docs manifest 参考

可以看到上边解析部分的处理会结合下边的格式

"doc.dremio_demo_app.__dremio_demo_app__": {
      "name": "__dremio_demo_app__",
      "resource_type": "doc",
      "package_name": "dremio_demo_app",
      "path": "dalongdemo.md",
      "original_file_path": "models/dalongdemo.md",
      "unique_id": "doc.dremio_demo_app.__dremio_demo_app__",
      "block_contents": "##  this is my pacakge demo\n\n\n### contains \n\n- mymodel-v2\n- mymodel-v3"
    },

说明

dbt 的manifest 是一个比较重要的东西,好多地方都会有使用到,后边详细说明下manifest 的内部解析以及执行

参考资料

core/dbt/context/docs.py
core/dbt/contracts/graph/manifest.py
https://docs.getdbt.com/reference/dbt-jinja-functions/doc

标签:name,package,doc,self,manifest,docs,dbt,函数
From: https://www.cnblogs.com/rongfengliang/p/18118296

相关文章

  • 使用Docker部署一个简单的web项目
    使用Docker部署一个简单的web项目开发流程在本地开发一个有静态文件服务的web服务程序web服务监听ip+port为0.0.0.0:3000在服务器上使用Dockerfile构建镜像使用构建出的镜像运行容器配置Nginx将端口代理到web服务的3000端口在本地开发一个有静态文件服......
  • 第十节 闭区间上连续函数的性质
    第十节闭区间上连续函数的性质一、有界性与最大值最小值定理对于在区间I上有定义的函数\(f(x)\),如果有\(x₀∈I\),使得对于任一\(x∈I\)都有\(f(x)≤f(x₀)(f(x)≥f(x₀))\),那么称$f(x₀)$是函数\(f(x)\)在区间I上的最大值(最小值)定理1:(有界性与最大值......
  • 深入了解PBKDF2:密码学中的关键推导函数
    title:深入了解PBKDF2:密码学中的关键推导函数date:2024/4/2020:37:35updated:2024/4/2020:37:35tags:密码学对称加密哈希函数KDFPBKDF2安全密钥派生第一章:密码学基础对称加密和哈希函数对称加密:对称加密是一种加密技术,使用相同的密钥进行加密和解密。常见......
  • docker---配置域名解析
    docker---配置域名解析以ubuntu22.04作为基础镜像,build新的镜像,在执行apt-getupdate命令时,出现以下错误:167.4Err:2http://security.ubuntu.com/ubuntujammy-securityInRelease167.4Temporaryfailureresolving'security.ubuntu.com'搜索错误信息,发现是域名解析问......
  • shell脚本while循环、read读取控制台输入与函数
    while循环while循环结构while[条件判断]do程序done脚本示例:点击查看代码#!/bin/bashwhile[$a-le$1]do sum=$[$sum+$a]doneecho$sum简易写法:点击查看代码#!/bin/basha=1while[$a-le$1]do letsum+=a leta++doneecho$sumread读取......
  • 使用docker compose一键启动多个应用
    使用dockercompose一键启动多个应用 环境说明linux系统版本:lsb_release-a  docker版本:docker-v  不同的操作系统以及软件版本,可能会遇到不一样的问题,一定要注意版本问题。  .1.安装教程,参考官网安装指南。https://dockerdocs.cn/compose/install/ind......
  • docker 数据卷查看以及数据删除
    如何清理您的Docker数据Docker不需要对系统进行配置更改…但它却使用大量的磁盘空间。短暂的使用后,通过下面命令您可能会震惊地看到一些可怕使用统计信息:dockersystemdf不过不用担心,Docker允许您从未使用的images,containers和volumes中回收磁盘空间。定期清理为了安全......
  • 【转载】Java函数式编程——为什么new Thread()中要用函数式编程
    面向对象过分强调“必须通过对象的形式来做事情”,而函数式思想则尽量忽略面向对象的复杂语法——强调做什么,而不是以什么形式做。面向对象的思想:做一件事情,找一个能解决这个事情的对象,调用对象的方法,完成事情.函数式编程思想:只要能获取到结果,谁去做的,怎么做的都不重要,......
  • 阿里云函数计算域SSL证书免费申请及部署
    阿里云函数计算大大简化了开发部署的工作量,用户只需聚焦于业务逻辑的开发,编写最重要的“核心代码”;不再需要关心服务器购买、负载均衡、自动伸缩等运维操作;极大地降低了服务搭建的复杂性,有效提升开发和迭代的速度。但是在使用过程中发现,对SSL证书的支持需要支付额外费用,具......
  • dbt asset-paths 简单说明
    dbt的asset-paths是一个比较有意思的配置,可以用来增强我们的文档信息,比如存放一些图片在资源描述中引用资源生成的文档中可以进行显示,提示文档的信息参考配置dbt_project.ymlasset-paths:["assets"]使用假如assets包含一些描述图片信息models/ap......