Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,9 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(Remainder(positiveLongLit, positiveLongLit), 0L)
checkEvaluation(Remainder(negativeLongLit, negativeLongLit), 0L)

// TODO: the following lines would fail the test due to inconsistency result of interpret
// and codegen for remainder between giant values, seems like a numeric stability issue
// DataTypeTestUtils.numericTypeWithoutDecimal.foreach { tpe =>
// checkConsistencyBetweenInterpretedAndCodegen(Remainder, tpe, tpe)
// }
DataTypeTestUtils.numericTypeWithoutDecimal.foreach { tpe =>
Copy link
Contributor

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?

Copy link
Contributor Author

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!

Copy link
Member

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.

Copy link
Contributor

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

checkConsistencyBetweenInterpretedAndCodegen(Remainder, tpe, tpe)
}
}

test("Abs") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -289,13 +290,37 @@ trait ExpressionEvalHelper extends GeneratorDrivenPropertyChecks {
(result, expected) match {
case (result: Array[Byte], expected: Array[Byte]) =>
java.util.Arrays.equals(result, expected)
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 = {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
}
}