dbt / jinja:对来自dbt_utils的每个列添加合并.Get_filtered_columns_in_rel



我试图利用循环将合并函数添加到由dbt_utils交付的每列。Get_filtered_columns_in_relation宏(见附图)。我正在遵循文档中提到的示例(https://github.com/dbt-labs/dbt-utils/tree/0.9.2/#get_filtered_columns_in_relation-source),但我仍然得到像

这样的错误
001003 (42000): SQL compilation error:
13:54:48    syntax error line 27 at position 2 unexpected '"ARTICLE_REFERENCE_GROUP"'.
13:54:48    syntax error line 39 at position 2 unexpected 'from'.
13:54:48    syntax error line 51 at position 0 unexpected ')'.

在我看来,关于报价或类似的地方仍然有问题。如果在列别名中添加额外的单引号,则得到

14:21:39    syntax error line 13 at position 36 unexpected ''COMPANY_NR''.
14:21:39    syntax error line 14 at position 49 unexpected ''ARTICLE_REFERENCE_GROUP''.

等。

你有这个/jinja/循环的经验,可以帮我修复它吗?提前感谢!

我的当前代码:

with source as (
{% set coalescecols = adapter.get_columns_in_relation(from=source('stg_poc_dbt','prices'), except=['_business_dt','_country_cd','_input_filename','_row_nr','_dq_st','_dq_err_msg','_run_id','_batch_id']) %}
select 
{% for col in coalescecols %}
coalesce({{ col.name }}, '') as {{ col.name }}
{%- if not loop.last %},{% endif -%}
{% endfor %}
--{{ dbt_utils.star(from=source('stg_poc_dbt','prices'), except=['_business_dt','_country_cd','_input_filename','_row_nr','_dq_st','_dq_err_msg','_run_id','_batch_id'])}}
from 
{{ source('stg_poc_dbt','prices') }}

问题实际上是您试图注释掉的那行!

首先对Jinja进行模板化,然后将SQL传递给数据库。这条线:

--{{ dbt_utils.star(from=source('stg_poc_dbt','prices'), except=['_business_dt','_country_cd','_input_filename','_row_nr','_dq_st','_dq_err_msg','_run_id','_batch_id'])}}

将被编译成不同行上的列名列表,因此sql注释--将只删除这些列名中的第一个。

Jinja注释采用{# ... #}的形式,所以如果你想注释掉对star的调用,你需要这样做:

{# {{ dbt_utils.star(from=source('stg_poc_dbt','prices'), except=['_business_dt','_country_cd','_input_filename','_row_nr','_dq_st','_dq_err_msg','_run_id','_batch_id'])}} #}
一般来说,调试jinja的最佳方法是查看编译后的代码。在执行dbt rundbt builddbt compiled之后,您可以在项目的target/compiled目录中找到它。这将向您显示在jinja模板化之后生成的SQL,并且您可以在编译后的代码中看到问题。

最新更新