Generate list of columns to exclude when using dbt_utils.unpivot

huangapple go评论62阅读模式
英文:

Generate list of columns to exclude when using dbt_utils.unpivot

问题

我正在使用 dbt 在 Snowflake 数据库中创建模型。我的一个模型有很多列,我想使用 dbt_utils.unpivot 对其中一些列进行展开。我想通过使用一个宏来创建列表,该宏使用正则表达式模式来过滤所有列,而不是硬编码要排除在展开之外的列的列表。

我的模型如下:

WITH unpivot as (
{{ 
  dbt_utils.unpivot(
  relation=ref('my_other_modell'),
  exclude= get_column_names('my_other_modell',regex='^F.*'),
  field_name='vairable_name',
  value_name='vairable_value'
) }})
SELECT * FROM unpivot

我的宏如下:

{% macro get_column_names(model_name, regex='+*') %}
    {%- set columns = adapter.get_columns_in_relation(ref(model_name)) -%}
    {%- set column_names = [] -%}
    {%- set re = modules.re %}
    
    {%- for column in columns %}
          {%- if not re.match(regex, column.name) %}
            {%- set _ = column_names.append(column.name) %}
        {%- endif %}
    {%- endfor %}
    
    {{ column_names}}
{% endmacro %}

我已经尝试了几种宏的变体,例如将 {{ column_names }} 替换为 {{ column_names|join(',') }},但我总是收到语法错误,如:

在第16行的位置0处出现语法错误,意外的 ','。
在第17行的位置17处出现语法错误,意外的 ','。
在第18行的位置17处出现语法错误,意外的 ','。
在第19行的位置17处出现语法错误,意外的 ','。
在第20行的位置17处出现语法错误,意外的 ','。

感谢您的建议 Generate list of columns to exclude when using dbt_utils.unpivot

英文:

I am using dbt to create models inside a Snowflake Database. One of my models has many columns, and I want to unpivot some of them using dbt_utils.unpivot. Instead of hardcoding the list of columns I want to exclude from the unpivot, I would like to create the list using a macro that filters all the columns using a regex pattern.

My model looks like this:

WITH unpivot as (

{{ 
  dbt_utils.unpivot(
  relation=ref('my_other_modell'),
  exclude= get_column_names('my_other_modell',regex='^F.*'),
  field_name='vairable_name',
  value_name='vairable_value'
) }})



SELECT * FROM unpivot

and my macro looks like this:

{% macro get_column_names(model_name, regex='+*') %}
    {%- set columns = adapter.get_columns_in_relation(ref(model_name)) -%}
    {%- set column_names = [] -%}
    {%- set re = modules.re %}
    
    {%- for column in columns %}
          {%- if not re.match(regex, column.name) %}
            {%- set _ = column_names.append(column.name) %}
        {%- endif %}
    {%- endfor %}
    
    {{ column_names}}
{% endmacro %}

I have already tried several variations of the macro, like replacing {{ column_names }} with {{ column_names|join(',') }}, but I always get a syntax error like:

syntax error line 16 at position 0 unexpected ','.
syntax error line 17 at position 17 unexpected ','.
syntax error line 18 at position 17 unexpected ','.
syntax error line 19 at position 17 unexpected ','.
syntax error line 20 at position 17 unexpected ','.

Any advice is appreciated Generate list of columns to exclude when using dbt_utils.unpivot

答案1

得分: 1

将我的宏直接集成到dtb_utils.unpivot宏中解决了我的问题。从日志中判断,似乎在将我的宏的输出解析为排除参数时,它被分解为每个单个字符(可能是通过某种选择语句)。

英文:

Integrating my macro directly into the dtb_utils.unpivot macro solved my problem. Judging from the logs, it seems that when parsing the output of my macro to the exclude parameter, it gets decomposed into every single character (possibly by some sort of select statement).

huangapple
  • 本文由 发表于 2023年6月5日 23:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定