[英]DBT set variable using macros
my goal is to get the last 2 dates from the tables and run insert_overwrite to load incremental on a large table.我的目标是从表中获取最后 2 个日期并运行 insert_overwrite 以在大表上加载增量。 I am trying to set a variable inside the model by calling on the macros I wrote.
我试图通过调用我编写的宏在 model 中设置一个变量。 The SQL query is in BigQuery.
SQL 查询位于 BigQuery 中。
I get an error message.我收到一条错误消息。
'None' has no attribute 'table'
inside model model内部
{% set dates = get_last_two_dates('window_start',source('raw.event','tmp')) %}
macros宏
{% 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 %}
Thanks in advance.提前致谢。 let me know if you have any other questions.
如果您还有其他问题,请告诉我。
You probably need to wrap {% set max_value... %}
with an {% if execute %}
block:您可能需要用
{% 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 %}
The reason for this is that your macro actually gets run twice -- once when dbt is scanning all of the models to build the DAG, and a second time when the model is actually run.原因是您的宏实际上运行了两次——一次是在 dbt 扫描所有模型以构建 DAG 时,第二次是在实际运行 model 时。
execute
is only true for this second pass. execute
仅在第二次通过时才成立。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.