How to deal with Spark UDF input/output of primitive nullable type

后端 未结 3 2092
走了就别回头了
走了就别回头了 2021-01-03 02:38

The issues:

1) Spark doesn\'t call UDF if input is column of primitive type that contains null:

inputDF.show()

+-----+
|  x  |
+-----+
         


        
3条回答
  •  佛祖请我去吃肉
    2021-01-03 02:56

    Based on the solution provided at SparkSQL: How to deal with null values in user defined function? by @zero323, an alternative way to achieve the requested result is:

    import scala.util.Try
    val udfHandlingNulls udf((x: Double) => Try(2.0).toOption)
    inputDF.withColumn("y", udfHandlingNulls($"x")).show()
    

提交回复
热议问题