-
Notifications
You must be signed in to change notification settings - Fork 507
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
Modernize legacy insert by period materialization #410
Closed
HorvathDanielMarton
wants to merge
29
commits into
dbt-labs:main
from
sspinc:fix-legacy-insert-by-period-macro
Closed
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
3493d3f
Update legacy macro to 0.21.latest incremental
HorvathDanielMarton 051a2e4
Raise error if no filter specified
HorvathDanielMarton 8536054
Create empty table first
HorvathDanielMarton 96ff9ed
Add configurations and period filter
HorvathDanielMarton 7ac2058
Make macro work on manually defined simple example
HorvathDanielMarton 364772b
Implement for loop with predefined time boundaries
HorvathDanielMarton 147494a
Move period boundary macros to helpers.sql
HorvathDanielMarton 3bb17d5
Modify helper macros to support Snowflake
HorvathDanielMarton a8d3259
Use macros to automate the for loop
HorvathDanielMarton 3e100c2
Refactor and add logging, comments
HorvathDanielMarton 760a33d
Use incremental_upsert() in the for loop
HorvathDanielMarton ec73c80
Introduce check_for_period_filter macro
HorvathDanielMarton f876d1d
Dispatch check_for_period_filter() macro
HorvathDanielMarton a961622
Remove unnecessary code, improve logging
HorvathDanielMarton ed6d55c
Handle full refresh
HorvathDanielMarton e53a801
Remove verbose CLI logging
HorvathDanielMarton b360a91
Update CHANGELOG.md
HorvathDanielMarton 53017a4
Update README.md
HorvathDanielMarton 888d781
Alias table expression in a helper function
HorvathDanielMarton 6fe4adb
Update integration tests
HorvathDanielMarton af2dd57
Set default period to week
HorvathDanielMarton cdba4b5
Use make_temp_relation() in for loop
HorvathDanielMarton 7c81020
Update README.md
HorvathDanielMarton f82f2ee
Run test_insert_by_period for all targets
HorvathDanielMarton 171e5df
Merge remote-tracking branch 'upstream/master' into fix-legacy-insert…
HorvathDanielMarton e1cdcee
Add aggressive CLI logging for debug purposes
HorvathDanielMarton 713f8a6
Add logging to get_period_boundaries()
HorvathDanielMarton 1ed1e58
Extend helper function logging
HorvathDanielMarton 9c680f9
Remove debug logging
HorvathDanielMarton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Move period boundary macros to helpers.sql
- Loading branch information
commit 147494a3a4bc14476c14b508d9ec81c9be7f94a4
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{% macro get_period_boundaries(target_schema, target_table, timestamp_field, start_date, stop_date, period) -%} | ||
{{ return(adapter.dispatch('get_period_boundaries', 'dbt_utils')(target_schema, target_table, timestamp_field, start_date, stop_date, period)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__get_period_boundaries(target_schema, target_table, timestamp_field, start_date, stop_date, period) -%} | ||
|
||
{% call statement('period_boundaries', fetch_result=True) -%} | ||
with data as ( | ||
select | ||
coalesce(max("{{timestamp_field}}"), '{{start_date}}')::timestamp as start_timestamp, | ||
coalesce( | ||
{{dbt_utils.dateadd('millisecond', | ||
-1, | ||
"nullif('" ~ stop_date ~ "','')::timestamp")}}, | ||
{{dbt_utils.current_timestamp()}} | ||
) as stop_timestamp | ||
from "{{target_schema}}"."{{target_table}}" | ||
) | ||
|
||
select | ||
start_timestamp, | ||
stop_timestamp, | ||
{{dbt_utils.datediff('start_timestamp', | ||
'stop_timestamp', | ||
period)}} + 1 as num_periods | ||
from data | ||
{%- endcall %} | ||
|
||
{%- endmacro %} | ||
|
||
{% macro get_period_sql(target_cols_csv, sql, timestamp_field, period, start_timestamp, stop_timestamp, offset) -%} | ||
{{ return(adapter.dispatch('get_period_sql', 'dbt_utils')(target_cols_csv, sql, timestamp_field, period, start_timestamp, stop_timestamp, offset)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__get_period_sql(target_cols_csv, sql, timestamp_field, period, start_timestamp, stop_timestamp, offset) -%} | ||
|
||
{%- set period_filter -%} | ||
("{{timestamp_field}}" > '{{start_timestamp}}'::timestamp + interval '{{offset}} {{period}}' and | ||
"{{timestamp_field}}" <= '{{start_timestamp}}'::timestamp + interval '{{offset}} {{period}}' + interval '1 {{period}}' and | ||
"{{timestamp_field}}" < '{{stop_timestamp}}'::timestamp) | ||
{%- endset -%} | ||
|
||
{%- set filtered_sql = sql | replace("__PERIOD_FILTER__", period_filter) -%} | ||
|
||
select | ||
{{target_cols_csv}} | ||
from ( | ||
{{filtered_sql}} | ||
) | ||
|
||
{%- endmacro %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will not work properly if
period = "month"
example:
2019-05-30 00:00:00 & 2019-05-31 00:00:00 => one day will not be processed.
Redshift can deal with the brackets, like
'{{start_timestamp}}'::timestamp + (interval '{{offset}} {{period}}' + interval '1 {{period}}')
, Snowflake will not like it.Suggestion (that I cannot test right away): calculate
offset_plus_one
asoffset + 1
and use it in setting the filter, likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this block and it works ok. Feel free to use it.