From d9b8d7363147133ea70fd047b21dbf52de02ccf7 Mon Sep 17 00:00:00 2001 From: Sumukh Date: Thu, 27 May 2021 23:20:57 +0530 Subject: [PATCH 1/2] cast day_of values to integer --- macros/calendar_date/day_of_month.sql | 3 +-- macros/calendar_date/day_of_week.sql | 10 +++++----- macros/calendar_date/day_of_year.sql | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/macros/calendar_date/day_of_month.sql b/macros/calendar_date/day_of_month.sql index 4a22c97..a48f6a3 100644 --- a/macros/calendar_date/day_of_month.sql +++ b/macros/calendar_date/day_of_month.sql @@ -1,4 +1,3 @@ {%- macro day_of_month(date) -%} -{{ dbt_date.date_part('day', date) }} +cast({{ dbt_date.date_part('day', date) }} as {{ dbt_utils.type_int() }}) {%- endmacro %} - diff --git a/macros/calendar_date/day_of_week.sql b/macros/calendar_date/day_of_week.sql index fcc4135..de02aed 100644 --- a/macros/calendar_date/day_of_week.sql +++ b/macros/calendar_date/day_of_week.sql @@ -10,10 +10,10 @@ case -- Shift start of week from Sunday (0) to Monday (1) when {{ dow }} = 0 then 7 - else {{ dow }} + else cast({{ dow }} as {{ dbt_utils.type_int() }}) end {%- else -%} - {{ dow }} + 1 + cast({{ dow }} + 1 as {{ dbt_utils.type_int() }}) {%- endif -%} {%- endmacro %} @@ -25,14 +25,14 @@ {{ dbt_date.date_part(dow_part, date) }} {%- else -%} {%- set dow_part = 'dayofweek' -%} - case + case when {{ dbt_date.date_part(dow_part, date) }} = 7 then 1 else {{ dbt_date.date_part(dow_part, date) }} + 1 end {%- endif -%} - - + + {%- endmacro %} {%- macro bigquery__day_of_week(date, isoweek) -%} diff --git a/macros/calendar_date/day_of_year.sql b/macros/calendar_date/day_of_year.sql index e68617d..29147f3 100644 --- a/macros/calendar_date/day_of_year.sql +++ b/macros/calendar_date/day_of_year.sql @@ -3,9 +3,9 @@ {%- endmacro %} {%- macro default__day_of_year(date) -%} - {{ dbt_date.date_part('dayofyear', date) }} + cast({{ dbt_date.date_part('dayofyear', date) }} as {{ dbt_utils.type_int() }}) {%- endmacro %} {%- macro postgres__day_of_year(date) -%} - {{ dbt_date.date_part('doy', date) }} + cast({{ dbt_date.date_part('doy', date) }} as {{ dbt_utils.type_int() }}) {%- endmacro %} From f52159abfea5769a1e74218800d9973ed7e48151 Mon Sep 17 00:00:00 2001 From: Sumukh Ghodke Date: Mon, 31 May 2021 00:03:05 +0530 Subject: [PATCH 2/2] add cast to bigint for redshift (#2) --- macros/calendar_date/day_of_month.sql | 6 +++++- macros/calendar_date/day_of_week.sql | 21 +++++++++++++++++++-- macros/calendar_date/day_of_year.sql | 8 ++++++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/macros/calendar_date/day_of_month.sql b/macros/calendar_date/day_of_month.sql index a48f6a3..d9157ad 100644 --- a/macros/calendar_date/day_of_month.sql +++ b/macros/calendar_date/day_of_month.sql @@ -1,3 +1,7 @@ {%- macro day_of_month(date) -%} -cast({{ dbt_date.date_part('day', date) }} as {{ dbt_utils.type_int() }}) +{{ dbt_date.date_part('day', date) }} +{%- endmacro %} + +{%- macro redshift__day_of_month(date) -%} +cast({{ dbt_date.date_part('day', date) }} as {{ dbt_utils.type_bigint() }}) {%- endmacro %} diff --git a/macros/calendar_date/day_of_week.sql b/macros/calendar_date/day_of_week.sql index de02aed..c467d29 100644 --- a/macros/calendar_date/day_of_week.sql +++ b/macros/calendar_date/day_of_week.sql @@ -10,10 +10,10 @@ case -- Shift start of week from Sunday (0) to Monday (1) when {{ dow }} = 0 then 7 - else cast({{ dow }} as {{ dbt_utils.type_int() }}) + else {{ dow }} end {%- else -%} - cast({{ dow }} + 1 as {{ dbt_utils.type_int() }}) + {{ dow }} + 1 {%- endif -%} {%- endmacro %} @@ -65,3 +65,20 @@ {%- endif -%} {%- endmacro %} + + +{%- macro redshift__day_of_week(date, isoweek) -%} + + {%- set dow = dbt_date.date_part('dayofweek', date) -%} + + {%- if isoweek -%} + case + -- Shift start of week from Sunday (0) to Monday (1) + when {{ dow }} = 0 then 7 + else cast({{ dow }} as {{ dbt_utils.type_bigint() }}) + end + {%- else -%} + cast({{ dow }} + 1 as {{ dbt_utils.type_bigint() }}) + {%- endif -%} + +{%- endmacro %} diff --git a/macros/calendar_date/day_of_year.sql b/macros/calendar_date/day_of_year.sql index 29147f3..577807c 100644 --- a/macros/calendar_date/day_of_year.sql +++ b/macros/calendar_date/day_of_year.sql @@ -3,9 +3,13 @@ {%- endmacro %} {%- macro default__day_of_year(date) -%} - cast({{ dbt_date.date_part('dayofyear', date) }} as {{ dbt_utils.type_int() }}) + {{ dbt_date.date_part('dayofyear', date) }} {%- endmacro %} {%- macro postgres__day_of_year(date) -%} - cast({{ dbt_date.date_part('doy', date) }} as {{ dbt_utils.type_int() }}) + {{ dbt_date.date_part('doy', date) }} +{%- endmacro %} + +{%- macro redshift__day_of_year(date) -%} + cast({{ dbt_date.date_part('dayofyear', date) }} as {{ dbt_utils.type_bigint() }}) {%- endmacro %}