File tree Expand file tree Collapse file tree 2 files changed +10
-6
lines changed
catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions
core/src/test/scala/org/apache/spark/sql Expand file tree Collapse file tree 2 files changed +10
-6
lines changed Original file line number Diff line number Diff line change @@ -1029,8 +1029,11 @@ case class ScalaUDF(
1029
1029
// such as IntegerType, its javaType is `int` and the returned type of user-defined
1030
1030
// function is Object. Trying to convert an Object to `int` will cause casting exception.
1031
1031
val evalCode = evals.map(_.code).mkString
1032
- val funcArguments = converterTerms.zip(evals).map {
1033
- case (converter, eval) => s " $converter.apply( ${eval.value}) "
1032
+ val funcArguments = converterTerms.zipWithIndex.map {
1033
+ case (converter, i) =>
1034
+ val eval = evals(i)
1035
+ val dt = children(i).dataType
1036
+ s " $converter.apply( ${eval.isNull} ? null : ( ${ctx.boxedType(dt)}) ${eval.value}) "
1034
1037
}.mkString(" ," )
1035
1038
val callFunc = s " ${ctx.boxedType(ctx.javaType(dataType))} $resultTerm = " +
1036
1039
s " ( ${ctx.boxedType(ctx.javaType(dataType))}) ${catalystConverterTerm}" +
Original file line number Diff line number Diff line change @@ -1131,14 +1131,15 @@ class DataFrameSuite extends QueryTest with SharedSQLContext {
1131
1131
}
1132
1132
1133
1133
test(" SPARK-11725: correctly handle null inputs for ScalaUDF" ) {
1134
- val df = Seq (
1134
+ val df = sparkContext.parallelize( Seq (
1135
1135
new java.lang.Integer (22 ) -> " John" ,
1136
- null .asInstanceOf [java.lang.Integer ] -> " Lucy" ).toDF(" age" , " name" )
1136
+ null .asInstanceOf [java.lang.Integer ] -> " Lucy" )) .toDF(" age" , " name" )
1137
1137
1138
+ // passing null into the UDF that could handle it
1138
1139
val boxedUDF = udf[java.lang.Integer , java.lang.Integer ] {
1139
- (i : java.lang.Integer ) => if (i == null ) null else i * 2
1140
+ (i : java.lang.Integer ) => if (i == null ) - 10 else i * 2
1140
1141
}
1141
- checkAnswer(df.select(boxedUDF($" age" )), Row (44 ) :: Row (null ) :: Nil )
1142
+ checkAnswer(df.select(boxedUDF($" age" )), Row (44 ) :: Row (- 10 ) :: Nil )
1142
1143
1143
1144
val primitiveUDF = udf((i : Int ) => i * 2 )
1144
1145
checkAnswer(df.select(primitiveUDF($" age" )), Row (44 ) :: Row (null ) :: Nil )
You can’t perform that action at this time.
0 commit comments