From 85957b3f0d11c1b563d2e5524f3a205ba074d5f4 Mon Sep 17 00:00:00 2001 From: Han Hong <52536323+seunghanhong@users.noreply.github.com> Date: Sat, 17 Apr 2021 04:32:09 +1000 Subject: [PATCH] Dupport quarter in postgres last day (#333) --- CHANGELOG.md | 2 ++ integration_tests/data/cross_db/data_last_day.csv | 1 + integration_tests/models/cross_db_utils/test_last_day.sql | 1 + macros/cross_db_utils/last_day.sql | 8 ++++++-- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 601464939..f34ce7743 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,13 @@ * Add new schema test, `not_accepted_values` ([#284](https://github.com/fishtown-analytics/dbt-utils/pull/284) [@JavierMonton](https://github.com/JavierMonton)) * Support a new argument, `zero_length_range_allowed` in the `mutually_exclusive_ranges` test ([#307](https://github.com/fishtown-analytics/dbt-utils/pull/307) [@zemekeng](https://github.com/zemekeneng)) * Add new schema test, `sequential_values` ([#318](https://github.com/fishtown-analytics/dbt-utils/pull/318), inspired by [@hundredwatt](https://github.com/hundredwatt)) +* Support `quarter` in the `postgres__last_day` macro ([#333](https://github.com/fishtown-analytics/dbt-utils/pull/333/files), [@seunghanhong](https://github.com/seunghanhong)) ## Fixes * Handle booleans gracefully in the unpivot macro ([#305](https://github.com/fishtown-analytics/dbt-utils/pull/305) [@avishalom](https://github.com/avishalom)) * Fix a bug in `get_relation_by_prefix` that happens with Snowflake external tables. Now the macro will retrieve tables that match the prefix which are external tables ([#350](https://github.com/fishtown-analytics/dbt-utils/issues/350)) + ## Under the hood diff --git a/integration_tests/data/cross_db/data_last_day.csv b/integration_tests/data/cross_db/data_last_day.csv index 186465d86..307fbeb60 100644 --- a/integration_tests/data/cross_db/data_last_day.csv +++ b/integration_tests/data/cross_db/data_last_day.csv @@ -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, diff --git a/integration_tests/models/cross_db_utils/test_last_day.sql b/integration_tests/models/cross_db_utils/test_last_day.sql index 5391ae10c..fee3a823a 100644 --- a/integration_tests/models/cross_db_utils/test_last_day.sql +++ b/integration_tests/models/cross_db_utils/test_last_day.sql @@ -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, diff --git a/macros/cross_db_utils/last_day.sql b/macros/cross_db_utils/last_day.sql index b66e9d30f..160ec2e12 100644 --- a/macros/cross_db_utils/last_day.sql +++ b/macros/cross_db_utils/last_day.sql @@ -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 -%}