MaxGekk commented on code in PR #48773:
URL: https://github.com/apache/spark/pull/48773#discussion_r1831275650


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala:
##########
@@ -478,17 +478,26 @@ case class MakeYMInterval(years: Expression, months: 
Expression)
   override def dataType: DataType = YearMonthIntervalType()
 
   override def nullSafeEval(year: Any, month: Any): Any = {
-    Math.toIntExact(Math.addExact(month.asInstanceOf[Number].longValue(),
-      Math.multiplyExact(year.asInstanceOf[Number].longValue(), 
MONTHS_PER_YEAR)))
+    try {
+      Math.toIntExact(Math.addExact(month.asInstanceOf[Number].longValue(),
+        Math.multiplyExact(year.asInstanceOf[Number].longValue(), 
MONTHS_PER_YEAR)))
+    } catch {
+      case e: ArithmeticException =>
+        throw QueryExecutionErrors.arithmeticOverflowError(e.getMessage)
+    }
   }
 
   override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    defineCodeGen(ctx, ev, (years, months) => {
+    nullSafeCodeGen(ctx, ev, (years, months) => {
       val math = classOf[Math].getName.stripSuffix("$")
       s"""
-         |$math.toIntExact(java.lang.Math.addExact($months,
-         |  $math.multiplyExact($years, $MONTHS_PER_YEAR)))
-         |""".stripMargin
+         |try {
+         |  ${ev.value} = $math.toIntExact(
+         |    $math.addExact($months, $math.multiplyExact($years, 
$MONTHS_PER_YEAR)));
+         |} catch (java.lang.ArithmeticException e) {
+         |  throw QueryExecutionErrors.arithmeticOverflowError(e.getMessage(), 
"", null);

Review Comment:
   as you are here, could you provide a query context of the error, see for 
example:
   
https://github.com/apache/spark/blob/0cb7a42731076b9960eb9ad27067cab7ae570356/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala#L393-L397



-- 
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.

To unsubscribe, e-mail: [email protected]

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