Skip to content
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

Use timestamp_tz type in microbatch delete DDL #1257

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

MichelleArk
Copy link
Contributor

@MichelleArk MichelleArk commented Nov 27, 2024

resolves ##1256
docs dbt-labs/docs.getdbt.com/#

Problem

When specifying a timestamp like TIMESTAMP '2024-11-05 00:00:00+00:00', snowflake uses the system timezone, even if the timestamp has offsets. This causes issues in microbatch because the delete DDL statement is leveraging the TIMESTAMP typing, and is deleting the wrong records, potentially causing duplicate rows to be inserted.

Solution

  • explicitly use to_timestamp_tz when computing incremental predicates for microbatch DDL
  • update tests to explicitly have timezone_tz input types to UTC (as we recommend for microbatch inputs)

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc) or this PR has already received feedback and approval from Product or DX

@cla-bot cla-bot bot added the cla:yes label Nov 27, 2024
@@ -58,10 +58,10 @@

{#-- Add additional incremental_predicates to filter for batch --#}
{% if model.config.get("__dbt_internal_microbatch_event_time_start") -%}
{% do incremental_predicates.append("DBT_INTERNAL_TARGET." ~ model.config.event_time ~ " >= TIMESTAMP '" ~ model.config.__dbt_internal_microbatch_event_time_start ~ "'") %}
{% do incremental_predicates.append("DBT_INTERNAL_TARGET." ~ model.config.event_time ~ " >= to_timestamp_tz('" ~ model.config.__dbt_internal_microbatch_event_time_start ~ "')") %}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, just using the raw timestamp (that includes UTC offset, which model.config.__dbt_internal_microbatch_event_time_start does) works well. E.g. just removing TIMESTAMP would have been enough here. However, the to_timestamp_tz piece for more intentional so I've opted for that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how using TIMESTAMP was causing the issue 🫠 I like th emove to using to_timestamp_tz instead of just removing TIMESTAMP 👍

@MichelleArk MichelleArk marked this pull request as ready for review November 27, 2024 21:29
@MichelleArk MichelleArk requested a review from a team as a code owner November 27, 2024 21:29
Copy link
Contributor

@QMalcolm QMalcolm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: You are crushiiiing it

@@ -58,10 +58,10 @@

{#-- Add additional incremental_predicates to filter for batch --#}
{% if model.config.get("__dbt_internal_microbatch_event_time_start") -%}
{% do incremental_predicates.append("DBT_INTERNAL_TARGET." ~ model.config.event_time ~ " >= TIMESTAMP '" ~ model.config.__dbt_internal_microbatch_event_time_start ~ "'") %}
{% do incremental_predicates.append("DBT_INTERNAL_TARGET." ~ model.config.event_time ~ " >= to_timestamp_tz('" ~ model.config.__dbt_internal_microbatch_event_time_start ~ "')") %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how using TIMESTAMP was causing the issue 🫠 I like th emove to using to_timestamp_tz instead of just removing TIMESTAMP 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants