Skip to content

Commit

Permalink
Add dayofyear function support in sparksql (7116)
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-mo authored and PHILO-HE committed Oct 29, 2023
1 parent 14af30f commit 6dab43a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
27 changes: 27 additions & 0 deletions velox/functions/sparksql/DateTimeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,31 @@ struct DayFunction : public InitSessionTimezone<T>,
}
};

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));
}
};

} // namespace facebook::velox::functions::sparksql
4 changes: 2 additions & 2 deletions velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ void registerFunctions(const std::string& prefix) {
{prefix + "day", prefix + "dayofmonth"});
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>(
Expand Down
25 changes: 24 additions & 1 deletion velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,37 @@ TEST_F(DateTimeFunctionsTest, dateSub) {

TEST_F(DateTimeFunctionsTest, dayOfYear) {
const auto day = [&](std::optional<int32_t> date) {
return evaluateOnce<int64_t, int32_t>("dayofyear(c0)", {date}, {DATE()});
return evaluateOnce<int32_t, int32_t>("dayofyear(c0)", {date}, {DATE()});
};
EXPECT_EQ(std::nullopt, day(std::nullopt));
EXPECT_EQ(100, day(parseDate("2016-04-09")));
EXPECT_EQ(235, day(parseDate("2023-08-23")));
EXPECT_EQ(1, day(parseDate("1970-01-01")));
}

TEST_F(DateTimeFunctionsTest, dayOfYearTimestamp) {
const auto day = [&](std::optional<Timestamp> date) {
return evaluateOnce<int32_t>("dayofyear(c0)", date);
};
EXPECT_EQ(std::nullopt, day(std::nullopt));
EXPECT_EQ(1, day(Timestamp(0, 0)));
EXPECT_EQ(365, day(Timestamp(-1, 9000)));
EXPECT_EQ(273, day(Timestamp(1632989700, 0)));
EXPECT_EQ(274, day(Timestamp(1633076100, 0)));
EXPECT_EQ(279, day(Timestamp(1633508100, 0)));
EXPECT_EQ(304, day(Timestamp(1635668100, 0)));

setQueryTimeZone("Pacific/Apia");

EXPECT_EQ(std::nullopt, day(std::nullopt));
EXPECT_EQ(365, day(Timestamp(0, 0)));
EXPECT_EQ(365, day(Timestamp(-1, 9000)));
EXPECT_EQ(273, day(Timestamp(1632989700, 0)));
EXPECT_EQ(274, day(Timestamp(1633076100, 0)));
EXPECT_EQ(279, day(Timestamp(1633508100, 0)));
EXPECT_EQ(304, day(Timestamp(1635668100, 0)));
}

TEST_F(DateTimeFunctionsTest, dayOfMonth) {
const auto day = [&](std::optional<int32_t> date) {
return evaluateOnce<int32_t, int32_t>("dayofmonth(c0)", {date}, {DATE()});
Expand Down

0 comments on commit 6dab43a

Please sign in to comment.