cloud-fan commented on code in PR #55934:
URL: https://github.com/apache/spark/pull/55934#discussion_r3288984479


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1988,16 +1988,24 @@ case class Cast(
       from: DataType,
       to: DataType): CastFunction = {
     assert(ansiEnabled)
-    val fromDt = ctx.addReferenceObj("from", from, from.getClass.getName)
-    val toDt = ctx.addReferenceObj("to", to, to.getClass.getName)
-    (c, evPrim, _) =>
-      code"""
-        if ($c == ($integralType) $c) {
-          $evPrim = ($integralType) $c;
-        } else {
-          throw QueryExecutionErrors.castingCauseOverflowError($c, $fromDt, 
$toDt);
-        }
-      """
+    if (integralType == "int") {
+      // Long -> Int: call LongExactNumeric.toInt directly. It already does the
+      // bounds check and throws castingCauseOverflowError -- same as the 
inline body.
+      val numericObj = 
LongExactNumeric.getClass.getCanonicalName.stripSuffix("$")
+      (c, evPrim, _) => code"$evPrim = $numericObj.toInt($c);"
+    } else {
+      // Byte/short narrowing remains inline; refactored in a follow-up PR.

Review Comment:
   Nit: the symmetric `// Byte/short narrowing remains inline; refactored in a 
follow-up PR.` comment in `castFractionToIntegralTypeCode` was removed in your 
last commit (`ff9b4cb3`, "Apply suggestion from @gengliangwang") — looks like 
this one was missed. The else-branch's role is already clear from the `if 
(integralType == "int")` guard above.
   
   ```suggestion
       } else {
   ```



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