comphead commented on code in PR #6642:
URL: https://github.com/apache/datafusion-comet/pull/6642#discussion_r4488371383


##########
spark/src/test/scala/org/apache/comet/CometAnswerToleranceSuite.scala:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.comet
+
+import org.scalatest.exceptions.TestFailedException
+
+import org.apache.spark.sql.{CometTestBase, DataFrame, Row}
+
+/**
+ * Tests the tolerance comparison behind `query tolerance=` in Comet SQL tests 
and the
+ * `checkSparkAnswer*WithTol*` helpers. The DataFrame stands in for the Comet 
result and the rows
+ * for the Spark answer.
+ */
+class CometAnswerToleranceSuite extends CometTestBase {
+  import testImplicits._
+
+  private def assertMatch(comet: DataFrame, spark: Row*): Unit =
+    checkCometAnswerWithTolerance(comet, spark, 1e-6)
+
+  private def assertMismatch(comet: DataFrame, spark: Row*): Unit = {
+    val e = 
intercept[TestFailedException](checkCometAnswerWithTolerance(comet, spark, 
1e-6))
+    assert(e.getMessage.startsWith("Results do not match within tolerance"), 
e.getMessage)
+  }
+
+  test("finite values match within the tolerance") {
+    assertMatch(Seq(1.0 + 1e-7).toDF("d"), Row(1.0))
+    assertMismatch(Seq(1.0 + 1e-5).toDF("d"), Row(1.0))
+    assertMatch(Seq(1.0f).toDF("f"), Row(1.0f))
+    assertMismatch(Seq(1.001f).toDF("f"), Row(1.0f))
+  }
+
+  test("NaN matches only NaN") {
+    assertMatch(Seq(Double.NaN).toDF("d"), Row(Double.NaN))
+    assertMismatch(Seq(Double.NaN).toDF("d"), Row(1.0))
+    assertMismatch(Seq(1.0).toDF("d"), Row(Double.NaN))
+    assertMismatch(Seq(Double.NaN).toDF("d"), Row(Double.PositiveInfinity))
+    assertMatch(Seq(Float.NaN).toDF("f"), Row(Float.NaN))
+    assertMismatch(Seq(Float.NaN).toDF("f"), Row(1.0f))
+    assertMismatch(Seq(1.0f).toDF("f"), Row(Float.NaN))
+  }
+
+  test("infinities compare with their sign") {
+    assertMatch(Seq(Double.PositiveInfinity).toDF("d"), 
Row(Double.PositiveInfinity))
+    assertMatch(Seq(Double.NegativeInfinity).toDF("d"), 
Row(Double.NegativeInfinity))
+    assertMismatch(Seq(Double.PositiveInfinity).toDF("d"), 
Row(Double.NegativeInfinity))
+    assertMismatch(Seq(Double.NegativeInfinity).toDF("d"), 
Row(Double.PositiveInfinity))
+    assertMismatch(Seq(Double.MaxValue).toDF("d"), 
Row(Double.PositiveInfinity))
+    assertMismatch(Seq(Float.PositiveInfinity).toDF("f"), 
Row(Float.NegativeInfinity))
+  }
+
+  test("signed zeros match each other") {
+    assertMatch(Seq(-0.0).toDF("d"), Row(0.0))
+    assertMatch(Seq(0.0f).toDF("f"), Row(-0.0f))
+  }
+
+  test("other values compare exactly") {
+    assertMatch(Seq(("a", 1.0)).toDF("s", "d"), Row("a", 1.0 + 1e-7))
+    assertMismatch(Seq(("a", 1.0)).toDF("s", "d"), Row("b", 1.0))
+    // Only top-level floating-point values get the tolerance.
+    assertMismatch(Seq(Seq(1.0 + 1e-7)).toDF("a"), Row(Seq(1.0)))
+    assertMismatch(Seq(Seq(-0.0)).toDF("a"), Row(Seq(0.0)))
+    assertMatch(Seq(Seq(Double.NaN)).toDF("a"), Row(Seq(Double.NaN)))
+  }
+
+  test("rows are sorted before they are paired") {
+    assertMatch(
+      Seq(Some(Double.NaN), Some(1.0), None, Some(Double.NegativeInfinity), 
Some(-0.0)).toDF("d"),
+      Row(0.0),
+      Row(Double.NegativeInfinity),
+      Row(null),
+      Row(1.0),
+      Row(Double.NaN))
+    // Sorting by `toString` would put 1.2E-16 after 0.5 but 0.0 before it, 
and pair 0.5 with 0.0.
+    assertMatch(Seq(0.5, 1.2246467991473532e-16).toDF("d"), Row(0.0), Row(0.5))
+    assertMismatch(Seq(1.0, 2.0).toDF("d"), Row(1.0), Row(Double.NaN))
+    assertMismatch(Seq(1.0, 2.0).toDF("d"), Row(1.0))
+  }

Review Comment:
   Could the suite also pin a `null` against a number, in both directions? For 
example `assertMismatch(Seq(Option.empty[Double]).toDF("d"), Row(0.0))` and the 
reverse with `Seq(Some(0.0)).toDF("d")` against `Row(null)`. The only `null` 
case here pairs `null` with `null`. In `toleranceGroupKey` a `null` float field 
becomes `Some(null)` while a non-null one becomes `None`, so this mismatch is 
decided by the grouping step and not by `valuesMatch`, and nothing currently 
fails if that distinction is lost. I have not run these, so I expect both to be 
rejected today.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to