This is an automated email from the ASF dual-hosted git repository.

ptoth pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.1 by this push:
     new caa68b192883 [SPARK-55975][SQL][TESTS] NaN comparison can cause false 
UT failures due to different NaNs
caa68b192883 is described below

commit caa68b192883f9fc942fc0b7acac6137c1f874b5
Author: Marco Gaido <[email protected]>
AuthorDate: Fri Mar 13 09:43:14 2026 +0100

    [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 
https://github.com/apache/spark/pull/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 <[email protected]>
    Signed-off-by: Peter Toth <[email protected]>
    (cherry picked from commit e7bbd323ec75e0ef96658c588146950131756f1e)
    Signed-off-by: Peter Toth <[email protected]>
---
 .../src/test/scala/org/apache/spark/sql/MathFunctionsSuite.scala | 9 +++++++++
 sql/core/src/test/scala/org/apache/spark/sql/QueryTest.scala     | 3 +++
 2 files changed, 12 insertions(+)

diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/MathFunctionsSuite.scala 
b/sql/core/src/test/scala/org/apache/spark/sql/MathFunctionsSuite.scala
index 23d3fc3d2fe5..42f15a04fd58 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/MathFunctionsSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/MathFunctionsSuite.scala
@@ -117,6 +117,15 @@ class MathFunctionsSuite extends QueryTest with 
SharedSparkSession {
     )
   }
 
+  test("different NaN comparison") {
+    assert(QueryTest.compare(
+      java.lang.Double.longBitsToDouble(0x7ff8000000000000L),
+      java.lang.Double.longBitsToDouble(0xfff8000000000000L)))
+    assert(QueryTest.compare(
+      java.lang.Float.intBitsToFloat(0x7fc00000),
+      java.lang.Float.intBitsToFloat(0xffc00000)))
+  }
+
   test("sin") {
     testOneToOneMathFunction(sin, math.sin)
   }
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/QueryTest.scala 
b/sql/core/src/test/scala/org/apache/spark/sql/QueryTest.scala
index 515a60228405..78c1bfe773ea 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/QueryTest.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/QueryTest.scala
@@ -409,9 +409,12 @@ object QueryTest extends Assertions {
     case (a: Row, b: Row) =>
       compare(a.toSeq, b.toSeq)
     // 0.0 == -0.0, turn float/double to bits before comparison, to 
distinguish 0.0 and -0.0.
+    // in some hardware NaN can be represented with different bits, so first 
check for it
     case (a: Double, b: Double) =>
+      a.isNaN && b.isNaN ||
       java.lang.Double.doubleToRawLongBits(a) == 
java.lang.Double.doubleToRawLongBits(b)
     case (a: Float, b: Float) =>
+      a.isNaN && b.isNaN ||
       java.lang.Float.floatToRawIntBits(a) == 
java.lang.Float.floatToRawIntBits(b)
     case (a, b) => a == b
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to