Skip to content

Commit

Permalink
support quarter in postgres last day (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
seunghanhong authored Apr 16, 2021
1 parent 14fff40 commit 7c6de17
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions integration_tests/data/cross_db/data_last_day.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
date_day,date_part,result
2018-01-02,month,2018-01-31
2018-01-02,quarter,2018-03-31
2018-01-02,year,2018-12-31
,month,
1 change: 1 addition & 0 deletions integration_tests/models/cross_db_utils/test_last_day.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ with data as (
select
case
when date_part = 'month' then {{ dbt_utils.last_day('date_day', 'month') }}
when date_part = 'quarter' then {{ dbt_utils.last_day('date_day', 'quarter') }}
when date_part = 'year' then {{ dbt_utils.last_day('date_day', 'year') }}
else null
end as actual,
Expand Down
8 changes: 6 additions & 2 deletions macros/cross_db_utils/last_day.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ testing is required to validate that it will work on other dateparts.
{% macro postgres__last_day(date, datepart) -%}

{%- if datepart == 'quarter' -%}
{{ exceptions.raise_compiler_error(
"dbt_utils.last_day is not supported for datepart 'quarter' on this adapter") }}
-- postgres dateadd does not support quarter interval.
cast(
{{dbt_utils.dateadd('day', '-1',
dbt_utils.dateadd('month', '3', dbt_utils.date_trunc(datepart, date))
)}}
as date)
{%- else -%}
{{dbt_utils.default_last_day(date, datepart)}}
{%- endif -%}
Expand Down

0 comments on commit 7c6de17

Please sign in to comment.