From 01a792543d97705458d0d7c18235c2429c9f7660 Mon Sep 17 00:00:00 2001 From: rui-mo Date: Wed, 13 Sep 2023 13:27:52 +0800 Subject: [PATCH] Fix date time functions --- velox/functions/sparksql/DateTimeFunctions.h | 104 +++++++++++++++++++ velox/functions/sparksql/Register.cpp | 13 ++- 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/velox/functions/sparksql/DateTimeFunctions.h b/velox/functions/sparksql/DateTimeFunctions.h index 3bd40279c0c9c..fa61ef30b7223 100644 --- a/velox/functions/sparksql/DateTimeFunctions.h +++ b/velox/functions/sparksql/DateTimeFunctions.h @@ -228,6 +228,110 @@ struct MakeDateFunction { } }; +template +struct QuarterFunction : public InitSessionTimezone, + public TimestampWithTimezoneSupport { + VELOX_DEFINE_FUNCTION_TYPES(T); + + FOLLY_ALWAYS_INLINE int32_t getQuarter(const std::tm& time) { + return time.tm_mon / 3 + 1; + } + + FOLLY_ALWAYS_INLINE void call( + int32_t& result, + const arg_type& timestamp) { + result = getQuarter(getDateTime(timestamp, this->timeZone_)); + } + + FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type& date) { + result = getQuarter(getDateTime(date)); + } + + FOLLY_ALWAYS_INLINE void call( + int32_t& result, + const arg_type& timestampWithTimezone) { + auto timestamp = this->toTimestamp(timestampWithTimezone); + result = getQuarter(getDateTime(timestamp, nullptr)); + } +}; + +template +struct MonthFunction : public InitSessionTimezone, + public TimestampWithTimezoneSupport { + VELOX_DEFINE_FUNCTION_TYPES(T); + + FOLLY_ALWAYS_INLINE int32_t getMonth(const std::tm& time) { + return 1 + time.tm_mon; + } + + FOLLY_ALWAYS_INLINE void call( + int32_t& result, + const arg_type& timestamp) { + result = getMonth(getDateTime(timestamp, this->timeZone_)); + } + + FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type& date) { + result = getMonth(getDateTime(date)); + } + + FOLLY_ALWAYS_INLINE void call( + int32_t& result, + const arg_type& timestampWithTimezone) { + auto timestamp = this->toTimestamp(timestampWithTimezone); + result = getMonth(getDateTime(timestamp, nullptr)); + } +}; + +template +struct DayFunction : public InitSessionTimezone, + public TimestampWithTimezoneSupport { + VELOX_DEFINE_FUNCTION_TYPES(T); + + FOLLY_ALWAYS_INLINE void call( + int32_t& result, + const arg_type& timestamp) { + result = getDateTime(timestamp, this->timeZone_).tm_mday; + } + + FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type& date) { + result = getDateTime(date).tm_mday; + } + + FOLLY_ALWAYS_INLINE void call( + int32_t& result, + const arg_type& timestampWithTimezone) { + auto timestamp = this->toTimestamp(timestampWithTimezone); + result = getDateTime(timestamp, nullptr).tm_mday; + } +}; + +template +struct DayOfYearFunction : public InitSessionTimezone, + public TimestampWithTimezoneSupport { + VELOX_DEFINE_FUNCTION_TYPES(T); + + FOLLY_ALWAYS_INLINE int32_t getDayOfYear(const std::tm& time) { + return time.tm_yday + 1; + } + + FOLLY_ALWAYS_INLINE void call( + int32_t& result, + const arg_type& timestamp) { + result = getDayOfYear(getDateTime(timestamp, this->timeZone_)); + } + + FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type& date) { + result = getDayOfYear(getDateTime(date)); + } + + FOLLY_ALWAYS_INLINE void call( + int32_t& result, + const arg_type& timestampWithTimezone) { + auto timestamp = this->toTimestamp(timestampWithTimezone); + result = getDayOfYear(getDateTime(timestamp, nullptr)); + } +}; + template struct LastDayFunction { VELOX_DEFINE_FUNCTION_TYPES(T); diff --git a/velox/functions/sparksql/Register.cpp b/velox/functions/sparksql/Register.cpp index bb50cd669aace..591c6f61223e3 100644 --- a/velox/functions/sparksql/Register.cpp +++ b/velox/functions/sparksql/Register.cpp @@ -270,15 +270,18 @@ void registerFunctions(const std::string& prefix) { registerFunction({prefix + "date_add"}); registerFunction({prefix + "date_sub"}); - registerFunction( + registerFunction({prefix + "quarter"}); + registerFunction({prefix + "quarter"}); + registerFunction({prefix + "month"}); + registerFunction({prefix + "month"}); + registerFunction( {prefix + "day", prefix + "dayofmonth"}); - registerFunction( + registerFunction( {prefix + "day", prefix + "dayofmonth"}); - registerFunction( + registerFunction( {prefix + "doy", prefix + "dayofyear"}); - registerFunction( + registerFunction( {prefix + "doy", prefix + "dayofyear"}); - registerFunction( {prefix + "dow", prefix + "dayofweek"}); registerFunction(