【问题标题】:Quoting for incremental model partitions to replace - dbt v0.17.2引用要替换的增量模型分区 - dbt v0.17.2
【发布时间】:2020-11-28 03:59:33
【问题描述】:

我正在使用 insert_overwrite 策略为 BigQuery 编写增量模型,并尝试使用变量设置 partitions_to_replace

{% set partitions_to_replace = [var('execution_date')] %}

只是为了测试编译,我在dbt_project.yml 中使用一个看起来像execution_date: '2020-01-01' 的变量进行编译。但是,似乎在具体化生成的合并语句中,没有引用日期,因此它失败并出现错误No matching signature for operator IN for argument types DATE and {INT64}。这是生成的SQL的相关sn-p:

when not matched by source
         and DBT_INTERNAL_DEST.visit_date in (
              2020-01-01
          ) 

有没有办法确保在变量周围加上引号?在我编写的 SQL 中使用变量时,我知道我可以将 var 函数用引号括起来,但在这种情况下,SQL 是由具体化生成的。

【问题讨论】:

    标签: sql jinja2 dbt


    【解决方案1】:

    这是一个公平的问题。出于灵活性考虑,具体化不会尝试将 partitions 值括在引号中,以此作为支持 SQL 表达式和文字作为潜在输入的一种方式。

    即您可能希望 merge 谓词为:

    when not matched by source
             and DBT_INTERNAL_DEST.visit_date in (
                  '2020-01-01'
              ) 
    

    但您可能同样希望它是:

        when not matched by source
             and DBT_INTERNAL_DEST.visit_date in (
                  date_sub(current_date, interval 1 day)
              ) 
    

    因此,您需要:

    • 通过用双引号括起来将字符串文字传递到您的var
    vars:
      execution_date: "'2020-01-01'"
    
    • 或处理set 语句中的附加引用,如下所示:
    {% set partitions_to_replace = [] %}
    {% for execution_date in [var('execution_date')] %}
        {% set ex_date %} '{{ execution_date }}' {% endset %}
        {% do partitions_to_replace.append(ex_date) %}
    {% endfor %}
    

    查看this related issue。 OP 对语法有一些建议,我们可以添加以使其更简单;我很想知道其中哪些对你有意义。

    【讨论】:

    • 谢谢!仅供参考 - jinja 解决方案有效,但将 var 本身包含在另一组引号中则无效。根据我是将单引号放在外面还是双引号,它会将日期解析为''2020-01-01''"'2020-01-01'"
    • 啊,明白了。报价很棘手。很高兴你成功了!
    【解决方案2】:

    我在通过cli 传递变量时遇到了同样的问题。即:

    dbt run --vars "execution_date: 2021-09-28"

    我的解决方法如下:

    • 设置partitions_to_replace 变量。

    {%- set partitions_to_replace -%} '{{var("execution_date")}}' {%- endset -%}

    • 然后在我的config 中,我只是将这个变量放在list

    partitions = [partitions_to_replace]

    • 最后,要在 SQL 模型中呈现变量,只需像这样调用它:

    partition_date = {{partitions_to_replace}}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2021-06-20
      • 2021-11-26
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      相关资源
      最近更新 更多