Github user yhuai commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10266#discussion_r47374514
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ScalaUDF.scala
 ---
    @@ -1029,24 +1029,27 @@ case class ScalaUDF(
         // such as IntegerType, its javaType is `int` and the returned type of 
user-defined
         // function is Object. Trying to convert an Object to `int` will cause 
casting exception.
         val evalCode = evals.map(_.code).mkString
    -    val funcArguments = converterTerms.zipWithIndex.map {
    -      case (converter, i) =>
    -        val eval = evals(i)
    -        val dt = children(i).dataType
    -        s"$converter.apply(${eval.isNull} ? null : (${ctx.boxedType(dt)}) 
${eval.value})"
    -    }.mkString(",")
    -    val callFunc = s"${ctx.boxedType(ctx.javaType(dataType))} $resultTerm 
= " +
    -      
s"(${ctx.boxedType(ctx.javaType(dataType))})${catalystConverterTerm}" +
    -        s".apply($funcTerm.apply($funcArguments));"
    +    val (converters, funcArguments) = converterTerms.zipWithIndex.map { 
case (converter, i) =>
    +      val eval = evals(i)
    +      val argTerm = ctx.freshName("arg")
    +      val convert = s"Object $argTerm = ${eval.isNull} ? null : 
$converter.apply(${eval.value});"
    +      (convert, argTerm)
    +    }.unzip
     
    -    evalCode + s"""
    -      ${ctx.javaType(dataType)} ${ev.value} = 
${ctx.defaultValue(dataType)};
    -      Boolean ${ev.isNull};
    +    val callFunc = s"${ctx.boxedType(dataType)} $resultTerm = " +
    +      s"(${ctx.boxedType(dataType)})${catalystConverterTerm}" +
    +        s".apply($funcTerm.apply(${funcArguments.mkString(", ")}));"
     
    +    s"""
    +      $evalCode
    +      ${converters.mkString("\n")}
           $callFunc
     
    -      ${ev.value} = $resultTerm;
    -      ${ev.isNull} = $resultTerm == null;
    +      boolean ${ev.isNull} = $resultTerm == null;
    +      ${ctx.javaType(dataType)} ${ev.value} = 
${ctx.defaultValue(dataType)};
    +      if (!${ev.isNull}) {
    +        ${ev.value} = $resultTerm;
    --- End diff --
    
    Seems we are fine because we check `if (!${ev.isNull})` first?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to