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

Merged
merged 4 commits into from
Dec 2, 2024

Conversation

MichelleArk
Copy link
Contributor

@MichelleArk MichelleArk commented Nov 27, 2024

resolves #1256

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

@MichelleArk MichelleArk Nov 27, 2024

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 feels 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
@MichelleArk MichelleArk requested a review from QMalcolm November 27, 2024 22:23
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 👍

@MichelleArk
Copy link
Contributor Author

New commit 238bae2 just cleans up some extraneous configuration in test code, that was accidentally left in from development. No functional changes.

@MichelleArk MichelleArk merged commit 5cc5d22 into main Dec 2, 2024
14 checks passed
@MichelleArk MichelleArk deleted the fix-microbatch-timestamp-tz branch December 2, 2024 16:05
github-actions bot pushed a commit that referenced this pull request Dec 2, 2024
colin-rogers-dbt pushed a commit that referenced this pull request Dec 2, 2024
(cherry picked from commit 5cc5d22)

Co-authored-by: Michelle Ark <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Microbatch timestamps incorrectly offset for system timezone in Snowflake
3 participants