Skip to content

Commit 755b09d

Browse files
mgaido91peter-toth
authored andcommitted
[SPARK-55975][SQL][TESTS] NaN comparison can cause false UT failures due to different NaNs
### What changes were proposed in this pull request? In the UTs, currently NaN comparisons can cause UT failures on the CI (which is also hardware dependent) as on some hardware NaN can be represented in different byte formats (see IEEE-754 that allows multiple bit patterns for NaN). This has caused spurious UT failures e.g. in #54676. ### Why are the changes needed? Fix NaN comparison so that we do not run into hardware-dependent UT failures and correctly passes when there are two NaNs. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Added UT. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #54772 from mgaido91/SPARK-55975. Authored-by: Marco Gaido <mgaido@fbk.eu> Signed-off-by: Peter Toth <peter.toth@gmail.com> (cherry picked from commit e7bbd32) Signed-off-by: Peter Toth <peter.toth@gmail.com>
1 parent 88704b9 commit 755b09d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

sql/core/src/test/scala/org/apache/spark/sql/MathFunctionsSuite.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ class MathFunctionsSuite extends QueryTest with SharedSparkSession {
117117
)
118118
}
119119

120+
test("different NaN comparison") {
121+
assert(QueryTest.compare(
122+
java.lang.Double.longBitsToDouble(0x7ff8000000000000L),
123+
java.lang.Double.longBitsToDouble(0xfff8000000000000L)))
124+
assert(QueryTest.compare(
125+
java.lang.Float.intBitsToFloat(0x7fc00000),
126+
java.lang.Float.intBitsToFloat(0xffc00000)))
127+
}
128+
120129
test("sin") {
121130
testOneToOneMathFunction(sin, math.sin)
122131
}

sql/core/src/test/scala/org/apache/spark/sql/QueryTest.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,12 @@ object QueryTest extends Assertions {
405405
case (a: Row, b: Row) =>
406406
compare(a.toSeq, b.toSeq)
407407
// 0.0 == -0.0, turn float/double to bits before comparison, to distinguish 0.0 and -0.0.
408+
// in some hardware NaN can be represented with different bits, so first check for it
408409
case (a: Double, b: Double) =>
410+
a.isNaN && b.isNaN ||
409411
java.lang.Double.doubleToRawLongBits(a) == java.lang.Double.doubleToRawLongBits(b)
410412
case (a: Float, b: Float) =>
413+
a.isNaN && b.isNaN ||
411414
java.lang.Float.floatToRawIntBits(a) == java.lang.Float.floatToRawIntBits(b)
412415
case (a, b) => a == b
413416
}

0 commit comments

Comments
 (0)