You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在DBT测试中引用其他数据表?实操方案失效求解答

在DBT中运行涉及其他数据表的测试方案

可以在DBT中运行关联其他数据表的测试,你提供的配置无法运行是因为使用了DBT不支持的assertion语法,以下是两种正确的实现方式:

方法一:自定义SQL测试(原生DBT方式)

  1. 在项目的tests目录下创建测试文件,比如test_a_join_b_row_count.sql,内容如下:
SELECT COUNT(*) AS actual_count
FROM {{ ref('a') }} a
JOIN {{ ref('b') }} b ON b.account_id = a.id
HAVING actual_count != 12345
  1. 在模型a的配置中引用这个自定义测试:
models:
  - name: a
    description: table A
    docs:
      show: true
    config:
      tags: [test]
    tests:
      - dbt_expectations.expect_table_row_count_to_equal:
          value: 5555
      - test_a_join_b_row_count

方法二:使用dbt_expectations内置测试函数

直接使用dbt_expectations提供的expect_query_result_to_equal测试,替换原配置中的assertion部分:

models:
  - name: a
    description: table A
    docs:
      show: true
    config:
      tags: [test]
    tests:
      - dbt_expectations.expect_table_row_count_to_equal:
          value: 5555
      - dbt_expectations.expect_query_result_to_equal:
          query: |
            SELECT COUNT(*) FROM {{ ref('a') }} a JOIN {{ ref('b') }} b ON b.account_id = a.id
          value: 12345

关键说明

DBT原生配置中没有assertion这个字段,你之前的配置语法不符合DBT规范,因此无法执行。上述两种方式均符合DBT的测试机制,其中:

  • 自定义SQL测试适合复杂逻辑的场景,完全由你控制查询逻辑;
  • 使用dbt_expectations的内置测试则更简洁,符合该工具的标准化测试风格。

内容的提问来源于stack exchange,提问作者Raksha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 16:03:20