将查询标签添加到DBT测试

发布于 2025-01-31 12:43:36 字数 346 浏览 3 评论 0 原文

我想询问您在DBT测试中添加雪花查询的查询标签。 是否有任何方法可以将查询标签添加到项目或模式YAML文件中?我已经尝试过,但我找不到方法。 我可以修改一些软件包,例如 dbt_utils.unique_combination_of_columns.combination_of_columns 添加查询标签语句。但是,如果我在下面的YAML中使用模式测试,在哪里以及如何定义测试的查询标签?

models:

name: dim_customers
  columns:
   - name: company
     tests:
       - not_null
       - unique

I would like to kindly ask you about adding query tags for Snowflake queries to dbt tests.
Is there any way to add query tags for tests to project or schema yaml file? I have tried but I could not find a way for it.
I can modify some packages like dbt_utils.unique_combination_of_columns.combination_of_columns adding query tag statement. But if I use the schema tests in the yaml like below, where and how can I define the query tags for tests?

models:

name: dim_customers
  columns:
   - name: company
     tests:
       - not_null
       - unique

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小耗子 2025-02-07 12:43:36

, in line 3 calls a macro named set_query_tag(), that it's defined

{% macro set_query_tag() -%}
  {# select some property of the node #}
  {% set new_query_tag = model.name %} 
  {% if new_query_tag %}
    {% set original_query_tag = get_current_query_tag() %}
    {{ log("Setting query_tag to '" ~ new_query_tag ~ "'. Will reset to '" ~ original_query_tag ~ "' after materialization.") }}
    {% do run_query("alter session set query_tag = '{}'".format(new_query_tag)) %}
    {{ return(original_query_tag)}}
  {% endif %}
  {{ return(none)}}
{% endmacro %}

这将覆盖默认的宏,并使用测试名称标记所有运行(类似于not_null__model_name__name__column_name)。如果您只想在测试中进行操作,则需要添加一些额外的话,如果/其他使用

node.resource_type == 'test' 

,请检查似乎这是几天前的可能性,因此现在可以按预期使用较旧版本来工作...

The default macro in the dbt package for snowflake, in line 3 calls a macro named set_query_tag(), that it's defined here. What you can do is to create a custom set_query_tag() in your dbt project, following this example (taken from here):

{% macro set_query_tag() -%}
  {# select some property of the node #}
  {% set new_query_tag = model.name %} 
  {% if new_query_tag %}
    {% set original_query_tag = get_current_query_tag() %}
    {{ log("Setting query_tag to '" ~ new_query_tag ~ "'. Will reset to '" ~ original_query_tag ~ "' after materialization.") }}
    {% do run_query("alter session set query_tag = '{}'".format(new_query_tag)) %}
    {{ return(original_query_tag)}}
  {% endif %}
  {{ return(none)}}
{% endmacro %}

This will override the default macro and tag all your runs with the test name (something like not_null__model_name__column_name). If you only want to do it with tests, you need to add some extra if/else using

node.resource_type == 'test' 

Also, check this it seems that this is possible since a few days ago, so it could now work as expected with older versions...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文