Ngone51 commented on a change in pull request #28979:
URL: https://github.com/apache/spark/pull/28979#discussion_r452120811



##########
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:
       Hi all, in the latest update, acquring schema can fallback to previous 
`ScalaReflection.schemaFor[RT]` when `ExpressionEncoder[RT]()` fails. In this 
way, we can support both`udf((i: String) => null)`(return type is `NullType`) 
and `udf((i: String) => null.asInstanceOf[String])` (return type is 
`StringType`) at the same time and avoid behaviour change. cc @maropu 




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

Reply via email to