Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.spark.sql.errors.QueryExecutionErrors;
import org.apache.spark.sql.types.Decimal;
import org.apache.spark.unsafe.types.CalendarInterval;
import org.apache.spark.unsafe.types.UTF8String;

/**
* Static helpers shared by date/time/interval expression {@code doGenCode}
Expand Down Expand Up @@ -68,4 +69,17 @@ public static CalendarInterval makeIntervalExact(
throw QueryExecutionErrors.arithmeticOverflowError(e.getMessage(), "", null);
}
}

/**
* Computes the first date after {@code startDate} that falls on the weekday
* named by {@code dayOfWeek} for {@code NextDay} (next_day) in ANSI mode
* ({@code failOnError = true}). The {@code SparkIllegalArgumentException}
* thrown by {@code DateTimeUtils.getDayOfWeekFromString} for an invalid
* day-of-week string propagates to the caller unchanged (in ANSI mode the
* caller wants exactly that exception), so no try/catch wrapper is emitted.
*/
public static int getNextDateExact(int startDate, UTF8String dayOfWeek) {
int dow = DateTimeUtils.getDayOfWeekFromString(dayOfWeek);
return DateTimeUtils.getNextDateForDayOfWeek(startDate, dow);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1588,17 +1588,16 @@ case class NextDay(
override def nullable: Boolean = true

override def nullSafeEval(start: Any, dayOfW: Any): Any = {
try {
val dow = DateTimeUtils.getDayOfWeekFromString(dayOfW.asInstanceOf[UTF8String])
val sd = start.asInstanceOf[Int]
DateTimeUtils.getNextDateForDayOfWeek(sd, dow)
} catch {
case e: SparkIllegalArgumentException =>
if (failOnError) {
throw e
} else {
null
}
if (failOnError) {
DateTimeExpressionUtils.getNextDateExact(
start.asInstanceOf[Int], dayOfW.asInstanceOf[UTF8String])
} else {
try {
val dow = DateTimeUtils.getDayOfWeekFromString(dayOfW.asInstanceOf[UTF8String])
DateTimeUtils.getNextDateForDayOfWeek(start.asInstanceOf[Int], dow)
} catch {
case _: SparkIllegalArgumentException => null
}
}
}

Expand All @@ -1609,19 +1608,22 @@ case class NextDay(
dayOfWeekTerm: String,
sd: String,
dowS: String): String = {
val failOnErrorBranch = if (failOnError) {
"throw e;"
if (failOnError) {
// In ANSI mode the only exception (SparkIllegalArgumentException for an
// invalid day-of-week) must propagate, so the previous catch merely
// rethrew it. Delegate to the helper and drop the no-op try/catch.
val utils = classOf[DateTimeExpressionUtils].getName
s"${ev.value} = $utils.getNextDateExact($sd, $dowS);"
} else {
s"${ev.isNull} = true;"
s"""
|try {
| int $dayOfWeekTerm = $dateTimeUtilClass.getDayOfWeekFromString($dowS);
| ${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd, $dayOfWeekTerm);
|} catch (org.apache.spark.SparkIllegalArgumentException e) {
| ${ev.isNull} = true;
|}
|""".stripMargin
}
s"""
|try {
| int $dayOfWeekTerm = $dateTimeUtilClass.getDayOfWeekFromString($dowS);
| ${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd, $dayOfWeekTerm);
|} catch (org.apache.spark.SparkIllegalArgumentException e) {
| $failOnErrorBranch
|}
|""".stripMargin
}

override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
Expand Down