mgaido91 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_r285353540
##########
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:
Well, actually my first trial was exactly what you are suggesting here, but
it didn't work: indeed it can cause compilation error (the error message is
something like `no common type for void and int`). Then, I also tried:
```
val boxedType = CodeGenerator.boxedType(dt)
val maybeConverted = if (CatalystTypeConverters.isPrimitive(dt)) {
s"(${boxedType}) eval.value"
} else {
"$convertersTerm[$i].apply(${eval.value})"
}
s"$boxedType $argTerm = ${eval.isNull} ? null : $maybeConverted;"
```
but this fails too with a confusing error message. Honestly, I am not sure
why this 2nd solution doesn't work, since I tried taking the code and compiling
it with jdk and it worked. My best guess is that it is a janino bug which
doesn't support it.
I did several trials but I haven't found any better alternative as this
seemed the only syntax working with janino.
----------------------------------------------------------------
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]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]