SQL Server JINJA/dbt宏不需要空格?

wz3gfoph  于 2023-01-29  发布在  其他
关注(0)|答案(1)|浏览(169)

我正在尝试用jinja-sql开发一个dbt宏来为我的基本hub模型创建一个模板。我如何让我的硬编码字段直接位于我生成的代码下面呢?我已经尝试了所有的-来限制空格,但没有效果。

{{hub_table}} AS (
        SELECT
            {%- if not leading_commas -%}
            {%- for column in column_names %}
            {% if column not in hub_default_columns %}{{column | lower~","}}{%- endif -%}
            {%- endfor -%}
            {%- endif -%}
            dvloaddatetime                                      AS dvLoadDateTime,
            dvlastseendate                                      AS dvLastSeenDate,
            dvsourceid                                          AS dvSourceID
    
        FROM source

    )

    SELECT * 

    FROM {{hub_table}}

{% endset %}

{% if execute %}

{{ log(hub_model_sql, info=True) }}
{% do return(hub_model_sql) %}

{% endif %}
{% endmacro %}

我收到的输出如下所示。

WITH source AS (
    SELECT *

    FROM {{ source('-----', 'h_CLIENT') }}

    ),

    h_CLIENT AS (
        SELECT
            h_client_hashkey,
            clientsid,

            dvloaddatetime                                      AS dvLoadDateTime,
            dvlastseendate                                      AS dvLastSeenDate,
            dvsourceid                                          AS dvSourceID

        FROM source

    )

    SELECT *

    FROM h_CLIENT

我想要/希望收到的输出如下所示。

WITH source AS (
    SELECT *

    FROM {{ source('-----', 'h_CLIENT') }}

    ),

    h_CLIENT AS (
        SELECT
            h_client_hashkey,
            clientsid,
            dvloaddatetime                                      AS dvLoadDateTime,
            dvlastseendate                                      AS dvLastSeenDate,
            dvsourceid                                          AS dvSourceID

        FROM source

    )

    SELECT *

    FROM h_CLIENT
xxls0lw8

xxls0lw81#

空格控制只影响标签和紧挨在它前面或后面的标签之间的空格/换行符。对于循环,你需要想象循环的内容是重复的;对于if语句,if块的空格控制将被应用,即使内容为空。如果两个标记是空格的结尾,则任何一个标记的-将删除该空格。
在本例中,当if column not in hub_default_columns的值为false时,会得到额外的换行符,因为该标记的开头没有空格控制,所以每次循环都会得到一个换行符,即使if块的内容为空。
要解决这个问题,您需要允许if块中有空格,但要让该块本身删除周围的空格。类似地,您需要调整select之后的第一个if语句,以确保select之后有一个换行符,即使leading_commas为true。
这一点:

{%- set column_names = ["a", "b", "c", "d"]-%}
{%- set hub_default_columns = ["c", "d"]-%}
{%- set leading_commas = false %}
        SELECT
            {% if not leading_commas -%}
            {%- for column in column_names -%}
            {%- if column not in hub_default_columns -%}
            {{column | lower~","}}
            {% endif -%}
            {%- endfor -%}
            {%- endif -%}
            dvloaddatetime                                      AS dvLoadDateTime,
            dvlastseendate                                      AS dvLastSeenDate,
            dvsourceid                                          AS dvSourceID

编译至:

SELECT
            a,
            b,
            dvloaddatetime                                      AS dvLoadDateTime,
            dvlastseendate                                      AS dvLastSeenDate,
            dvsourceid                                          AS dvSourceID

相关问题