Ngone51 commented on a change in pull request #28979:
URL: https://github.com/apache/spark/pull/28979#discussion_r452208986
##########
File path: sql/core/src/main/scala/org/apache/spark/sql/functions.scala
##########
@@ -4521,9 +4522,10 @@ object functions {
* @since 1.3.0
*/
def udf[RT: TypeTag](f: Function0[RT]): UserDefinedFunction = {
- val ScalaReflection.Schema(dataType, nullable) =
ScalaReflection.schemaFor[RT]
+ val outputEncoder = Try(ExpressionEncoder[RT]()).toOption
+ val ScalaReflection.Schema(dataType, nullable) =
outputEncoder.map(_.dataTypeAndNullable).getOrElse(ScalaReflection.schemaFor[RT])
Review comment:
Oh..wait!!
I just realized that we should actually compare `schemaFor` and
`serializerForType`(rather than `serializerFor`). `serializerForType` will
call `dataTypeFor` first. `dataTypeFor` actually supports `NullType`,
`CalendarInterval`, `Decimal` as well.
The reason `ExpressionEncoder` doesn't support `NullType` is, before
`ExpressionEncoder` calls `serializerForType`, it calls
`mirror.runtimeClass(tpe)` first:
https://github.com/apache/spark/blob/fab4ca5156d5e1cc0e976c7c27b28a12fa61eb6d/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/encoders/ExpressionEncoder.scala#L56-L58
and `null` (without explicit data type) can fail at
`mirror.runtimeClass(tpe)`:
```scala
java.lang.ClassNotFoundException: scala.Null
at
scala.reflect.internal.util.AbstractFileClassLoader.findClass(AbstractFileClassLoader.scala:72)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:398)
at
scala.reflect.runtime.JavaMirrors$JavaMirror.javaClass(JavaMirrors.scala:589)
at
scala.reflect.runtime.JavaMirrors$JavaMirror.$anonfun$classToJava$1(JavaMirrors.scala:1265)
at
scala.reflect.runtime.TwoWayCaches$TwoWayCache.$anonfun$toJava$1(TwoWayCaches.scala:61)
at
scala.reflect.runtime.TwoWayCaches$TwoWayCache.toJava(TwoWayCaches.scala:57)
at
scala.reflect.runtime.JavaMirrors$JavaMirror.classToJava(JavaMirrors.scala:1257)
at
scala.reflect.runtime.JavaMirrors$JavaMirror.typeToJavaClass(JavaMirrors.scala:1351)
at
scala.reflect.runtime.JavaMirrors$JavaMirror.runtimeClass(JavaMirrors.scala:227)
at
scala.reflect.runtime.JavaMirrors$JavaMirror.runtimeClass(JavaMirrors.scala:68)
at
org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$.apply(ExpressionEncoder.scala:56)
at org.apache.spark.sql.functions$.udf(functions.scala:4542)
... 47 elided
```
----------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]