繁体   English   中英

DBT 使用宏设置变量

[英]DBT set variable using macros

我的目标是从表中获取最后 2 个日期并运行 insert_overwrite 以在大表上加载增量。 我试图通过调用我编写的宏在 model 中设置一个变量。 SQL 查询位于 BigQuery 中。

我收到一条错误消息。

'None' has no attribute 'table'

model内部

{% set dates = get_last_two_dates('window_start',source('raw.event','tmp')) %}

{% macro get_last_two_dates(target_column_name, target_table = this) %}

{% set query %}
select string_agg(format('%T',target_date),',') target_date_string
from (
SELECT distinct date({{ target_column_name }}) target_date
FROM {{ target_table }}
order by 1 desc
LIMIT 2
) a
{% endset %}

{% set max_value = run_query(query).columns[0][0] %}
{% do return(max_value) %}

{% endmacro %}

提前致谢。 如果您还有其他问题,请告诉我。

您可能需要用{% if execute %}块包装{% set max_value... %}

{% macro get_last_two_dates(target_column_name, target_table = this) %}

{% set query %}
select string_agg(format('%T',target_date),',') target_date_string
from (
SELECT distinct date({{ target_column_name }}) target_date
FROM {{ target_table }}
order by 1 desc
LIMIT 2
) a
{% endset %}

{% if execute %}
{% set max_value = run_query(query).columns[0][0] %}
{% else %}
{% set max_value = "" %}
{% endif %}
{% do return(max_value) %}

{% endmacro %}

原因是您的宏实际上运行了两次——一次是在 dbt 扫描所有模型以构建 DAG 时,第二次是在实际运行 model 时。 execute仅在第二次通过时才成立。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM