Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3074,8 +3074,26 @@ object functions {
* @group datetime_funcs
* @since 1.5.0
*/
def next_day(date: Column, dayOfWeek: String): Column = withExpr {
NextDay(date.expr, lit(dayOfWeek).expr)
def next_day(date: Column, dayOfWeek: String): Column = next_day(date, lit(dayOfWeek))

/**
* Returns the first date which is later than the value of the `date` column that is on the
* specified day of the week.
*
* For example, `next_day('2015-07-27', "Sunday")` returns 2015-08-02 because that is the first
* Sunday after 2015-07-27.
*
* @param date A date, timestamp or string. If a string, the data must be in a format that
* can be cast to a date, such as `yyyy-MM-dd` or `yyyy-MM-dd HH:mm:ss.SSSS`
* @param dayOfWeek A column of the day of week. Case insensitive, and accepts: "Mon", "Tue",
* "Wed", "Thu", "Fri", "Sat", "Sun"
* @return A date, or null if `date` was a string that could not be cast to a date or if
* `dayOfWeek` was an invalid value
* @group datetime_funcs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add @since 3.2.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @since 3.2.0
*/
def next_day(date: Column, dayOfWeek: Column): Column = withExpr {
NextDay(date.expr, dayOfWeek.expr)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,21 @@ class DateFunctionsSuite extends QueryTest with SharedSparkSession {
val df1 = Seq(("mon", "2015-07-23"), ("tuesday", "2015-07-20")).toDF("dow", "d")
val df2 = Seq(("th", "2015-07-23 00:11:22"), ("xx", "2015-07-24 11:22:33")).toDF("dow", "t")
checkAnswer(
df1.select(next_day(col("d"), "MONDAY")),
Seq(Row(Date.valueOf("2015-07-27")), Row(Date.valueOf("2015-07-27"))))
df1.select(
next_day(col("d"), "MONDAY"),
next_day(col("d"), col("dow")),
next_day(col("d"), "NonValidDay")),
Seq(
Row(Date.valueOf("2015-07-27"), Date.valueOf("2015-07-27"), null),
Row(Date.valueOf("2015-07-27"), Date.valueOf("2015-07-21"), null)))
checkAnswer(
df2.select(next_day(col("t"), "th")),
Seq(Row(Date.valueOf("2015-07-30")), Row(Date.valueOf("2015-07-30"))))
df2.select(
next_day(col("t"), "th"),
next_day(col("t"), col("dow")),
next_day(col("t"), "NonValidDay")),
Seq(
Row(Date.valueOf("2015-07-30"), Date.valueOf("2015-07-30"), null),
Row(Date.valueOf("2015-07-30"), null, null)))
}

def checkExceptionMessage(df: DataFrame): Unit = {
Expand Down