@@ -23,6 +23,7 @@ import com.google.common.math.LongMath
2323
2424import org .apache .spark .SparkFunSuite
2525import org .apache .spark .sql .catalyst .InternalRow
26+ import org .apache .spark .sql .catalyst .analysis .TypeCoercion .ImplicitTypeCasts .implicitCast
2627import org .apache .spark .sql .catalyst .dsl .expressions ._
2728import org .apache .spark .sql .catalyst .expressions .codegen .GenerateMutableProjection
2829import org .apache .spark .sql .catalyst .optimizer .SimpleTestOptimizer
@@ -223,6 +224,14 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
223224 def f : (Double ) => Double = (x : Double ) => 1 / math.tan(x)
224225 testUnary(Cot , f)
225226 checkConsistencyBetweenInterpretedAndCodegen(Cot , DoubleType )
227+ val nullLit = Literal .create(null , NullType )
228+ val intNullLit = Literal .create(null , IntegerType )
229+ val intLit = Literal .create(1 , IntegerType )
230+ checkEvaluation(checkDataTypeAndCast(Cot (nullLit)), null , EmptyRow )
231+ checkEvaluation(checkDataTypeAndCast(Cot (intNullLit)), null , EmptyRow )
232+ checkEvaluation(checkDataTypeAndCast(Cot (intLit)), 1 / math.tan(1 ), EmptyRow )
233+ checkEvaluation(checkDataTypeAndCast(Cot (- intLit)), 1 / math.tan(- 1 ), EmptyRow )
234+ checkEvaluation(checkDataTypeAndCast(Cot (0 )), 1 / math.tan(0 ), EmptyRow )
226235 }
227236
228237 test(" atan" ) {
@@ -250,6 +259,11 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
250259 checkConsistencyBetweenInterpretedAndCodegen(Cbrt , DoubleType )
251260 }
252261
262+ def checkDataTypeAndCast (expression : UnaryMathExpression ): Expression = {
263+ val expNew = implicitCast(expression.child, expression.inputTypes(0 )).getOrElse(expression)
264+ expression.withNewChildren(Seq (expNew))
265+ }
266+
253267 test(" ceil" ) {
254268 testUnary(Ceil , (d : Double ) => math.ceil(d).toLong)
255269 checkConsistencyBetweenInterpretedAndCodegen(Ceil , DoubleType )
@@ -262,12 +276,22 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
262276 val doublePi : Double = 3.1415
263277 val floatPi : Float = 3.1415f
264278 val longLit : Long = 12345678901234567L
265- checkEvaluation(Ceil (doublePi), 4L , EmptyRow )
266- checkEvaluation(Ceil (floatPi.toDouble), 4L , EmptyRow )
267- checkEvaluation(Ceil (longLit), longLit, EmptyRow )
268- checkEvaluation(Ceil (- doublePi), - 3L , EmptyRow )
269- checkEvaluation(Ceil (- floatPi.toDouble), - 3L , EmptyRow )
270- checkEvaluation(Ceil (- longLit), - longLit, EmptyRow )
279+ val nullLit = Literal .create(null , NullType )
280+ val floatNullLit = Literal .create(null , FloatType )
281+ checkEvaluation(checkDataTypeAndCast(Ceil (doublePi)), 4L , EmptyRow )
282+ checkEvaluation(checkDataTypeAndCast(Ceil (floatPi)), 4L , EmptyRow )
283+ checkEvaluation(checkDataTypeAndCast(Ceil (longLit)), longLit, EmptyRow )
284+ checkEvaluation(checkDataTypeAndCast(Ceil (- doublePi)), - 3L , EmptyRow )
285+ checkEvaluation(checkDataTypeAndCast(Ceil (- floatPi)), - 3L , EmptyRow )
286+ checkEvaluation(checkDataTypeAndCast(Ceil (- longLit)), - longLit, EmptyRow )
287+
288+ checkEvaluation(checkDataTypeAndCast(Ceil (nullLit)), null , EmptyRow )
289+ checkEvaluation(checkDataTypeAndCast(Ceil (floatNullLit)), null , EmptyRow )
290+ checkEvaluation(checkDataTypeAndCast(Ceil (0 )), 0L , EmptyRow )
291+ checkEvaluation(checkDataTypeAndCast(Ceil (1 )), 1L , EmptyRow )
292+ checkEvaluation(checkDataTypeAndCast(Ceil (1234567890123456L )), 1234567890123456L , EmptyRow )
293+ checkEvaluation(checkDataTypeAndCast(Ceil (0.01 )), 1L , EmptyRow )
294+ checkEvaluation(checkDataTypeAndCast(Ceil (- 0.10 )), 0L , EmptyRow )
271295 }
272296
273297 test(" floor" ) {
@@ -282,12 +306,22 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
282306 val doublePi : Double = 3.1415
283307 val floatPi : Float = 3.1415f
284308 val longLit : Long = 12345678901234567L
285- checkEvaluation(Floor (doublePi), 3L , EmptyRow )
286- checkEvaluation(Floor (floatPi.toDouble), 3L , EmptyRow )
287- checkEvaluation(Floor (longLit), longLit, EmptyRow )
288- checkEvaluation(Floor (- doublePi), - 4L , EmptyRow )
289- checkEvaluation(Floor (- floatPi.toDouble), - 4L , EmptyRow )
290- checkEvaluation(Floor (- longLit), - longLit, EmptyRow )
309+ val nullLit = Literal .create(null , NullType )
310+ val floatNullLit = Literal .create(null , FloatType )
311+ checkEvaluation(checkDataTypeAndCast(Floor (doublePi)), 3L , EmptyRow )
312+ checkEvaluation(checkDataTypeAndCast(Floor (floatPi)), 3L , EmptyRow )
313+ checkEvaluation(checkDataTypeAndCast(Floor (longLit)), longLit, EmptyRow )
314+ checkEvaluation(checkDataTypeAndCast(Floor (- doublePi)), - 4L , EmptyRow )
315+ checkEvaluation(checkDataTypeAndCast(Floor (- floatPi)), - 4L , EmptyRow )
316+ checkEvaluation(checkDataTypeAndCast(Floor (- longLit)), - longLit, EmptyRow )
317+
318+ checkEvaluation(checkDataTypeAndCast(Floor (nullLit)), null , EmptyRow )
319+ checkEvaluation(checkDataTypeAndCast(Floor (floatNullLit)), null , EmptyRow )
320+ checkEvaluation(checkDataTypeAndCast(Floor (0 )), 0L , EmptyRow )
321+ checkEvaluation(checkDataTypeAndCast(Floor (1 )), 1L , EmptyRow )
322+ checkEvaluation(checkDataTypeAndCast(Floor (1234567890123456L )), 1234567890123456L , EmptyRow )
323+ checkEvaluation(checkDataTypeAndCast(Floor (0.01 )), 0L , EmptyRow )
324+ checkEvaluation(checkDataTypeAndCast(Floor (- 0.10 )), - 1L , EmptyRow )
291325 }
292326
293327 test(" factorial" ) {
@@ -541,10 +575,14 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
541575 val intPi : Int = 314159265
542576 val longPi : Long = 31415926535897932L
543577 val bdPi : BigDecimal = BigDecimal (31415927L , 7 )
578+ val floatPi : Float = 3.1415f
544579
545580 val doubleResults : Seq [Double ] = Seq (0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 3.0 , 3.1 , 3.14 , 3.142 ,
546581 3.1416 , 3.14159 , 3.141593 )
547582
583+ val floatResults : Seq [Float ] = Seq (0.0f , 0.0f , 0.0f , 0.0f , 0.0f , 0.0f , 3.0f , 3.1f , 3.14f ,
584+ 3.141f , 3.1415f , 3.1415f , 3.1415f )
585+
548586 val shortResults : Seq [Short ] = Seq [Short ](0 , 0 , 30000 , 31000 , 31400 , 31420 ) ++
549587 Seq .fill[Short ](7 )(31415 )
550588
@@ -563,10 +601,12 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
563601 checkEvaluation(Round (shortPi, scale), shortResults(i), EmptyRow )
564602 checkEvaluation(Round (intPi, scale), intResults(i), EmptyRow )
565603 checkEvaluation(Round (longPi, scale), longResults(i), EmptyRow )
604+ checkEvaluation(Round (floatPi, scale), floatResults(i), EmptyRow )
566605 checkEvaluation(BRound (doublePi, scale), doubleResults(i), EmptyRow )
567606 checkEvaluation(BRound (shortPi, scale), shortResults(i), EmptyRow )
568607 checkEvaluation(BRound (intPi, scale), intResultsB(i), EmptyRow )
569608 checkEvaluation(BRound (longPi, scale), longResults(i), EmptyRow )
609+ checkEvaluation(BRound (floatPi, scale), floatResults(i), EmptyRow )
570610 }
571611
572612 val bdResults : Seq [BigDecimal ] = Seq (BigDecimal (3.0 ), BigDecimal (3.1 ), BigDecimal (3.14 ),
0 commit comments