Skip to content

Commit

Permalink
Fix date time functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-mo committed Oct 18, 2023
1 parent 80deccd commit 01a7925
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
104 changes: 104 additions & 0 deletions velox/functions/sparksql/DateTimeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,110 @@ struct MakeDateFunction {
}
};

template <typename T>
struct QuarterFunction : public InitSessionTimezone<T>,
public TimestampWithTimezoneSupport<T> {
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>& timestamp) {
result = getQuarter(getDateTime(timestamp, this->timeZone_));
}

FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
result = getQuarter(getDateTime(date));
}

FOLLY_ALWAYS_INLINE void call(
int32_t& result,
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
auto timestamp = this->toTimestamp(timestampWithTimezone);
result = getQuarter(getDateTime(timestamp, nullptr));
}
};

template <typename T>
struct MonthFunction : public InitSessionTimezone<T>,
public TimestampWithTimezoneSupport<T> {
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>& timestamp) {
result = getMonth(getDateTime(timestamp, this->timeZone_));
}

FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
result = getMonth(getDateTime(date));
}

FOLLY_ALWAYS_INLINE void call(
int32_t& result,
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
auto timestamp = this->toTimestamp(timestampWithTimezone);
result = getMonth(getDateTime(timestamp, nullptr));
}
};

template <typename T>
struct DayFunction : public InitSessionTimezone<T>,
public TimestampWithTimezoneSupport<T> {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void call(
int32_t& result,
const arg_type<Timestamp>& timestamp) {
result = getDateTime(timestamp, this->timeZone_).tm_mday;
}

FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
result = getDateTime(date).tm_mday;
}

FOLLY_ALWAYS_INLINE void call(
int32_t& result,
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
auto timestamp = this->toTimestamp(timestampWithTimezone);
result = getDateTime(timestamp, nullptr).tm_mday;
}
};

template <typename T>
struct DayOfYearFunction : public InitSessionTimezone<T>,
public TimestampWithTimezoneSupport<T> {
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>& timestamp) {
result = getDayOfYear(getDateTime(timestamp, this->timeZone_));
}

FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
result = getDayOfYear(getDateTime(date));
}

FOLLY_ALWAYS_INLINE void call(
int32_t& result,
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
auto timestamp = this->toTimestamp(timestampWithTimezone);
result = getDayOfYear(getDateTime(timestamp, nullptr));
}
};

template <typename T>
struct LastDayFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);
Expand Down
13 changes: 8 additions & 5 deletions velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,18 @@ void registerFunctions(const std::string& prefix) {
registerFunction<DateAddFunction, Date, Date, int32_t>({prefix + "date_add"});
registerFunction<DateSubFunction, Date, Date, int32_t>({prefix + "date_sub"});

registerFunction<DayFunction, int64_t, Timestamp>(
registerFunction<QuarterFunction, int32_t, Timestamp>({prefix + "quarter"});
registerFunction<QuarterFunction, int32_t, Date>({prefix + "quarter"});
registerFunction<MonthFunction, int32_t, Timestamp>({prefix + "month"});
registerFunction<MonthFunction, int32_t, Date>({prefix + "month"});
registerFunction<DayFunction, int32_t, Timestamp>(
{prefix + "day", prefix + "dayofmonth"});
registerFunction<DayFunction, int64_t, Date>(
registerFunction<DayFunction, int32_t, Date>(
{prefix + "day", prefix + "dayofmonth"});
registerFunction<DayOfYearFunction, int64_t, Timestamp>(
registerFunction<DayOfYearFunction, int32_t, Timestamp>(
{prefix + "doy", prefix + "dayofyear"});
registerFunction<DayOfYearFunction, int64_t, Date>(
registerFunction<DayOfYearFunction, int32_t, Date>(
{prefix + "doy", prefix + "dayofyear"});

registerFunction<DayOfWeekFunction, int32_t, Timestamp>(
{prefix + "dow", prefix + "dayofweek"});
registerFunction<DayOfWeekFunction, int32_t, Date>(
Expand Down

0 comments on commit 01a7925

Please sign in to comment.