Skip to content

[SPARK-26382][CORE] prefix comparator should handle -0.0 #23334

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

Closed
wants to merge 2 commits into from
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 @@ -69,6 +69,8 @@ public static final class DoublePrefixComparator {
* details see http://stereopsis.com/radix.html.
*/
public static long computePrefix(double value) {
// normalize -0.0 to 0.0, as they should be equal
value = value == -0.0 ? 0.0 : value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, value == -0.0 is true for both 0.0 and -0.0, but the current one is the simplest one for this normalization. We don't need to exclude 0.0 here. +1 for the logic.

// Java's doubleToLongBits already canonicalizes all NaN values to the smallest possible
// positive NaN, so there's nothing special we need to do for NaNs.
long bits = Double.doubleToLongBits(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class PrefixComparatorsSuite extends SparkFunSuite with PropertyChecks {
val nan2Prefix = PrefixComparators.DoublePrefixComparator.computePrefix(nan2)
assert(nan1Prefix === nan2Prefix)
val doubleMaxPrefix = PrefixComparators.DoublePrefixComparator.computePrefix(Double.MaxValue)
// NaN is greater than the max double value.
assert(PrefixComparators.DOUBLE.compare(nan1Prefix, doubleMaxPrefix) === 1)
}

Expand All @@ -134,22 +135,34 @@ class PrefixComparatorsSuite extends SparkFunSuite with PropertyChecks {
assert(java.lang.Double.doubleToRawLongBits(negativeNan) < 0)
val prefix = PrefixComparators.DoublePrefixComparator.computePrefix(negativeNan)
val doubleMaxPrefix = PrefixComparators.DoublePrefixComparator.computePrefix(Double.MaxValue)
// -NaN is greater than the max double value.
assert(PrefixComparators.DOUBLE.compare(prefix, doubleMaxPrefix) === 1)
}

test("double prefix comparator handles other special values properly") {
val nullValue = 0L
// See `SortPrefix.nullValue` for how we deal with nulls for float/double type
val smallestNullPrefix = 0L
val largestNullPrefix = -1L
val nan = PrefixComparators.DoublePrefixComparator.computePrefix(Double.NaN)
val posInf = PrefixComparators.DoublePrefixComparator.computePrefix(Double.PositiveInfinity)
val negInf = PrefixComparators.DoublePrefixComparator.computePrefix(Double.NegativeInfinity)
val minValue = PrefixComparators.DoublePrefixComparator.computePrefix(Double.MinValue)
val maxValue = PrefixComparators.DoublePrefixComparator.computePrefix(Double.MaxValue)
val zero = PrefixComparators.DoublePrefixComparator.computePrefix(0.0)
val minusZero = PrefixComparators.DoublePrefixComparator.computePrefix(-0.0)

// null is greater than everything including NaN, when we need to treat it as the largest value.
assert(PrefixComparators.DOUBLE.compare(largestNullPrefix, nan) === 1)
// NaN is greater than the positive infinity.
assert(PrefixComparators.DOUBLE.compare(nan, posInf) === 1)
assert(PrefixComparators.DOUBLE.compare(posInf, maxValue) === 1)
assert(PrefixComparators.DOUBLE.compare(maxValue, zero) === 1)
assert(PrefixComparators.DOUBLE.compare(zero, minValue) === 1)
assert(PrefixComparators.DOUBLE.compare(minValue, negInf) === 1)
assert(PrefixComparators.DOUBLE.compare(negInf, nullValue) === 1)
// null is smaller than everything including negative infinity, when we need to treat it as
// the smallest value.
assert(PrefixComparators.DOUBLE.compare(negInf, smallestNullPrefix) === 1)
// 0.0 should be equal to -0.0.
assert(PrefixComparators.DOUBLE.compare(zero, minusZero) === 0)
}
}