-
Notifications
You must be signed in to change notification settings - Fork 28.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-32764][SQL] -0.0 should be equal to 0.0
### What changes were proposed in this pull request? This is a Spark 3.0 regression introduced by #26761. We missed a corner case that `java.lang.Double.compare` treats 0.0 and -0.0 as different, which breaks SQL semantic. This PR adds back the `OrderingUtil`, to provide custom compare methods that take care of 0.0 vs -0.0 ### Why are the changes needed? Fix a correctness bug. ### Does this PR introduce _any_ user-facing change? Yes, now `SELECT 0.0 > -0.0` returns false correctly as Spark 2.x. ### How was this patch tested? new tests Closes #29647 from cloud-fan/float. Authored-by: Wenchen Fan <wenchen@databricks.com> Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
- Loading branch information
1 parent
c8c082c
commit 4144b6d
Showing
8 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
41 changes: 41 additions & 0 deletions
41
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/SQLOrderingUtil.scala
This file contains 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,41 @@ | ||
/* | ||
* 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.spark.sql.catalyst.util | ||
|
||
object SQLOrderingUtil { | ||
|
||
/** | ||
* A special version of double comparison that follows SQL semantic: | ||
* 1. NaN == NaN | ||
* 2. NaN is greater than any non-NaN double | ||
* 3. -0.0 == 0.0 | ||
*/ | ||
def compareDoubles(x: Double, y: Double): Int = { | ||
if (x == y) 0 else java.lang.Double.compare(x, y) | ||
} | ||
|
||
/** | ||
* A special version of float comparison that follows SQL semantic: | ||
* 1. NaN == NaN | ||
* 2. NaN is greater than any non-NaN float | ||
* 3. -0.0 == 0.0 | ||
*/ | ||
def compareFloats(x: Float, y: Float): Int = { | ||
if (x == y) 0 else java.lang.Float.compare(x, y) | ||
} | ||
} |
This file contains 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 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 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 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
75 changes: 75 additions & 0 deletions
75
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/SQLOrderingUtilSuite.scala
This file contains 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,75 @@ | ||
/* | ||
* 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.spark.sql.catalyst.util | ||
|
||
import java.lang.{Double => JDouble, Float => JFloat} | ||
|
||
import org.apache.spark.SparkFunSuite | ||
|
||
class SQLOrderingUtilSuite extends SparkFunSuite { | ||
|
||
test("compareDoublesSQL") { | ||
def shouldMatchDefaultOrder(a: Double, b: Double): Unit = { | ||
assert(SQLOrderingUtil.compareDoubles(a, b) === JDouble.compare(a, b)) | ||
assert(SQLOrderingUtil.compareDoubles(b, a) === JDouble.compare(b, a)) | ||
} | ||
shouldMatchDefaultOrder(0d, 0d) | ||
shouldMatchDefaultOrder(0d, 1d) | ||
shouldMatchDefaultOrder(-1d, 1d) | ||
shouldMatchDefaultOrder(Double.MinValue, Double.MaxValue) | ||
|
||
val specialNaN = JDouble.longBitsToDouble(0x7ff1234512345678L) | ||
assert(JDouble.isNaN(specialNaN)) | ||
assert(JDouble.doubleToRawLongBits(Double.NaN) != JDouble.doubleToRawLongBits(specialNaN)) | ||
|
||
assert(SQLOrderingUtil.compareDoubles(Double.NaN, Double.NaN) === 0) | ||
assert(SQLOrderingUtil.compareDoubles(Double.NaN, specialNaN) === 0) | ||
assert(SQLOrderingUtil.compareDoubles(Double.NaN, Double.PositiveInfinity) > 0) | ||
assert(SQLOrderingUtil.compareDoubles(specialNaN, Double.PositiveInfinity) > 0) | ||
assert(SQLOrderingUtil.compareDoubles(Double.NaN, Double.NegativeInfinity) > 0) | ||
assert(SQLOrderingUtil.compareDoubles(Double.PositiveInfinity, Double.NaN) < 0) | ||
assert(SQLOrderingUtil.compareDoubles(Double.NegativeInfinity, Double.NaN) < 0) | ||
assert(SQLOrderingUtil.compareDoubles(0.0d, -0.0d) === 0) | ||
assert(SQLOrderingUtil.compareDoubles(-0.0d, 0.0d) === 0) | ||
} | ||
|
||
test("compareFloatsSQL") { | ||
def shouldMatchDefaultOrder(a: Float, b: Float): Unit = { | ||
assert(SQLOrderingUtil.compareFloats(a, b) === JFloat.compare(a, b)) | ||
assert(SQLOrderingUtil.compareFloats(b, a) === JFloat.compare(b, a)) | ||
} | ||
shouldMatchDefaultOrder(0f, 0f) | ||
shouldMatchDefaultOrder(0f, 1f) | ||
shouldMatchDefaultOrder(-1f, 1f) | ||
shouldMatchDefaultOrder(Float.MinValue, Float.MaxValue) | ||
|
||
val specialNaN = JFloat.intBitsToFloat(-6966608) | ||
assert(JFloat.isNaN(specialNaN)) | ||
assert(JFloat.floatToRawIntBits(Float.NaN) != JFloat.floatToRawIntBits(specialNaN)) | ||
|
||
assert(SQLOrderingUtil.compareDoubles(Float.NaN, Float.NaN) === 0) | ||
assert(SQLOrderingUtil.compareDoubles(Float.NaN, specialNaN) === 0) | ||
assert(SQLOrderingUtil.compareDoubles(Float.NaN, Float.PositiveInfinity) > 0) | ||
assert(SQLOrderingUtil.compareDoubles(specialNaN, Float.PositiveInfinity) > 0) | ||
assert(SQLOrderingUtil.compareDoubles(Float.NaN, Float.NegativeInfinity) > 0) | ||
assert(SQLOrderingUtil.compareDoubles(Float.PositiveInfinity, Float.NaN) < 0) | ||
assert(SQLOrderingUtil.compareDoubles(Float.NegativeInfinity, Float.NaN) < 0) | ||
assert(SQLOrderingUtil.compareDoubles(0.0f, -0.0f) === 0) | ||
assert(SQLOrderingUtil.compareDoubles(-0.0f, 0.0f) === 0) | ||
} | ||
} |
This file contains 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