Skip to content

Commit

Permalink
passing null into ScalaUDF
Browse files Browse the repository at this point in the history
  • Loading branch information
Davies Liu committed Dec 10, 2015
1 parent 442a771 commit 4636fe3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
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

0 comments on commit 4636fe3

Please sign in to comment.