在 dbt 的 Jinja 宏中使用字符串作为参数
Using a string as an argument in a Jinja macro in dbt
我想创建一个 dbt 宏来简化以下几行
COALESCE(LOWER(tags::TEXT) ~ '.*my-first-query.*', FALSE),
COALESCE(LOWER(tags::TEXT) ~ '.*my-second-query.*', FALSE),
COALESCE(LOWER(tags::TEXT) ~ '.*my-other-query.*', FALSE)
我想将计算转换为函数,这样我就可以将行转换为
{{ extract_clean_tag(my-first-query) }},
{{ extract_clean_tag(my-second-query) }},
{{ extract_clean_tag(my-other-query) }}
如何在 dbt 中编写此宏?我在将字符串作为参数传递给函数时遇到问题。
到目前为止,我已经尝试过
{% macro extract_clean_tag(tag_regex) %}
COALESCE(LOWER(tags::TEXT) ~ '.*{{ tag_regex }}.*', FALSE)
{% endmacro %}
并通过 extract_clean_tag(my-first-query)
调用它,但是 dbt returns:
column "my-first-query" does not exist
您必须使用 'my-first-query' 作为参数来调用它,如下所示:
{{ extract_clean_tag('my-first-query') }}
如果没有引号,Jinja 解析器正在寻找一个名为 my-first-query
的变量,而引号表示您正在传递一个字符串。
另请参阅此处:https://docs.getdbt.com/docs/building-a-dbt-project/jinja-macros/#macros(cents_to_dollars 示例)
我想创建一个 dbt 宏来简化以下几行
COALESCE(LOWER(tags::TEXT) ~ '.*my-first-query.*', FALSE),
COALESCE(LOWER(tags::TEXT) ~ '.*my-second-query.*', FALSE),
COALESCE(LOWER(tags::TEXT) ~ '.*my-other-query.*', FALSE)
我想将计算转换为函数,这样我就可以将行转换为
{{ extract_clean_tag(my-first-query) }},
{{ extract_clean_tag(my-second-query) }},
{{ extract_clean_tag(my-other-query) }}
如何在 dbt 中编写此宏?我在将字符串作为参数传递给函数时遇到问题。
到目前为止,我已经尝试过
{% macro extract_clean_tag(tag_regex) %}
COALESCE(LOWER(tags::TEXT) ~ '.*{{ tag_regex }}.*', FALSE)
{% endmacro %}
并通过 extract_clean_tag(my-first-query)
调用它,但是 dbt returns:
column "my-first-query" does not exist
您必须使用 'my-first-query' 作为参数来调用它,如下所示:
{{ extract_clean_tag('my-first-query') }}
如果没有引号,Jinja 解析器正在寻找一个名为 my-first-query
的变量,而引号表示您正在传递一个字符串。
另请参阅此处:https://docs.getdbt.com/docs/building-a-dbt-project/jinja-macros/#macros(cents_to_dollars 示例)