-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-30009][CORE][SQL] Support different floating-point Ordering for Scala 2.12 / 2.13 #26654
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
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * 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.util | ||
|
|
||
| /** | ||
| * This class only exists to bridge the difference between Scala 2.12 and Scala 2.13's | ||
| * support for floating-point ordering. It is implemented separately for both as there | ||
| * is no method that exists in both for comparison. | ||
| * | ||
| * It functions like Ordering.Double in Scala 2.12. | ||
| */ | ||
| private[spark] object OrderingUtil { | ||
|
|
||
| def compareDouble(x: Double, y: Double): Int = Ordering.Double.compare(x, y) | ||
|
|
||
| def compareFloat(x: Float, y: Float): Int = Ordering.Float.compare(x, y) | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * 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.util | ||
|
|
||
| /** | ||
| * This class only exists to bridge the difference between Scala 2.12 and Scala 2.13's | ||
| * support for floating-point ordering. It is implemented separately for both as there | ||
| * is no method that exists in both for comparison. | ||
| * | ||
| * It functions like Ordering.Double.TotalOrdering in Scala 2.13, which matches java.lang.Double | ||
| * rather than Scala 2.12's Ordering.Double in handling of NaN. | ||
| */ | ||
| private[spark] object OrderingUtil { | ||
|
|
||
| def compareDouble(x: Double, y: Double): Int = Ordering.Double.TotalOrdering.compare(x, y) | ||
|
Contributor
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. after another thought, shall we just use
Contributor
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. This is also how Scala 2.12 implements
Contributor
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. Then we don't need to have branches between Scala 2.12 and 2.13. We can also use
Member
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 that sounds fine and simpler. If
Member
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. See #26761 |
||
|
|
||
| def compareFloat(x: Float, y: Float): Int = Ordering.Float.TotalOrdering.compare(x, y) | ||
|
|
||
| } | ||
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.
hmm, doesn't Scala 2.12's Ordering.Double.compare delegates to java.lang.Double.compare?
why this matches java.lang.Double, but not Scala 2.12's Ordering.Double?
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.
It does, for
compare. The superclassOrderingthen defines operations likelt,lteq, etc in terms ofcompare. But 2.12Ordering.Doubleoverrides them to use operations like<,<=. As far as I can tell it presents a consistent total ordering viacomparealready (as it appearsjava.lang.Double.comparedoes), but its comparisons aren't consistent with how NaNs behave inlt, etc. Then again... perhaps neither is Java. ItsComparators.natural()would work consistently, but the comparisons don't match the Java operators.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.
So I guess the question is, when to the
Orderings get used by Spark? it's not immediately clear if they are, even. If they're used for sorting, all is well... I think, as sorting would usecompareand all of the impls in question behave the same way.If it's used to evaluate how doubles compare somewhere, then, should those answers be consistent with the sort ordering? or Java/Scala operators? I'd presume the former, but, that's not how it works right now. And the choice to use
TotalOrderingchanges that in 2.13.IeeeOrderingin 2.13 to be conservativeTotalOrderingfeels more logical.TotalOrdering. Then the 2.13 choice is already correct,TotalOrdering.I actually suspect it doesn't matter, doesn't get used.