首页 > 其他分享 >dbt Relation check_schema_exists 一个有意思的功能

dbt Relation check_schema_exists 一个有意思的功能

时间:2024-05-18 09:00:49浏览次数:15  
标签:information exists check Relation relation dbt schema

dbt 内部总有一些隐藏的小细节,官方文档没有说明,但是在一些adapter 实现中包含,一些是关于check_schema_exists 的一些说明

内部处理

  • dbt/adapters/sql/impl.py
def check_schema_exists(self, database: str, schema: str) -> bool:
    information_schema = self.Relation.create(
        database=database,
        schema=schema,
        identifier="INFORMATION_SCHEMA",
        quote_policy=self.config.quoting,
    ).information_schema()
    # 可以看到会包含一个INFORMATION_SCHEMA 信息,我们可以基于此使用标准的INFORMATION_SCHEMA 进行一些查询
    kwargs = {"information_schema": information_schema, "schema": schema}
    results = self.execute_macro(CHECK_SCHEMA_EXISTS_MACRO_NAME, kwargs=kwargs)
    return results[0][0] > 0
  • macro 处理
{% macro check_schema_exists(information_schema, schema) -%}
  {{ return(adapter.dispatch('check_schema_exists', 'dbt')(information_schema, schema)) }}
{% endmacro %}
 
{% macro default__check_schema_exists(information_schema, schema) -%}
  {% set sql -%}
        select count(*)
       # 此处replace 实际上是dbt 包装的一个replace 实现, information_schema_view 是InformationSchema的一个属性
        from {{ information_schema.replace(information_schema_view='SCHEMATA') }}
        where catalog_name='{{ information_schema.database }}'
          and schema_name='{{ schema }}'
  {%- endset %}
  {{ return(run_query(sql)) }}
{% endmacro %}

参考基于data class 的调用

dbt 包装了一个data class 可以进行relation 的创建

  • 参考使用
{% set relation = api.Relation.create(schema='appdemo', identifier='events') %}
{{log(relation,info=True)}}
{{log(relation.information_schema().replace(information_schema_view='TABLE'),info=True)}}

说明

dbt 隐藏一个小功能,还是比较有用的,对于实际使用也比较方便,dremio 对于schema 因为是不支持,支持基于python 代码实现了通过rest api 处理了

参考资料

dbt/adapters/sql/impl.py
dbt/adapters/base/relation.py
dbt/adapters/dremio/impl.py
dbt/include/global_project/macros/adapters/metadata.sql
https://docs.getdbt.com/reference/dbt-classes

标签:information,exists,check,Relation,relation,dbt,schema
From: https://www.cnblogs.com/rongfengliang/p/18153191

相关文章

  • 执行npm run serve有时提示npm update check failed
    背景:这个错误虽说无关紧要,但有时候会出现就感觉不爽。错误提示: 解决方法:在网络上查阅资料后才知道是因为文件夹权限的问题(1.)删除目录configstore由于权限问题,该目录经常出现故障。如果删除该目录,则下次运行命令时将重新生成该目录。(2.)在Windows上删除......
  • Flink同步kafka到iceberg数据延迟,两个checkpoint后才可查询
    一、问题描述用户配置了高级参数很多,观察kafka增量数据不多,flink负载不高情况下两个checkpoint后才可查询到数据。  排查时hdfs有数据文件产生,但是mainfast文件中最新快照id没变化。 二、原因经腾讯排查,用户参数指定高级参数execution.checkpointing.unaligned:true引起......
  • npm depcheck 包依赖检查
    1.概述在前端开发时,如果经常会遇到一些依赖的问题,比如一个项目,之前在package.json安装了某个包,后来又删除了这个包,但是node_modules包还是在的,但是我们把代码给其他人安装的时候,可能就缺失这些包,导致前端项目启动不了2.使用depcheck检查包2.1安装npmi-gdepcheck全局......
  • TEE开发 checkpatch.sh 使用方法
    checkpatch.sh是对checkpatch.pl的封装,在opteeos官方脚本里面scripts/下可以找到有如下使用方法Usage:checkpatch.sh[--working]Checkworkingareacheckpatch.sh<commit>...Checkspecificcommit(s)checkp......
  • checkboxlist绑定数据方法
    checkboxlist绑定数据方法1.把数据绑定到CheckBoxList中特别要注意加载顺序protectedvoidPage_Load(objectsender,EventArgse){if(!Page.IsPostBack){SqlConnectioncon=GetDBCon.GetCon();......
  • checkpoint防火墙测试授权申请
    本文介绍如何在线申请checkpoint防火墙的测试授权请先确保已注册官网账号并能正常登录ProductCenter,并安装好checkpoint并配置好管理IP(授权申请需要用到设备IP地址,不需要连网)(官网账号最好使用公司邮箱申请)ProductCenter链接正常登录后可看到如下图内容其中selecta......
  • [code notes] check_agg_arguments
    TheSQLselectsum(sum(a))frommyt1groupbya;Thisnotefocusesonlyonsum(sum(a))andit'sabouthowpostgresrejectsthesqlabove.Notessum(sum(a))|||||\_innermostargument,Varnode|\_ functioncall\_functioncal......
  • u-radio-group 与 radio-group,u-radio与radio,u-checkbox-group与checkbox-group、u-ch
    回显问题:官方的回显没有uview的回显好用<u-radio-group@change="selfInjuryChange"placement="column"v-model="model.abilityAssessmentInfoDTO.idioctonia"><viewv-for="(item,index)infallList":......
  • Intel 显卡单机多卡 FSDP 模型 checkpointing 时 Assert Out
    Intel显卡单机多卡FSDP模型checkpointing时AssertOut Intel显卡单机多卡FSDP模型checkpointing时AssertOut现象根因顺藤摸瓜抽丝剥茧解法最后的话现象使用HuggingFaceTrainer在单机多卡环境下对LLAMA2-7B进行LoRAfinetuning时,......
  • dbt macro 中获取relation 的几种方法
    很多时候我们是希望在自己开发的macro中引用relation这样可以获取实际模型在数据库中的信息,方便数据的写入,或者进行查询实现动态能力,尤其在进行数据质量方便的处理时候,以下简单说明下一些可选的方法参考方法直接使用api.Relation.create创建新的如果知道一些信息(database......