-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-17506][SQL] Improve the check double values equality rule. #15059
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
Changes from all commits
78f3733
1721e0c
b4ad8e3
f4ef207
6f91656
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ package org.apache.spark.sql.catalyst.expressions | |
|
|
||
| import org.scalacheck.Gen | ||
| import org.scalactic.TripleEqualsSupport.Spread | ||
| import org.scalatest.exceptions.TestFailedException | ||
| import org.scalatest.prop.GeneratorDrivenPropertyChecks | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
|
|
@@ -289,13 +290,37 @@ trait ExpressionEvalHelper extends GeneratorDrivenPropertyChecks { | |
| (result, expected) match { | ||
| case (result: Array[Byte], expected: Array[Byte]) => | ||
| java.util.Arrays.equals(result, expected) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW do you mean to remove this case? does it not apply now?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it should have been replaced by the new case below. |
||
| case (result: Double, expected: Spread[Double @unchecked]) => | ||
| expected.asInstanceOf[Spread[Double]].isWithin(result) | ||
| case (result: Double, expected: Double) if result.isNaN && expected.isNaN => | ||
| true | ||
| case (result: Double, expected: Double) => | ||
| relativeErrorComparison(result, expected) | ||
| case (result: Float, expected: Float) if result.isNaN && expected.isNaN => | ||
| true | ||
| case _ => result == expected | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Private helper function for comparing two values using relative tolerance. | ||
| * Note that if x or y is extremely close to zero, i.e., smaller than Double.MinPositiveValue, | ||
| * the relative tolerance is meaningless, so the exception will be raised to warn users. | ||
| * | ||
| * TODO: this duplicates functions in spark.ml.util.TestingUtils.relTol and | ||
| * spark.mllib.util.TestingUtils.relTol, they could be moved to common utils sub module for the | ||
| * whole spark project which does not depend on other modules. See more detail in discussion: | ||
| * https://github.com/apache/spark/pull/15059#issuecomment-246940444 | ||
| */ | ||
| private def relativeErrorComparison(x: Double, y: Double, eps: Double = 1E-8): Boolean = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems fine. You could refer to the source of this code and explain the duplication but it's not a big deal.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've add comment to indicate the problem. Thank you! |
||
| val absX = math.abs(x) | ||
| val absY = math.abs(y) | ||
| val diff = math.abs(x - y) | ||
| if (x == y) { | ||
| true | ||
| } else if (absX < Double.MinPositiveValue || absY < Double.MinPositiveValue) { | ||
| throw new TestFailedException( | ||
| s"$x or $y is extremely close to zero, so the relative tolerance is meaningless.", 0) | ||
| } else { | ||
| diff < eps * math.min(absX, absY) | ||
| } | ||
| } | ||
| } | ||
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.
this TODO is not fixed yet, why remove it?
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.
The result of interpret and codegen for remainder between giant values are equal within relative tolerance, so maybe this no longer requires to be resolved. Thanks!
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.
Ah, it seemed worth removing because the change does apparently make this test pass, and that's what the comment refers to. If it's still an issue, we can restore a modified version of the comment.
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.
nvm, it's fixed in #15171