使用DBT中使用Star Macro获取列的名称和类型
使用Star宏,除了列名称外,还有一种方法可以获取列数据类型(布尔值,数值等)吗?
例如,此查询使用Star宏从参考表收集列名,将其保存为数组变量 column_names
,然后我循环循环此数组,并将最大函数应用于所有列函数。
{% set column_names = star(
from=ref_table,
except=["a", "b", "c"],
as_list=True)
%}
select
date_trunc('week', day) as week,
name,
{%- for col in column_names %}
max({{ col|lower }}) as {{ col | lower }}{%- if not loop.last %},{{ '\n ' }}{% endif %}
{%- endfor %}
from {{ ref('my_table_name') }}
group by 1, 2
我想有条件地将最大函数应用于布尔列。
这可能看起来像是类似的
{%- for col in column_names %}
{% if is_boolean(col) %}
max({{ col|lower }}) as {{ col | lower }}{%- if not loop.last %},{{ '\n ' }}{% endif %}
{% endif %}
{%- endfor %}
,但是问题是星形宏将列名作为字符串传递,因此它不会随身携带任何元数据。
我如何在此处获取列数据类型?
数据仓库:雪花
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以查看
dbt_utils.star的源=“ noreferrer”>此处
在引擎盖下,它使用
dbt_utils.get_filtered_columns_in_relation
。该宏还返回列名。然而!该宏使用内置,返回 data_type 属性。因此,您的代码变为:
You can view the source for
dbt_utils.star
hereUnder the hood, it uses
dbt_utils.get_filtered_columns_in_relation
. That macro also just returns column names. However! that macro uses the built-inadapter.get_columns_in_relation
, which returns a list of Column objects, which have adata_type
property.So your code becomes: