Skip to content

Commit

Permalink
Fix dayofmonth Spark function (facebookincubator#7115)
Browse files Browse the repository at this point in the history
Summary:
Returns the month of the year for the input DATE as an INTEGER between 1 and 12.

Before this change, the return type was BIGINT (fixed to INTEGER) and input could
be DATE or TIMESTAMP (only DATE is allowed now.)

[Spark's implementation](https://github.com/apache/spark/blob/b0791b513da3f0671417b9fbcd3a0caddbb45318/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala#L853)  and  [unit test](https://github.com/apache/spark/blob/b0791b513da3f0671417b9fbcd3a0caddbb45318/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DateExpressionsSuite.scala#L196).

Pull Request resolved: facebookincubator#7115

Reviewed By: amitkdutta

Differential Revision: D51200420

Pulled By: mbasmanova

fbshipit-source-id: 7418fd55f33de2b9238ed42f4f169e90f40a51e9
  • Loading branch information
JkSelf authored and facebook-github-bot committed Nov 10, 2023
1 parent ae0c450 commit 3f30748
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
12 changes: 6 additions & 6 deletions velox/docs/functions/spark/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ These functions support TIMESTAMP and DATE input types.

num_days can be positive or negative.

.. spark:function:: dayofyear(date) -> integer
.. spark:function:: dayofmonth(date) -> integer
Returns Returns the day of year of the date/timestamp. ::
Returns the day of month of the date. ::

SELECT dayofyear('2016-04-09'); -- 100
SELECT dayofmonth('2009-07-30'); -- 30

.. spark:function:: dayofmonth(date) -> integer
.. spark:function:: dayofyear(date) -> integer
Returns the day of month of the date/timestamp. ::
Returns the day of year of the date/timestamp. ::

SELECT dayofmonth('2009-07-30'); -- 30
SELECT dayofyear('2016-04-09'); -- 100

.. spark:function:: dayofweek(date/timestamp) -> integer
Expand Down
8 changes: 8 additions & 0 deletions velox/functions/sparksql/DateTimeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,12 @@ struct QuarterFunction {
}
};

template <typename T>
struct DayFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void call(int32_t& result, const arg_type<Date>& date) {
result = getDateTime(date).tm_mday;
}
};
} // namespace facebook::velox::functions::sparksql
4 changes: 1 addition & 3 deletions velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ 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>(
{prefix + "day", prefix + "dayofmonth"});
registerFunction<DayFunction, int64_t, Date>(
registerFunction<DayFunction, int32_t, Date>(
{prefix + "day", prefix + "dayofmonth"});
registerFunction<DayOfYearFunction, int64_t, Timestamp>(
{prefix + "doy", prefix + "dayofyear"});
Expand Down
2 changes: 1 addition & 1 deletion velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ TEST_F(DateTimeFunctionsTest, dayOfYear) {

TEST_F(DateTimeFunctionsTest, dayOfMonth) {
const auto day = [&](std::optional<int32_t> date) {
return evaluateOnce<int64_t, int32_t>("dayofmonth(c0)", {date}, {DATE()});
return evaluateOnce<int32_t, int32_t>("dayofmonth(c0)", {date}, {DATE()});
};
EXPECT_EQ(std::nullopt, day(std::nullopt));
EXPECT_EQ(30, day(parseDate("2009-07-30")));
Expand Down

0 comments on commit 3f30748

Please sign in to comment.