Open crystalro0 opened 1 year ago
Thanks for reporting this @crystalro0 🤩
Adding an override to alter_relation_add_remove_columns
like you mentioned worked for me as well.
If we update the implementation of default__alter_relation_add_remove_columns
, I'd suggest using adapter.quote
like elsewhere in that file to achieve the same effect:
{% for column in add_columns %}
add column {{ adapter.quote(column.name) }} {{ column.data_type }}{{ ',' if not loop.last }}
{% endfor %}{{ ',' if remove_columns | length > 0 }}
{% for column in remove_columns %}
drop column {{ adapter.quote(column.name) }} {{ ',' if not loop.last }}
{% endfor %}
we face the same issue with postgres adapter. Any chance it will be fixed?
let's also validate against dbt-postgres
Same deal on BQ even if you set the quote
config on the newly added column:
-- models/stg_customers.sql
{{
config(
materialized='incremental',
unique_key='id',
on_schema_change='sync_all_columns'
)
}}
select 1 id, 'alice' as first_name
# models/schema.yml
models:
- name: stg_customers
columns:
- name: id
- name: first_name
Do a first build without issues.
$ dbt --debug build --full-refresh
00:17:25 On model.my_dbt_project.stg_customers: /* {"app": "dbt", "dbt_version": "1.8.5", "profile_name": "all", "target_name": "bq", "node_id": "model.my_dbt_project.stg_customers"} */
create or replace table `cse-sandbox-319708`.`dbt_jyeo`.`stg_customers`
OPTIONS()
as (
select 1 id, 'alice' as first_name
);
00:17:26 BigQuery adapter: https://console.cloud.google.com/bigquery?project=cse-sandbox-319708&j=bq:US:49d34f81-2697-4f4e-9928-d6bbf353f337&page=queryresults
00:17:29 Sending event: {'category': 'dbt', 'action': 'run_model', 'label': '1d114b72-c90f-42be-9dfc-173e7831f4c2', 'context': [<snowplow_tracker.self_describing_json.SelfDescribingJson object at 0x12bae6b50>]}
00:17:29 1 of 1 OK created sql incremental model dbt_jyeo.stg_customers ................. [CREATE TABLE (1.0 rows, 0 processed) in 7.09s]
Add a new column but using a reserved SQL keyword:
-- models/stg_customers.sql
{{
config(
materialized='incremental',
unique_key='id',
on_schema_change='sync_all_columns'
)
}}
select 1 id, 'alice' as first_name, 'premium' as `group`
# models/schema.yml
models:
- name: stg_customers
columns:
- name: id
- name: first_name
- name: group
quote: true
$ dbt --debug build
00:18:50 On model.my_dbt_project.stg_customers: /* {"app": "dbt", "dbt_version": "1.8.5", "profile_name": "all", "target_name": "bq", "node_id": "model.my_dbt_project.stg_customers"} */
create or replace table `cse-sandbox-319708`.`dbt_jyeo`.`stg_customers__dbt_tmp`
OPTIONS(
expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 12 hour)
)
as (
select 1 id, 'alice' as first_name, 'premium' as `group`
);
00:18:53 BigQuery adapter: https://console.cloud.google.com/bigquery?project=cse-sandbox-319708&j=bq:US:5fdf3b2d-82e2-46f6-8ae7-ece16515cb4a&page=queryresults
00:18:59
In `cse-sandbox-319708`.`dbt_jyeo`.`stg_customers`:
Schema changed: True
Source columns not in target: [<BigQueryColumn group (STRING, NULLABLE)>]
Target columns not in source: []
New column types: []
00:18:59 On model.my_dbt_project.stg_customers: /* {"app": "dbt", "dbt_version": "1.8.5", "profile_name": "all", "target_name": "bq", "node_id": "model.my_dbt_project.stg_customers"} */
alter table `cse-sandbox-319708`.`dbt_jyeo`.`stg_customers`
add column group STRING
00:19:00 BigQuery adapter: https://console.cloud.google.com/bigquery?project=cse-sandbox-319708&j=bq:US:28b881d0-a878-452b-9ba1-3f9bbc404e67&page=queryresults
00:19:00 BigQuery adapter: Retry attempt 1 of 1 after error: BadRequest('Syntax error: Unexpected keyword GROUP at [6:27]; reason: invalidQuery, location: query, message: Syntax error: Unexpected keyword GROUP at [6:27]')
00:19:01 BigQuery adapter: https://console.cloud.google.com/bigquery?project=cse-sandbox-319708&j=bq:US:2c470b5b-5562-4550-ad66-9e473e1725ee&page=queryresults
00:19:01 BigQuery adapter: https://console.cloud.google.com/bigquery?project=cse-sandbox-319708&j=bq:US:2c470b5b-5562-4550-ad66-9e473e1725ee&page=queryresults
00:19:01 Database Error in model stg_customers (models/stg_customers.sql)
Syntax error: Unexpected keyword GROUP at [6:27]
00:19:01 Sending event: {'category': 'dbt', 'action': 'run_model', 'label': '9c085ee1-adb6-4667-9365-7e77f2c75256', 'context': [<snowplow_tracker.self_describing_json.SelfDescribingJson object at 0x149853710>]}
00:19:01 1 of 1 ERROR creating sql incremental model dbt_jyeo.stg_customers ............. [ERROR in 11.46s]
^ In this scenario, the user has even gone the extra mile to set the quote
config on the column - but similar not respected when being added to the existing table.
This issue should be renamed - something like:
Across all adapters, dbt does not quote column names when being added to an existing incremental model via on_schema_change
Is this a new bug in dbt-core?
Current Behavior
While working with DBT incremental config:
on_schema_change='append_new_columns'
The append new columns flag is not able to capture the correct case-sensitive column name and add it to the incremental table causing the run to fail.Expected Behavior
Be able to apply to add the new columns to an incremental table with the
'append_new_columns'
config.Steps To Reproduce
Relevant log output
Environment
Which database adapter are you using with dbt?
snowflake
Additional Context
suggested the following as a workaround:
saved in project macro folder.