首页 > 其他分享 >dbt dbt-audit-helper 包compare_relation_columns 处理简单说明

dbt dbt-audit-helper 包compare_relation_columns 处理简单说明

时间:2024-06-01 09:01:41浏览次数:12  
标签:audit compare type cols relation data dbt columns

dbt dbt-audit-helper 包在进行compare_relation_columns处理的时候进行数据表列字段创建顺序的判断

参考使用

我按照test 处理的,同时进行的测试异常进行存储

  • 使用
{{ audit_helper.compare_relation_columns(
    a_relation = source("dalongdemo","mytest_appv2"),
    b_relation = source("dalongdemo","mytest_appv3")
)}}
  • 存储效果

内部处理参考

  • 参考macro 定义
{% macro compare_relation_columns(a_relation, b_relation) %}
  {{ return(adapter.dispatch('compare_relation_columns', 'audit_helper')(a_relation, b_relation)) }}
{% endmacro %}
 
{% macro default__compare_relation_columns(a_relation, b_relation) %}
# 自己包装的,实际内部默认使用了adapter 的get_columns_in_relation,但是对于不同数据库可能会有差异,基于dispatch 模式进行了处理
with a_cols as (
    {{ audit_helper.get_columns_in_relation_sql(a_relation) }}
),
 
b_cols as (
    {{ audit_helper.get_columns_in_relation_sql(b_relation) }}
)
# 通过full outer join 进行实际上的判断处理,会结合类型,名称,以及字段创建的顺序
select
    column_name,
    a_cols.ordinal_position as a_ordinal_position,
    b_cols.ordinal_position as b_ordinal_position,
    a_cols.data_type as a_data_type,
    b_cols.data_type as b_data_type,
    coalesce(a_cols.ordinal_position = b_cols.ordinal_position, false) as has_ordinal_position_match,
    coalesce(a_cols.data_type = b_cols.data_type, false) as has_data_type_match,
    a_cols.data_type is not null and b_cols.data_type is null as in_a_only,
    b_cols.data_type is not null and a_cols.data_type is null as in_b_only,
    b_cols.data_type is not null and a_cols.data_type is not null as in_both
from a_cols
full outer join b_cols using (column_name)
order by coalesce(a_cols.ordinal_position, b_cols.ordinal_position)
 
{% endmacro %}
   
{% macro get_columns_in_relation_sql(relation) %}
 
{{ adapter.dispatch('get_columns_in_relation_sql', 'audit_helper')(relation) }}
 
{% endmacro %}
 
{% macro default__get_columns_in_relation_sql(relation) %}
 
  {% set columns = adapter.get_columns_in_relation(relation) %}
  {% for column in columns %}
    select 
      {{ dbt.string_literal(column.name) }} as column_name, 
      {{ loop.index }} as ordinal_position,
      {{ dbt.string_literal(column.data_type) }} as data_type
 
  {% if not loop.last -%}
    union all 
  {%- endif %}
  {% endfor %}
   
{% endmacro %}
 
