Skip to content

Commit 01a7925

Browse files
committed
Fix date time functions
1 parent 80deccd commit 01a7925

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

velox/functions/sparksql/DateTimeFunctions.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,110 @@ struct MakeDateFunction {
228228
}
229229
};
230230

231+
template <typename T>
232+
struct QuarterFunction : public InitSessionTimezone<T>,
233+
public TimestampWithTimezoneSupport<T> {
234+
VELOX_DEFINE_FUNCTION_TYPES(T);
235+
236+
FOLLY_ALWAYS_INLINE int32_t getQuarter(const std::tm& time) {
237+
return time.tm_mon / 3 + 1;
238+
}
239+
240+
FOLLY_ALWAYS_INLINE void call(
241+
int32_t& result,
242+
const arg_type<Timestamp>& timestamp) {
243+
result = getQuarter(getDateTime(timestamp, this->timeZone_));
244+
}
245+
246+
FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
247+
result = getQuarter(getDateTime(date));
248+
}
249+
250+
FOLLY_ALWAYS_INLINE void call(
251+
int32_t& result,
252+
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
253+
auto timestamp = this->toTimestamp(timestampWithTimezone);
254+
result = getQuarter(getDateTime(timestamp, nullptr));
255+
}
256+
};
257+
258+
template <typename T>
259+
struct MonthFunction : public InitSessionTimezone<T>,
260+
public TimestampWithTimezoneSupport<T> {
261+
VELOX_DEFINE_FUNCTION_TYPES(T);
262+
263+
FOLLY_ALWAYS_INLINE int32_t getMonth(const std::tm& time) {
264+
return 1 + time.tm_mon;
265+
}
266+
267+
FOLLY_ALWAYS_INLINE void call(
268+
int32_t& result,
269+
const arg_type<Timestamp>& timestamp) {
270+
result = getMonth(getDateTime(timestamp, this->timeZone_));
271+
}
272+
273+
FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
274+
result = getMonth(getDateTime(date));
275+
}
276+
277+
FOLLY_ALWAYS_INLINE void call(
278+
int32_t& result,
279+
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
280+
auto timestamp = this->toTimestamp(timestampWithTimezone);
281+
result = getMonth(getDateTime(timestamp, nullptr));
282+
}
283+
};
284+
285+
template <typename T>
286+
struct DayFunction : public InitSessionTimezone<T>,
287+
public TimestampWithTimezoneSupport<T> {
288+
VELOX_DEFINE_FUNCTION_TYPES(T);
289+
290+
FOLLY_ALWAYS_INLINE void call(
291+
int32_t& result,
292+
const arg_type<Timestamp>& timestamp) {
293+
result = getDateTime(timestamp, this->timeZone_).tm_mday;
294+
}
295+
296+
FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
297+
result = getDateTime(date).tm_mday;
298+
}
299+
300+
FOLLY_ALWAYS_INLINE void call(
301+
int32_t& result,
302+
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
303+
auto timestamp = this->toTimestamp(timestampWithTimezone);
304+
result = getDateTime(timestamp, nullptr).tm_mday;
305+
}
306+
};
307+
308+
template <typename T>
309+
struct DayOfYearFunction : public InitSessionTimezone<T>,
310+
public TimestampWithTimezoneSupport<T> {
311+
VELOX_DEFINE_FUNCTION_TYPES(T);
312+
313+
FOLLY_ALWAYS_INLINE int32_t getDayOfYear(const std::tm& time) {
314+
return time.tm_yday + 1;
315+
}
316+
317+
FOLLY_ALWAYS_INLINE void call(
318+
int32_t& result,
319+
const arg_type<Timestamp>& timestamp) {
320+
result = getDayOfYear(getDateTime(timestamp, this->timeZone_));
321+
}
322+
323+
FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
324+
result = getDayOfYear(getDateTime(date));
325+
}
326+
327+
FOLLY_ALWAYS_INLINE void call(
328+
int32_t& result,
329+
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
330+
auto timestamp = this->toTimestamp(timestampWithTimezone);
331+
result = getDayOfYear(getDateTime(timestamp, nullptr));
332+
}
333+
};
334+
231335
template <typename T>
232336
struct LastDayFunction {
233337
VELOX_DEFINE_FUNCTION_TYPES(T);

velox/functions/sparksql/Register.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,18 @@ void registerFunctions(const std::string& prefix) {
270270
registerFunction<DateAddFunction, Date, Date, int32_t>({prefix + "date_add"});
271271
registerFunction<DateSubFunction, Date, Date, int32_t>({prefix + "date_sub"});
272272

273-
registerFunction<DayFunction, int64_t, Timestamp>(
273+
registerFunction<QuarterFunction, int32_t, Timestamp>({prefix + "quarter"});
274+
registerFunction<QuarterFunction, int32_t, Date>({prefix + "quarter"});
275+
registerFunction<MonthFunction, int32_t, Timestamp>({prefix + "month"});
276+
registerFunction<MonthFunction, int32_t, Date>({prefix + "month"});
277+
registerFunction<DayFunction, int32_t, Timestamp>(
274278
{prefix + "day", prefix + "dayofmonth"});
275-
registerFunction<DayFunction, int64_t, Date>(
279+
registerFunction<DayFunction, int32_t, Date>(
276280
{prefix + "day", prefix + "dayofmonth"});
277-
registerFunction<DayOfYearFunction, int64_t, Timestamp>(
281+
registerFunction<DayOfYearFunction, int32_t, Timestamp>(
278282
{prefix + "doy", prefix + "dayofyear"});
279-
registerFunction<DayOfYearFunction, int64_t, Date>(
283+
registerFunction<DayOfYearFunction, int32_t, Date>(
280284
{prefix + "doy", prefix + "dayofyear"});
281-
282285
registerFunction<DayOfWeekFunction, int32_t, Timestamp>(
283286
{prefix + "dow", prefix + "dayofweek"});
284287
registerFunction<DayOfWeekFunction, int32_t, Date>(

0 commit comments

Comments
 (0)