chongguang commented on a change in pull request #30807:
URL: https://github.com/apache/spark/pull/30807#discussion_r550075012



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
##########
@@ -1170,52 +1175,69 @@ case class LastDay(startDate: Expression)
   group = "datetime_funcs",
   since = "1.5.0")
 // scalastyle:on line.size.limit
-case class NextDay(startDate: Expression, dayOfWeek: Expression)
+case class NextDay(
+    startDate: Expression,
+    dayOfWeek: Expression,
+    failOnError: Boolean = SQLConf.get.ansiEnabled)
   extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
 
   override def left: Expression = startDate
   override def right: Expression = dayOfWeek
 
+  def this(left: Expression, right: Expression) = this(left, right, 
SQLConf.get.ansiEnabled)
+
   override def inputTypes: Seq[AbstractDataType] = Seq(DateType, StringType)
 
   override def dataType: DataType = DateType
   override def nullable: Boolean = true
 
   override def nullSafeEval(start: Any, dayOfW: Any): Any = {
-    val dow = 
DateTimeUtils.getDayOfWeekFromString(dayOfW.asInstanceOf[UTF8String])
-    if (dow == -1) {
-      null
-    } else {
+    try {
+      val dow = 
DateTimeUtils.getDayOfWeekFromString(dayOfW.asInstanceOf[UTF8String])
       val sd = start.asInstanceOf[Int]
       DateTimeUtils.getNextDateForDayOfWeek(sd, dow)
+    } catch {
+      case _: IllegalArgumentException if !failOnError => null
+    }
+  }
+
+  private def nextDayGenCode(
+    ev: ExprCode,
+    dayOfWeekTerm: String,
+    sd: String,
+    dowS: String): String = {
+    val dateTimeUtilClass = DateTimeUtils.getClass.getName.stripSuffix("$")
+
+    if (failOnError) {
+      s"""
+       |int $dayOfWeekTerm = $dateTimeUtilClass.getDayOfWeekFromString($dowS);
+       |${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd, 
$dayOfWeekTerm);
+       |""".stripMargin
+    } else {
+      s"""
+       |int $dayOfWeekTerm;
+       |try {
+       |  $dayOfWeekTerm = $dateTimeUtilClass.getDayOfWeekFromString($dowS);
+       |  ${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd, 
$dayOfWeekTerm);
+       |} catch (IllegalArgumentException e) {
+       |  ${ev.isNull} = true;
+       |}
+       |""".stripMargin
     }
   }
 
   override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): 
ExprCode = {
     nullSafeCodeGen(ctx, ev, (sd, dowS) => {
-      val dateTimeUtilClass = DateTimeUtils.getClass.getName.stripSuffix("$")
       val dayOfWeekTerm = ctx.freshName("dayOfWeek")
       if (dayOfWeek.foldable) {
         val input = dayOfWeek.eval().asInstanceOf[UTF8String]
-        if ((input eq null) || DateTimeUtils.getDayOfWeekFromString(input) == 
-1) {
-          s"""
-             |${ev.isNull} = true;
-           """.stripMargin
+        if (input eq null) {
+          s"""${ev.isNull} = true;"""
         } else {
-          val dayOfWeekValue = DateTimeUtils.getDayOfWeekFromString(input)
-          s"""
-             |${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd, 
$dayOfWeekValue);
-           """.stripMargin
+          nextDayGenCode(ev, dayOfWeekTerm, sd, dowS)

Review comment:
       Thx for pointing out. 
   How about the following which avoids re-evaluating `getDayOfWeekFromString` 
in case of exception?
   ```
             try {
               val dayOfWeekValue = DateTimeUtils.getDayOfWeekFromString(input)
               val dateTimeUtilClass = 
DateTimeUtils.getClass.getName.stripSuffix("$")
               s"${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd, 
$dayOfWeekValue);"
             } catch {
               case e: IllegalArgumentException => if (failOnError) {
                 val errMsg = e.getMessage.replace("\"", """\"""")
                 s"""
                    |if (true) {
                    |  throw new IllegalArgumentException("$errMsg");
                    |}
                    |""".stripMargin
               } else {
                 s"""${ev.isNull} = true;"""
               }
             }
   ```
   
   The `if (true)` here seems a little strange but I just saw it in other parts 
of code. So I'm thinking it's ok?
   For exemple here: 
https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala#L91




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to