Skip to content

Commit 955f49a

Browse files
committed
Fix a % b if a is a double
1 parent 7e418e9 commit 955f49a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,11 @@ case class Remainder(left: Expression, right: Expression)
310310
if (input1 == null) {
311311
null
312312
} else {
313-
integral.rem(input1, input2)
313+
input1 match {
314+
case d: Double => d % input2.asInstanceOf[java.lang.Double]
315+
case f: Float => f % input2.asInstanceOf[java.lang.Float]
316+
case _ => integral.rem(input1, input2)
317+
}
314318
}
315319
}
316320
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
175175
}
176176
}
177177

178+
test("SPARK-17617: % (Remainder) double % double on super big double") {
179+
val leftDouble = Literal(-5083676433652386516D)
180+
val rightDouble = Literal(10D)
181+
checkEvaluation(Remainder(leftDouble, rightDouble), -6.0D)
182+
183+
// Float has smaller precision
184+
val leftFloat = Literal(-5083676433652386516F)
185+
val rightFloat = Literal(10F)
186+
checkEvaluation(Remainder(leftFloat, rightFloat), -2.0F)
187+
}
188+
178189
test("Abs") {
179190
testNumericDataTypes { convert =>
180191
val input = Literal(convert(1))

0 commit comments

Comments
 (0)