MCPcopy Create free account
hub / github.com/apache/impala / MonthsBetween

Method MonthsBetween

be/src/exprs/timestamp-functions-ir.cc:576–599  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

574}
575
576DoubleVal TimestampFunctions::MonthsBetween(FunctionContext* context,
577 const TimestampVal& ts1, const TimestampVal& ts2) {
578 if (ts1.is_null || ts2.is_null) return DoubleVal::null();
579 const TimestampValue& ts_value1 = TimestampValue::FromTimestampVal(ts1);
580 const TimestampValue& ts_value2 = TimestampValue::FromTimestampVal(ts2);
581 if (!ts_value1.HasDate() || !ts_value2.HasDate()) return DoubleVal::null();
582 IntVal year1 = Year(context, ts1);
583 IntVal year2 = Year(context, ts2);
584 IntVal month1 = Month(context, ts1);
585 IntVal month2 = Month(context, ts2);
586 IntVal day1 = DayOfMonth(context, ts1);
587 IntVal day2 = DayOfMonth(context, ts2);
588 int days_diff = 0;
589 // If both timestamps are last days of different months they don't contribute
590 // a fractional value to the number of months, therefore there is no need to
591 // calculate difference in their days.
592 if (!(day1.val == GetLastDayOfMonth(month1.val, year1.val) &&
593 day2.val == GetLastDayOfMonth(month2.val, year2.val))) {
594 days_diff = day1.val - day2.val;
595 }
596 double months_between = (year1.val - year2.val) * 12 +
597 month1.val - month2.val + (static_cast<double>(days_diff) / 31.0);
598 return DoubleVal(months_between);
599}
600
601TimestampVal TimestampFunctions::NextDay(FunctionContext* context,
602 const TimestampVal& date, const StringVal& weekday) {

Callers

nothing calls this directly

Calls 3

GetLastDayOfMonthFunction · 0.85
DoubleValClass · 0.85
HasDateMethod · 0.80

Tested by

no test coverage detected