-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] Missing query-comments on insert statements for models with enforced contracts #9617
Comments
Thanks for reporting this @OFlanagan 👍 Which incremental strategies are you seeing this with?
Is it all of the above, so just one or two of them? |
We are currently using the default settings on redshift. I am not sure what incremental strategy that would correspond to but it is not merge. |
I'm seeing what you are saying about seeds, but I'm not seeing the same thing for incremental models. Can you share the relevant sections of your Here's what I'm seeing in mine which includes the query comment:
But seeds are not showing the query comment:
|
I probably can't share the logs for confidentiality reasons but for the tmp table: �[0m14:19:03.392616 [debug] [Thread-2 (]: Using redshift connection "model.redshift.tablename_staging" and then when inserting from tmp table into main table I can try updating to the latest version of dbt-core and see if that resolves the issue for incrementals |
Yeah, give it a shot with the latest versions for dbt-core and dbt-redshift and let me know how it goes. |
I updated to dbt-core 1.7.8 and dbt-redshift redshift=1.7.3 and still don't see the comments on the inserts |
@OFlanagan I'm not sure what is different between our environments... 🤔 See below for my exact project files -- could you try out this simplified project to eliminate anything in your context that might be affecting the query comments?
name: "my_project"
version: "1.0.0"
config-version: 2
profile: "redshift"
clean-targets:
- dbt_packages
- target
- logs
{{ config(materialized="table") }}
select 1 as id
{{ config(
materialized="incremental",
strategy="append",
) }}
select 1 as id
{{ config(
materialized="incremental",
strategy="delete_insert",
) }}
select 1 as id dbt clean
dbt build --full-refresh
dbt build Then examine |
When I run that project I do see the comments on the insert. However the incremental strategies appear to be performing a different set of operations to those in the project where I am encountering the issue.
and then the increment is loaded into the main table with
Both of these queries have the query comment. In my project I am seeing a create temporary table command with the DDL (I am using distkey and sortkey in redshift - probably the difference). This has the query-comment and then a separate insert into the temp table (with the query-comment) and then a delete from the main table (with query-comment) and then an insert into the main table from the tmp table (without the query-comment) I would assume that the key difference here is the dist style and sort key settings in redshift |
I tried without the diststyle and sort key specifications but that didn't fix it. |
I'm not sure why the query comment isn't showing. There's surely something different, but it's not yet clear what that difference is. Here's an idea for further troubleshooting:
Wanna give that a shot and see what happens? |
Great call. The issue appears when a contract is specified in the properties file Model File
in the properties file
Logs
|
You found it! Nice sleuth work It looks like model contracts have an insert statement that doesn't have the query comments applied. Namely, not all SQL statements in here include query comments. It's not limited to incremental models. See below for a simple reproducible example using Postgres.
{{ config(materialized="table") }}
select 1 as id
models:
- name: my_model
config:
contract:
enforced: true
columns:
- name: id
data_type: integer Subset of queries -- notice no query comment prior to insert: BEGIN
/* {"app": "dbt", "dbt_version": "1.7.8", "profile_name": "postgres", "target_name": "postgres", "node_id": "model.my_project.my_model"} */
create table "postgres"."dbt_dbeatty"."my_model__dbt_tmp"
(
id integer
)
;
insert into "postgres"."dbt_dbeatty"."my_model__dbt_tmp" (
id
)
(
select id
from (
select 1 as id
) as model_subq
);
/* {"app": "dbt", "dbt_version": "1.7.8", "profile_name": "postgres", "target_name": "postgres", "node_id": "model.my_project.my_model"} */
alter table "postgres"."dbt_dbeatty"."my_model" rename to "my_model__dbt_backup"
/* {"app": "dbt", "dbt_version": "1.7.8", "profile_name": "postgres", "target_name": "postgres", "node_id": "model.my_project.my_model"} */
alter table "postgres"."dbt_dbeatty"."my_model__dbt_tmp" rename to "my_model"
COMMIT Acceptance criteria
Out of scope
|
Thanks @dbeatty10 . I notice that you have identified "Query comments for seeds" as out of scope. Will there be a separate issue for that? |
Would you mind opening an issue for seeds specifically? You can just copy the same exact description you have for this issue and just make some slight tweaks. |
Is this a new bug in dbt-core?
Current Behavior
Queries run against redshift do not all have the query-comment appended at the start.
This query-comment is present for many CREATE and DELETE commands but is missing for inserts for:
incrementals (inserting into the incremental table from the temp table)
and seeds (the insert does not have query comments)
Expected Behavior
the documentation states "A string to inject as a comment in each query that dbt runs against your database"
This means that the above inserts should also include the query-comment
https://docs.getdbt.com/reference/project-configs/query-comment
Steps To Reproduce
dbt run --select seed_name
or similar for incrementalRelevant log output
No response
Environment
Which database adapter are you using with dbt?
redshift
Additional Context
No response
The text was updated successfully, but these errors were encountered: