Skip to content

Commit

Permalink
Add tests and update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Apr 15, 2024
1 parent 484d3ff commit 7d17e0f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions velox/docs/functions/spark/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,10 @@ These functions support TIMESTAMP and DATE input types.
.. spark:function:: year(x) -> integer
Returns the year from ``x``.

.. spark:function:: year_of_week(x) -> integer
Returns the ISO week-numbering year that ``x`` falls in. For example, 2005-01-02 is
part of the 53rd week of year 2004, so the result is 2004. Only supports DATE type.

SELECT year_of_week('2005-01-02'); -- 2004
16 changes: 16 additions & 0 deletions velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,5 +968,21 @@ TEST_F(DateTimeFunctionsTest, makeYMInterval) {
fromYear(-178956971), "Integer overflow in make_ym_interval(-178956971)");
}

TEST_F(DateTimeFunctionsTest, yearOfWeek) {
const auto yearOfWeek = [&](std::optional<int32_t> date) {
return evaluateOnce<int64_t, int32_t>("year_of_week(c0)", {date}, {DATE()});
};
EXPECT_EQ(std::nullopt, yearOfWeek(std::nullopt));
EXPECT_EQ(1970, yearOfWeek(0));
EXPECT_EQ(1970, yearOfWeek(-1));
EXPECT_EQ(1969, yearOfWeek(-4));
EXPECT_EQ(1970, yearOfWeek(-3));
EXPECT_EQ(1970, yearOfWeek(365));
EXPECT_EQ(1970, yearOfWeek(367));
EXPECT_EQ(1971, yearOfWeek(368));
EXPECT_EQ(2005, yearOfWeek(parseDate("2006-01-01")));
EXPECT_EQ(2006, yearOfWeek(parseDate("2006-01-02")));
}

} // namespace
} // namespace facebook::velox::functions::sparksql::test

0 comments on commit 7d17e0f

Please sign in to comment.