You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/05/18 17:22:57 UTC

[GitHub] [spark] JoshRosen commented on a change in pull request #24636: [SPARK-27684][SQL] Avoid conversion overhead for primitive types

JoshRosen commented on a change in pull request #24636: [SPARK-27684][SQL] Avoid conversion overhead for primitive types
URL: https://github.com/apache/spark/pull/24636#discussion_r285347516
 
 

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ScalaUDF.scala
 ##########
 @@ -1003,22 +1003,38 @@ 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("\n")
-    val (funcArgs, initArgs) = evals.zipWithIndex.map { case (eval, i) =>
-      val argTerm = ctx.freshName("arg")
-      val convert = s"$convertersTerm[$i].apply(${eval.value})"
-      val initArg = s"Object $argTerm = ${eval.isNull} ? null : $convert;"
-      (argTerm, initArg)
+    val (funcArgs, initArgs) = evals.zipWithIndex.zip(children.map(_.dataType)).map {
+      case ((eval, i), dt) =>
+        val argTerm = ctx.freshName("arg")
+        val initArg = if (CatalystTypeConverters.isPrimitive(dt)) {
+          val convertedTerm = ctx.freshName("conv")
+          s"""
+             |${CodeGenerator.boxedType(dt)} $convertedTerm = ${eval.value};
 
 Review comment:
   Out of curiosity, why do we need this extra `convertedTerm` for the boxing? Could you instead do
   
   ```
   Object $argTerm = ${eval.isNull} ? null : ${eval.value};
   ```
   
   and avoid the use of an extra variable name? Or if you want more typechecking, do
   
   ```
   ${CodeGenerator.boxedType(dt)} $argTerm = ${eval.isNull} ? null : ${eval.value};
   ```
   
   and used the boxed type as `$argTerm`'s type?
   
   To avoid repetition and more tightly scope the conditional part of the argument convert logic, we even might consider something like this
   
   ```scala
   val boxedType = CodeGenerator.boxedType(dt)
   val maybeConverted = if (CatalystTypeConverters.isPrimitive(dt)) {
     eval.value
   } else {
     "$convertersTerm[$i].apply(${eval.value})"
   }
   s"$boxedType $argTerm = ${eval.isNull} ? null : $maybeConverted;"
   ```
   
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

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