You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe:
Describe the feature you'd like: add_months/months_between:
We now have calculation requirements for month addition and subtraction and monthly difference, and the result needs to be consistent with Oracle/Hive. May I ask whether these two functions can be implemented. Example:
add_months('2019-2-28',1)=2019-3-31
add_months('2020-2-28',1)=2019-3-28
add_months('2020-2-29',1)=2019-3-31
add_months('2020-2-28',-1)=2019-1-28
add_months('2020-2-29',-1)=2019-1-31
add_months('2020-3-31',-1)=2020-2-29
add_months('2020-3-30',-1)=2020-2-29
add_months('2020-3-29',-1)=2020-2-29
add_months('2019-2-28',-1)=2019-1-31
add_months('2019-3-31',-1)=2019-2-28
The implementation in Hive is: months_between:
public Object evaluate(DeferredObject[] arguments) throws HiveException {
// the function should support both short date and full timestamp format
// time part of the timestamp should not be skipped
Date date1 = getTimestampValue(arguments, 0, tsConverters);
if (date1 == null) {
date1 = getDateValue(arguments, 0, dtInputTypes, dtConverters);
if (date1 == null) {
return null;
}
}
Date date2 = getTimestampValue(arguments, 1, tsConverters);
if (date2 == null) {
date2 = getDateValue(arguments, 1, dtInputTypes, dtConverters);
if (date2 == null) {
return null;
}
}
cal1.setTime(date1);
cal2.setTime(date2);
// skip day/time part if both dates are end of the month
// or the same day of the month
int monDiffInt = (cal1.get(YEAR) - cal2.get(YEAR)) * 12 + (cal1.get(MONTH) - cal2.get(MONTH));
if (cal1.get(DATE) == cal2.get(DATE)
|| (cal1.get(DATE) == cal1.getActualMaximum(DATE) && cal2.get(DATE) == cal2
.getActualMaximum(DATE))) {
output.set(monDiffInt);
return output;
}
int sec1 = getDayPartInSec(cal1);
int sec2 = getDayPartInSec(cal2);
// 1 sec is 0.000000373 months (1/2678400). 1 month is 31 days.
// there should be no adjustments for leap seconds
double monBtwDbl = monDiffInt + (sec1 - sec2) / 2678400D;
// Round a double to 8 decimal places.
double result = BigDecimal.valueOf(monBtwDbl).setScale(8, ROUND_HALF_UP).doubleValue();
output.set(result);
return output;
}
add_months:
protected Calendar addMonth(Date d, int numMonths) {
calendar.setTime(d);
boolean lastDatOfMonth = isLastDayOfMonth(calendar);
calendar.add(Calendar.MONTH, numMonths);
if (lastDatOfMonth) {
int maxDd = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, maxDd);
}
return calendar;
}
The text was updated successfully, but these errors were encountered:
Feature Request
Is your feature request related to a problem? Please describe:
Describe the feature you'd like:
add_months/months_between:
We now have calculation requirements for month addition and subtraction and monthly difference, and the result needs to be consistent with Oracle/Hive. May I ask whether these two functions can be implemented. Example:
add_months('2019-2-28',1)=2019-3-31
add_months('2020-2-28',1)=2019-3-28
add_months('2020-2-29',1)=2019-3-31
add_months('2020-2-28',-1)=2019-1-28
add_months('2020-2-29',-1)=2019-1-31
add_months('2020-3-31',-1)=2020-2-29
add_months('2020-3-30',-1)=2020-2-29
add_months('2020-3-29',-1)=2020-2-29
add_months('2019-2-28',-1)=2019-1-31
add_months('2019-3-31',-1)=2019-2-28
The implementation in Hive is:
months_between:
public Object evaluate(DeferredObject[] arguments) throws HiveException {
// the function should support both short date and full timestamp format
// time part of the timestamp should not be skipped
Date date1 = getTimestampValue(arguments, 0, tsConverters);
if (date1 == null) {
date1 = getDateValue(arguments, 0, dtInputTypes, dtConverters);
if (date1 == null) {
return null;
}
}
}
add_months:
protected Calendar addMonth(Date d, int numMonths) {
calendar.setTime(d);
}
The text was updated successfully, but these errors were encountered: