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

dbt中length_selected_resources变量为空的原因及解决

问题解决:dbt中获取所选资源数量变量为空的问题

问题场景

你在使用dbt时,误以为length_selected_resources是官方提供的变量,用来返回通过--select指定的资源数量,于是在宏中写下这样的日志打印代码:

{% do log("length_selected_resources_LOG::"~length_selected_resources, info=true) %}

运行dbt run --select marts.growth后,发现日志里这个变量的值是空的,达不到预期效果。

原因分析

dbt官方并没有提供名为length_selected_resources的内置变量,你混淆了变量的正确写法。

正确解决方案

要获取所选资源的数量,应该使用内置变量selected_resources配合Jinja的length过滤器,正确写法如下:

{% do log("selected_resources_length_LOG::"~(selected_resources | length), info=true) %}

selected_resources是dbt提供的内置变量,存储了当前会话中通过--select等参数指定的所有资源列表,用| length过滤器即可得到列表的长度,也就是所选资源的数量。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 05:22:01