chongguang commented on a change in pull request #30807:
URL: https://github.com/apache/spark/pull/30807#discussion_r549666327
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
##########
@@ -1195,27 +1208,46 @@ case class NextDay(startDate: Expression, dayOfWeek:
Expression)
nullSafeCodeGen(ctx, ev, (sd, dowS) => {
val dateTimeUtilClass = DateTimeUtils.getClass.getName.stripSuffix("$")
val dayOfWeekTerm = ctx.freshName("dayOfWeek")
+ val failOnErrorTerm = ctx.freshName("failOnError")
if (dayOfWeek.foldable) {
val input = dayOfWeek.eval().asInstanceOf[UTF8String]
- if ((input eq null) || DateTimeUtils.getDayOfWeekFromString(input) ==
-1) {
+ if (input eq null) {
+ s"""${ev.isNull} = true;"""
+ } else if (DateTimeUtils.getDayOfWeekFromString(input) == -1) {
+ val errMsg = input.replace(
+ UTF8String.fromString("""""""), UTF8String.fromString("""\""""))
s"""
- |${ev.isNull} = true;
- """.stripMargin
+ |boolean $failOnErrorTerm = $failOnError;
+ |if ($failOnErrorTerm) {
+ | throw new IllegalArgumentException("Illegal input for
dayOfWeek: $errMsg");
+ |} else {
+ | ${ev.isNull} = true;
+ |}
+ |""".stripMargin
} else {
val dayOfWeekValue = DateTimeUtils.getDayOfWeekFromString(input)
- s"""
- |${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd,
$dayOfWeekValue);
- """.stripMargin
+ s"${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd,
$dayOfWeekValue);"
}
} else {
+ val errMsg = dowS.replace(""""""", """\"""")
+ val illegalInputBranch =
+ s"""
+ |boolean $failOnErrorTerm = $failOnError;
+ |if ($failOnErrorTerm) {
Review comment:
Hi @cloud-fan I did the same thing here but it can be done like this:
```
val illegalInputBranch = if (failOnError) {
val errMsg = dowS.replace(""""""", """\"""")
s"""throw new IllegalArgumentException("Illegal input for
dayOfWeek: $errMsg");"""
} else {
s"""${ev.isNull} = true;"""
}
s"""
|int $dayOfWeekTerm =
$dateTimeUtilClass.getDayOfWeekFromString($dowS);
|if ($dayOfWeekTerm == -1) {
| $illegalInputBranch
|} else {
| ${ev.value} = $dateTimeUtilClass.getNextDateForDayOfWeek($sd,
$dayOfWeekTerm);
|}
|""".stripMargin
```
It is ok here because the exception is already in an if-else block.
Do you want me to make the change?
----------------------------------------------------------------
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]