Repository navigation
test: compare NaN, infinity sign and row order in tolerance checks #6642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andygrove
wants to merge
5
commits into
apache:main
Choose a base branch
from
andygrove:test/strict-tolerance-checks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea70afb
test: compare NaN, infinity sign and row order in tolerance checks
andygrove c041056
Merge apache/main into test/strict-tolerance-checks
andygrove 054ae39
test: pair unordered rows one-to-one in tolerance checks
andygrove 611cfc9
Merge remote-tracking branch 'apache/main' into test/strict-tolerance…
andygrove 567195c
test: pin a null against a number in the tolerance suite, and fix the…
andygrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
spark/src/test/scala/org/apache/comet/CometAnswerToleranceSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * 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("null matches only null") { | ||
| assertMatch(Seq(Option.empty[Double]).toDF("d"), Row(null)) | ||
| assertMismatch(Seq(Option.empty[Double]).toDF("d"), Row(0.0)) | ||
| assertMismatch(Seq(Some(0.0)).toDF("d"), Row(null)) | ||
| assertMismatch(Seq(Option.empty[Float]).toDF("f"), Row(0.0f)) | ||
| assertMismatch(Seq(Some(0.0f)).toDF("f"), Row(null)) | ||
| // A null does not pair with a number when the row has more than one field either. | ||
| assertMismatch(Seq((Option.empty[Double], "a")).toDF("d", "s"), Row(0.0, "a")) | ||
| assertMismatch(Seq((Some(0.0), "a")).toDF("d", "s"), Row(null, "a")) | ||
| } | ||
|
|
||
| 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)) | ||
| } | ||
|
|
||
| test("rows of an unordered query pair one-to-one") { | ||
| // A leading value within tolerance must not pair rows whose other fields differ. | ||
| assertMatch(Seq((1e-7, "a"), (0.0, "b")).toDF("d", "s"), Row(0.0, "a"), Row(0.0, "b")) | ||
| assertMismatch(Seq((1e-7, "b"), (0.0, "b")).toDF("d", "s"), Row(0.0, "a"), Row(0.0, "b")) | ||
| // The same with a second floating-point field, which sorting alone cannot pair. | ||
| assertMatch(Seq((1e-7, 1.0), (0.0, 2.0)).toDF("x", "y"), Row(0.0, 1.0), Row(0.0, 2.0)) | ||
| // The first Spark row matches both Comet rows, the second only the first Comet row. Pairing | ||
| // in order, or giving each Spark row the first free Comet row it matches, pairs the first | ||
| // rows and leaves the second Spark row without a partner. | ||
| assertMatch(Seq((2e-7, 5e-7), (3e-7, -8e-7)).toDF("x", "y"), Row(0.0, 0.0), Row(1e-7, 9e-7)) | ||
| // Every row matches a row on the other side, but not one of its own. | ||
| assertMismatch( | ||
| Seq((0.0, 0.0), (1.0, 1.0), (1.0, 1.0)).toDF("x", "y"), | ||
| Row(0.0, 0.0), | ||
| Row(0.0, 0.0), | ||
| Row(1.0, 1.0)) | ||
| // Maps match whatever the order of their entries, which their string form does not ignore. | ||
| assertMatch( | ||
| Seq(Map("b" -> 2, "a" -> 1), Map("a" -> 2, "b" -> 1)).toDF("m"), | ||
| Row(Map("a" -> 1, "b" -> 2)), | ||
| Row(Map("b" -> 1, "a" -> 2))) | ||
| assertMismatch( | ||
| Seq(Map("b" -> 2, "a" -> 1), Map("a" -> 1, "b" -> 2)).toDF("m"), | ||
| Row(Map("a" -> 1, "b" -> 2)), | ||
| Row(Map("b" -> 1, "a" -> 2))) | ||
| } | ||
|
|
||
| test("rows of a sorted query are paired in order") { | ||
| assertMatch(Seq(1.0, 2.0).toDF("d").orderBy($"d".desc), Row(2.0), Row(1.0)) | ||
| assertMismatch(Seq(1.0, 2.0).toDF("d").orderBy($"d".desc), Row(1.0), Row(2.0)) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the suite also pin a
nullagainst a number, in both directions? For exampleassertMismatch(Seq(Option.empty[Double]).toDF("d"), Row(0.0))and the reverse withSeq(Some(0.0)).toDF("d")againstRow(null). The onlynullcase here pairsnullwithnull. IntoleranceGroupKeyanullfloat field becomesSome(null)while a non-null one becomesNone, so this mismatch is decided by the grouping step and not byvaluesMatch, and nothing currently fails if that distinction is lost. I have not run these, so I expect both to be rejected today.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
null matches only nullto the suite, in both directions forDoubleandFloat, and with a second field in the row. You expected both to be rejected, and they are. I also tried moving anullfield into the float group intoleranceGroupKey, and the suite stays green, becauserowsMatchfalls through toQueryTest.compare(null, 0.0)for the pair, which is false. So grouping and the pairwise comparison each reject it, and the tests pin the result rather than one of the two steps. Pushed in 567195c.