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
27 changes: 24 additions & 3 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
</parent>

<artifactId>spark-core_2.12</artifactId>
<properties>
<sbt.project.name>core</sbt.project.name>
</properties>
<packaging>jar</packaging>
<name>Spark Project Core</name>
<url>http://spark.apache.org/</url>

<properties>
<sbt.project.name>core</sbt.project.name>
<extra.source.dir>src/main/scala-${scala.binary.version}</extra.source.dir>
</properties>

<dependencies>
<dependency>
<groupId>com.thoughtworks.paranamer</groupId>
Expand Down Expand Up @@ -516,6 +519,24 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${extra.source.dir}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
33 changes: 33 additions & 0 deletions core/src/main/scala-2.12/org/apache/spark/util/OrderingUtil.scala
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)

}
34 changes: 34 additions & 0 deletions core/src/main/scala-2.13/org/apache/spark/util/OrderingUtil.scala
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.
Comment on lines +25 to +26

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member Author

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 superclass Ordering then defines operations like lt, lteq, etc in terms of compare. But 2.12 Ordering.Double overrides them to use operations like <, <=. As far as I can tell it presents a consistent total ordering via compare already (as it appears java.lang.Double.compare does), but its comparisons aren't consistent with how NaNs behave in lt, etc. Then again... perhaps neither is Java. Its Comparators.natural() would work consistently, but the comparisons don't match the Java operators.

Copy link
Copy Markdown
Member Author

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 use compare and 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 TotalOrdering changes that in 2.13.

  • If we think the current behavior is correct, and matters, then 2.12 is OK and then we use IeeeOrdering in 2.13 to be conservative
  • If the current behavior doesn't matter, it doesn't matter what we choose. TotalOrdering feels more logical.
  • If the current behavior is wrong, we can patch 2.12 to work like 2.13's TotalOrdering. Then the 2.13 choice is already correct, TotalOrdering.

I actually suspect it doesn't matter, doesn't get used.

*/
private[spark] object OrderingUtil {

def compareDouble(x: Double, y: Double): Int = Ordering.Double.TotalOrdering.compare(x, y)

@cloud-fan cloud-fan Dec 4, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

after another thought, shall we just use java.lang.Double.compare? This is how Scala 2.13 implements Ordering.Double.TotalOrdering:
https://github.com/scala/scala/blob/d0bd8241bb60bebc2bf0cbd2e9b01212fd1de93b/src/library/scala/math/Ordering.scala#L452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is also how Scala 2.12 implements Ordering.Double.compare:
https://github.com/scala/scala/blob/2.12.x/src/library/scala/math/Ordering.scala#L295

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 java.lang.Double.compare to replace Utils.nanSafeCompareDoubles

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes that sounds fine and simpler. If compare is the only functionality being used, this collapses to something simpler. I can make a follow-up. (We'll need the separate source trees for other reasons though.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit

import org.apache.spark.SparkFunSuite
import org.apache.spark.internal.Logging
import org.apache.spark.util.OrderingUtil
import org.apache.spark.util.Utils.timeIt
import org.apache.spark.util.random.XORShiftRandom

Expand Down Expand Up @@ -59,7 +60,7 @@ class SorterSuite extends SparkFunSuite with Logging {

Arrays.sort(keys)
new Sorter(new KVArraySortDataFormat[Double, Number])
.sort(keyValueArray, 0, keys.length, Ordering.Double)
.sort(keyValueArray, 0, keys.length, OrderingUtil.compareDouble)

keys.zipWithIndex.foreach { case (k, i) =>
assert(k === keyValueArray(2 * i))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.math.Numeric._
import scala.math.Ordering

import org.apache.spark.sql.types.Decimal.DecimalIsConflicted

import org.apache.spark.util.OrderingUtil

object ByteExactNumeric extends ByteIsIntegral with Ordering.ByteOrdering {
private def checkOverflow(res: Int, x: Byte, y: Byte, op: String): Unit = {
Expand Down Expand Up @@ -118,7 +118,7 @@ object LongExactNumeric extends LongIsIntegral with Ordering.LongOrdering {
}
}

object FloatExactNumeric extends FloatIsFractional with Ordering.FloatOrdering {
object FloatExactNumeric extends FloatIsFractional {
private def overflowException(x: Float, dataType: String) =
throw new ArithmeticException(s"Casting $x to $dataType causes overflow")

Expand Down Expand Up @@ -148,9 +148,11 @@ object FloatExactNumeric extends FloatIsFractional with Ordering.FloatOrdering {
overflowException(x, "int")
}
}

override def compare(x: Float, y: Float): Int = OrderingUtil.compareFloat(x, y)
}

object DoubleExactNumeric extends DoubleIsFractional with Ordering.DoubleOrdering {
object DoubleExactNumeric extends DoubleIsFractional {
private def overflowException(x: Double, dataType: String) =
throw new ArithmeticException(s"Casting $x to $dataType causes overflow")

Expand All @@ -174,6 +176,8 @@ object DoubleExactNumeric extends DoubleIsFractional with Ordering.DoubleOrderin
overflowException(x, "long")
}
}

override def compare(x: Double, y: Double): Int = OrderingUtil.compareDouble(x, y)
}

object DecimalExactNumeric extends DecimalIsConflicted {
Expand Down