I am attempting to write a user defined function that will allow you to input a date(current_date) and a range and the query will return the following dates along with how many dates have passed. As example if given current_date, query will return current_date - 0 day, next_day - 1 day. I have been trying different date types(unixtimestamp, date, jodatime, localdatetime). I scrapped my previous code and am really just hoping for some insight on the problem and less code.
You can do it using pure Hive query:
set hivevar:start_date=2019-01-02;
set hivevar:end_date=2019-01-31;
with date_range as
(--this query generates date range
select date_add ('${hivevar:start_date}',s.i) as dt, s.i days
from ( select posexplode(split(space(datediff('${hivevar:end_date}','${hivevar:start_date}')),' ')) as (i,x) ) s
)
select dt, days from date_range;
Returns:
dt days
2019-01-02 0
2019-01-03 1
2019-01-04 2
2019-01-05 3
2019-01-06 4
2019-01-07 5
2019-01-08 6
2019-01-09 7
2019-01-10 8
2019-01-11 9
2019-01-12 10
2019-01-13 11
2019-01-14 12
2019-01-15 13
2019-01-16 14
2019-01-17 15
2019-01-18 16
2019-01-19 17
2019-01-20 18
2019-01-21 19
2019-01-22 20
2019-01-23 21
2019-01-24 22
2019-01-25 23
2019-01-26 24
2019-01-27 25
2019-01-28 26
2019-01-29 27
2019-01-30 28
2019-01-31 29
Hope you got the idea.