Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,11 @@ 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
val funcArguments = converterTerms.zip(evals).map {
case (converter, eval) => s"$converter.apply(${eval.value})"
val funcArguments = converterTerms.zipWithIndex.map {
case (converter, i) =>
val eval = evals(i)
val dt = children(i).dataType
s"$converter.apply(${eval.isNull} ? null : (${ctx.boxedType(dt)}) ${eval.value})"
}.mkString(",")
val callFunc = s"${ctx.boxedType(ctx.javaType(dataType))} $resultTerm = " +
s"(${ctx.boxedType(ctx.javaType(dataType))})${catalystConverterTerm}" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,14 +1138,15 @@ class DataFrameSuite extends QueryTest with SharedSQLContext {
}

test("SPARK-11725: correctly handle null inputs for ScalaUDF") {
val df = Seq(
val df = sparkContext.parallelize(Seq(
new java.lang.Integer(22) -> "John",
null.asInstanceOf[java.lang.Integer] -> "Lucy").toDF("age", "name")
null.asInstanceOf[java.lang.Integer] -> "Lucy")).toDF("age", "name")

// passing null into the UDF that could handle it
val boxedUDF = udf[java.lang.Integer, java.lang.Integer] {
(i: java.lang.Integer) => if (i == null) null else i * 2
(i: java.lang.Integer) => if (i == null) -10 else i * 2
}
checkAnswer(df.select(boxedUDF($"age")), Row(44) :: Row(null) :: Nil)
checkAnswer(df.select(boxedUDF($"age")), Row(44) :: Row(-10) :: Nil)

val primitiveUDF = udf((i: Int) => i * 2)
checkAnswer(df.select(primitiveUDF($"age")), Row(44) :: Row(null) :: Nil)
Expand Down