简体   繁体   English

使用 DBT 在 bigquery 中更新表

[英]Updating a table in bigquery using DBT

I am trying to update a table in bigquery using DBT.我正在尝试使用 DBT 更新 bigquery 中的表。 The below command executes in bigquery;下面的命令在 bigquery 中执行;

Update {{ ref('my_table') }}
SET variable = 'value'
WHERE lower(variable) LIKE '%XX%' or lower(variable) like '%YY%'

However when I run it in DBT I get the following error但是,当我在 DBT 中运行它时,出现以下错误

Server error: Database Error in rpc request (from remote system)
Syntax error: Expected end of input but got keyword LIMIT at [4:1]

Does anyone know why this is happening and how to resolve?有谁知道为什么会这样以及如何解决?

It's a little unintuitive at first I know but with dbt, every model is a select statement .起初我知道这有点不直观,但对于 dbt, 每个 model 都是一个 select 语句

You should instead think of doing something like:您应该考虑做类似的事情:

with cte as (
     select * from {{ ref('my_table') }}
     where <criteria>
)
 select col1,
        col2,
        'value' as col3
 from cte

Or possibly even simpler:或者可能更简单:

SELECT 
  'value' as variable
FROM {{ ref('my_table') }}
WHERE lower(variable) LIKE '%XX%' or lower(variable) like '%YY%'

Simply because during the dbt run cycle, the new values will be materialized into the new model.仅仅是因为在dbt run周期中,新值将具体化为新的 model。

However, if you are looking for ways to clean underlying tables in a DRY way, I'd highly recommend this thread Modeling SQL Update Statements from the dbt discourse for some patterns on managing statements which handle specific value cleaning.但是,如果您正在寻找以 DRY 方式清理基础表的方法,我强烈推荐这个线程建模 SQL 来自 dbt 话语的更新语句,了解处理特定值清理的管理语句的一些模式。 Example from Kyle Ries:凯尔·里斯的例子:

{% set mappings = {'something': 'boo', 'something-else': 'boo-else'} %}

with source as (
        select * from {{ ref(‘stg_foobar’) }}
),

final as ( 

        select
            case
              {% for old, new in mappings %}
                when other_column like ‘{{old}}’ then ‘{{new}}’
              {% endfor %}
            end as column_name
        from
            source

)

select * from final

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM