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

如何修改DBT通用not_null测试的编译SQL以避免查询全列?

修改DBT通用not_null测试以仅扫描被测列

完全可以通过自定义通用测试模板解决这个问题,适配BigQuery对数据扫描量敏感的场景,具体操作如下:

  • 复制并自定义not_null测试模板
    在你的DBT项目根目录下创建tests/generic文件夹(如果还没有的话),新建not_null.sql文件,把DBT默认的not_null测试代码复制进去。默认模板代码大致是:

    {% test not_null(model, column_name) %}
        select *
        from {{ model }}
        where {{ column_name }} is null
    {% endtest %}
    
  • 修改查询逻辑为仅选目标列
    将模板里的select *替换成select {{ column_name }},修改后的代码如下:

    {% test not_null(model, column_name) %}
        select {{ column_name }}
        from {{ model }}
        where {{ column_name }} is null
    {% endtest %}
    

    这样编译后的SQL就会只查询被测列而非整张表,直接减少70GB左右的数据扫描量,匹配你的需求。

  • 验证修改结果
    运行dbt compile --select test:*生成编译后的SQL文件,或者执行dbt test --select [你的模型名],检查生成的SQL是否已经改为仅选择目标列。

额外提示:

  • 这个修改仅作用于当前DBT项目,不会影响全局的DBT默认测试逻辑。
  • 如果需要优化其他通用测试(比如unique、accepted_values),也可以用同样的方法复制对应模板并修改查询列的逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 10:03:31