{% macro redshift__get_columns_in_relation_sql(relation) %}
  {# You can't store the results of an info schema query to a table/view in Redshift, because the data only lives on the leader node #}
  {{ return (audit_helper.default__get_columns_in_relation_sql(relation)) }}
{% endmacro %}
   
{% macro snowflake__get_columns_in_relation_sql(relation) %}
{#-
From: https://github.com/dbt-labs/dbt/blob/dev/louisa-may-alcott/plugins/snowflake/dbt/include/snowflake/macros/adapters.sql#L48
Edited to include ordinal_position
-#}
  select
      ordinal_position,
      column_name,
      data_type,
      character_maximum_length,
      numeric_precision,
      numeric_scale
 
  from
  {{ relation.information_schema('columns') }}
 
  where table_name ilike '{{ relation.identifier }}'
    {% if relation.schema %}
    and table_schema ilike '{{ relation.schema }}'
    {% endif %}
    {% if relation.database %}
    and table_catalog ilike '{{ relation.database }}'
    {% endif %}
  order by ordinal_position
{% endmacro %}
   
{% macro postgres__get_columns_in_relation_sql(relation) %}
{#-
From: https://github.com/dbt-labs/dbt/blob/23484b18b71010f701b5312f920f04529ceaa6b2/plugins/postgres/dbt/include/postgres/macros/adapters.sql#L32
Edited to include ordinal_position
-#}
  select
      ordinal_position,
      column_name,
      data_type,
      character_maximum_length,
      numeric_precision,
      numeric_scale
 
  from {{ relation.information_schema('columns') }}
  where table_name = '{{ relation.identifier }}'
    {% if relation.schema %}
    and table_schema = '{{ relation.schema }}'
    {% endif %}
  order by ordinal_position
{% endmacro %}
   
{% macro bigquery__get_columns_in_relation_sql(relation) %}
 
  select
      ordinal_position,
      column_name,
      data_type
 
  from `{{ relation.database }}`.`{{ relation.schema }}`.INFORMATION_SCHEMA.COLUMNS
  where table_name = '{{ relation.identifier }}'
 
{% endmacro %}

说明

dbt dbt-audit-helper 包compare_relation_columns 内部处理实际上使用了还是标准INFORMATION_SCHEMA.COLUMNS 的能力,只是
包装了公共方法使用上更加方便了

参考资料

https://github.com/dbt-labs/dbt-audit-helper?tab=readme-ov-file#compare_relation_columns-source

标签:audit,compare,type,cols,relation,data,dbt,columns
From: https://www.cnblogs.com/rongfengliang/p/18167940

相关文章

  • 【使用技巧】CodeDecom.exe批量反编译JAR包+Beyond Compare对比
    使用工具进行批量反编译+差异对比,检查确认补丁变更D:\tmp\test\CodeDecom>CodeDecom.exe source D:\tmp\test\jarForder  D:\tmp\test\codeForder......
  • Java 理解和使用compareTo和compare方法
    在Java编程中,经常需要对对象进行排序。为了实现排序功能,Java提供了两种主要的方法:compareTo和compare。尽管它们都用于比较对象,但它们在使用场景和定义位置上有所不同。本文将详细探讨这两种方法的区别、用途以及如何在实际项目中使用它们。compareTo方法compareTo方法......
  • Delphi CxGrid/CxDBTreeList等将排序筛选条件改为中文方法
    Delphi CxGrid/CxDBTreeList等将排序筛选条件改为中文方法一、加入cxLocalizer控件二、在FormCreate里加入以下代码procedureTForm1.FormCreate(Sender:TObject);begin cxLocalizer1.LoadFromResource(HInstance); cxLocalizer1.Language:='中文(简体,中国)';......
  • dbt adapter 的get_relation 简单说明
    dbt的adapter.get_relation可以方便的获取存在的relcation信息,以下是一个简单说明参考实现内部处理@available.parse_nonedefget_relation(self,database:str,schema:str,identifier:str)->Optional[BaseRelation]:relations_list=self.lis......
  • dbt Relation check_schema_exists 一个有意思的功能
    dbt内部总有一些隐藏的小细节,官方文档没有说明,但是在一些adapter实现中包含,一些是关于check_schema_exists的一些说明内部处理dbt/adapters/sql/impl.pydefcheck_schema_exists(self,database:str,schema:str)->bool:information_schema=self.Re......
  • ABP 框架 AutoMapper 映射实体时提示 Unmapped members were found 缺少 FullAuditedE
    在配置MapperProfile的时候, 运行提示映射出错:对于Workflow->WorkflowDto和CreateUpdateWorkflowDto->Workflow的映射,存在没有映射的属性 IsDeleted,DeleterId,DeletionTime,LastModificationTime,LastModifierId,CreationTime和CreatorId。我之前写代码都没有映射这......
  • BeyondCompare破解版-解压即用
    链接:https://pan.baidu.com/s/1vGKnPh6WMhk_bYJyJyKqXw提取码:dasf打开软件找到帮助——关于beyondcompare,如下破解成功。操作简单,能正常使用原文链接:https://blog.csdn.net/sinat_39684057/article/details/98053227......
  • dbt snapshot 处理简单说明
    dbt的snapshot实际上也是一种物化处理,支持与test,docs,稍有不同就是dbt没定义独立的block扩展,以下是一个简单说明dbt目前默认的snapshot是基于了scd2模型使用包含了配置以及snapshot定义,配置支持dbt_project项目级以及独立snapshot定义,对于snapshot是需要指定策略的......
  • dbt fromyaml 上下文方法简单说明
    fromyaml上下文方法可以用来加载yaml内容,属于一个工具类,比如automate-dv就使用了不少方法参考使用{%-setinfo-%}source_model:raw_staging:"raw_customer"derived_columns:SOURCE:"!1"LOAD_DATETIME:"CRM_DATA_INGESTION_TIME"E......
  • dbt macro 中获取relation 的几种方法
    很多时候我们是希望在自己开发的macro中引用relation这样可以获取实际模型在数据库中的信息,方便数据的写入,或者进行查询实现动态能力,尤其在进行数据质量方便的处理时候,以下简单说明下一些可选的方法参考方法直接使用api.Relation.create创建新的如果知道一些信息(database......