Skip to content

Commit 6f03f85

Browse files
committed
Fix bug in Double / Float ordering
1 parent 42a1ad5 commit 6f03f85

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ object TypeUtils {
6969
if ((xIsNan && yIsNan) || (x == y)) 0
7070
else if (xIsNan) 1
7171
else if (yIsNan) -1
72-
else if (x > y) -1
73-
else 1
72+
else if (x > y) 1
73+
else -1
7474
}
7575

7676
def compareFloats(x: Float, y: Float): Int = {
@@ -79,7 +79,7 @@ object TypeUtils {
7979
if ((xIsNan && yIsNan) || (x == y)) 0
8080
else if (xIsNan) 1
8181
else if (yIsNan) -1
82-
else if (x > y) -1
83-
else 1
82+
else if (x > y) 1
83+
else -1
8484
}
8585
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TypeUtilsSuite.scala

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@
1717

1818
package org.apache.spark.sql.catalyst.util
1919

20+
import java.lang.{Double => JDouble, Float => JFloat}
21+
2022
import org.apache.spark.SparkFunSuite
2123

2224
class TypeUtilsSuite extends SparkFunSuite {
2325

2426
import TypeUtils._
2527

2628
test("compareDoubles") {
27-
assert(compareDoubles(0, 0) === 0)
28-
assert(compareDoubles(1, 0) === -1)
29-
assert(compareDoubles(0, 1) === 1)
30-
assert(compareDoubles(Double.MinValue, Double.MaxValue) === 1)
29+
def shouldMatchDefaultOrder(a: Double, b: Double): Unit = {
30+
assert(compareDoubles(a, b) === JDouble.compare(a, b))
31+
assert(compareDoubles(b, a) === JDouble.compare(b, a))
32+
}
33+
shouldMatchDefaultOrder(0d, 0d)
34+
shouldMatchDefaultOrder(0d, 1d)
35+
shouldMatchDefaultOrder(Double.MinValue, Double.MaxValue)
3136
assert(compareDoubles(Double.NaN, Double.NaN) === 0)
3237
assert(compareDoubles(Double.NaN, Double.PositiveInfinity) === 1)
3338
assert(compareDoubles(Double.NaN, Double.NegativeInfinity) === 1)
@@ -36,10 +41,13 @@ class TypeUtilsSuite extends SparkFunSuite {
3641
}
3742

3843
test("compareFloats") {
39-
assert(compareFloats(0, 0) === 0)
40-
assert(compareFloats(1, 0) === -1)
41-
assert(compareFloats(0, 1) === 1)
42-
assert(compareFloats(Float.MinValue, Float.MaxValue) === 1)
44+
def shouldMatchDefaultOrder(a: Float, b: Float): Unit = {
45+
assert(compareFloats(a, b) === JFloat.compare(a, b))
46+
assert(compareFloats(b, a) === JFloat.compare(b, a))
47+
}
48+
shouldMatchDefaultOrder(0f, 0f)
49+
shouldMatchDefaultOrder(1f, 1f)
50+
shouldMatchDefaultOrder(Float.MinValue, Float.MaxValue)
4351
assert(compareFloats(Float.NaN, Float.NaN) === 0)
4452
assert(compareFloats(Float.NaN, Float.PositiveInfinity) === 1)
4553
assert(compareFloats(Float.NaN, Float.NegativeInfinity) === 1)

0 commit comments

Comments
 (0)