Skip to content

Commit c8ed71b

Browse files
srowendongjoon-hyun
authored andcommitted
[SPARK-30011][SQL] Inline 2.12 "AsIfIntegral" classes, not present in 2.13
### What changes were proposed in this pull request? Classes like DoubleAsIfIntegral are not found in Scala 2.13, but used in the current build. This change 'inlines' the 2.12 implementation and makes it work with both 2.12 and 2.13. ### Why are the changes needed? To cross-compile with 2.13. ### Does this PR introduce any user-facing change? It should not as it copies in 2.12's existing behavior. ### How was this patch tested? Existing tests. Closes apache#26769 from srowen/SPARK-30011. Authored-by: Sean Owen <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 1595e46 commit c8ed71b

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ package org.apache.spark.sql.types
2020
import java.lang.{Long => JLong}
2121
import java.math.{BigInteger, MathContext, RoundingMode}
2222

23+
import scala.util.Try
24+
2325
import org.apache.spark.annotation.Unstable
24-
import org.apache.spark.sql.AnalysisException
2526

2627
/**
2728
* A mutable implementation of BigDecimal that can hold a Long if values are small enough.
@@ -622,6 +623,9 @@ object Decimal {
622623
override def toLong(x: Decimal): Long = x.toLong
623624
override def fromInt(x: Int): Decimal = new Decimal().set(x)
624625
override def compare(x: Decimal, y: Decimal): Int = x.compare(y)
626+
// Added from Scala 2.13; don't override to work in 2.12
627+
// TODO revisit once Scala 2.12 support is dropped
628+
def parseString(str: String): Option[Decimal] = Try(Decimal(str)).toOption
625629
}
626630

627631
/** A [[scala.math.Fractional]] evidence parameter for Decimals. */

sql/catalyst/src/main/scala/org/apache/spark/sql/types/DoubleType.scala

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
package org.apache.spark.sql.types
1919

2020
import scala.math.{Fractional, Numeric}
21-
import scala.math.Numeric.DoubleAsIfIntegral
2221
import scala.reflect.runtime.universe.typeTag
22+
import scala.util.Try
2323

2424
import org.apache.spark.annotation.Stable
25-
import org.apache.spark.util.Utils
2625

2726
/**
2827
* The data type representing `Double` values. Please use the singleton `DataTypes.DoubleType`.
@@ -40,7 +39,7 @@ class DoubleType private() extends FractionalType {
4039
private[sql] val fractional = implicitly[Fractional[Double]]
4140
private[sql] val ordering =
4241
(x: Double, y: Double) => java.lang.Double.compare(x, y)
43-
private[sql] val asIntegral = DoubleAsIfIntegral
42+
private[sql] val asIntegral = DoubleType.DoubleAsIfIntegral
4443

4544
override private[sql] def exactNumeric = DoubleExactNumeric
4645

@@ -56,4 +55,34 @@ class DoubleType private() extends FractionalType {
5655
* @since 1.3.0
5756
*/
5857
@Stable
59-
case object DoubleType extends DoubleType
58+
case object DoubleType extends DoubleType {
59+
60+
// Traits below copied from Scala 2.12; not present in 2.13
61+
// TODO: SPARK-30011 revisit once Scala 2.12 support is dropped
62+
trait DoubleIsConflicted extends Numeric[Double] {
63+
def plus(x: Double, y: Double): Double = x + y
64+
def minus(x: Double, y: Double): Double = x - y
65+
def times(x: Double, y: Double): Double = x * y
66+
def negate(x: Double): Double = -x
67+
def fromInt(x: Int): Double = x.toDouble
68+
def toInt(x: Double): Int = x.toInt
69+
def toLong(x: Double): Long = x.toLong
70+
def toFloat(x: Double): Float = x.toFloat
71+
def toDouble(x: Double): Double = x
72+
// logic in Numeric base trait mishandles abs(-0.0)
73+
override def abs(x: Double): Double = math.abs(x)
74+
// Added from Scala 2.13; don't override to work in 2.12
75+
def parseString(str: String): Option[Double] =
76+
Try(java.lang.Double.parseDouble(str)).toOption
77+
78+
}
79+
80+
trait DoubleAsIfIntegral extends DoubleIsConflicted with Integral[Double] {
81+
def quot(x: Double, y: Double): Double = (BigDecimal(x) quot BigDecimal(y)).doubleValue
82+
def rem(x: Double, y: Double): Double = (BigDecimal(x) remainder BigDecimal(y)).doubleValue
83+
}
84+
85+
object DoubleAsIfIntegral extends DoubleAsIfIntegral {
86+
override def compare(x: Double, y: Double): Int = java.lang.Double.compare(x, y)
87+
}
88+
}

sql/catalyst/src/main/scala/org/apache/spark/sql/types/FloatType.scala

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
package org.apache.spark.sql.types
1919

2020
import scala.math.{Fractional, Numeric}
21-
import scala.math.Numeric.FloatAsIfIntegral
2221
import scala.reflect.runtime.universe.typeTag
22+
import scala.util.Try
2323

2424
import org.apache.spark.annotation.Stable
25-
import org.apache.spark.util.Utils
2625

2726
/**
2827
* The data type representing `Float` values. Please use the singleton `DataTypes.FloatType`.
@@ -40,7 +39,7 @@ class FloatType private() extends FractionalType {
4039
private[sql] val fractional = implicitly[Fractional[Float]]
4140
private[sql] val ordering =
4241
(x: Float, y: Float) => java.lang.Float.compare(x, y)
43-
private[sql] val asIntegral = FloatAsIfIntegral
42+
private[sql] val asIntegral = FloatType.FloatAsIfIntegral
4443

4544
override private[sql] def exactNumeric = FloatExactNumeric
4645

@@ -57,4 +56,33 @@ class FloatType private() extends FractionalType {
5756
* @since 1.3.0
5857
*/
5958
@Stable
60-
case object FloatType extends FloatType
59+
case object FloatType extends FloatType {
60+
61+
// Traits below copied from Scala 2.12; not present in 2.13
62+
// TODO: SPARK-30011 revisit once Scala 2.12 support is dropped
63+
trait FloatIsConflicted extends Numeric[Float] {
64+
def plus(x: Float, y: Float): Float = x + y
65+
def minus(x: Float, y: Float): Float = x - y
66+
def times(x: Float, y: Float): Float = x * y
67+
def negate(x: Float): Float = -x
68+
def fromInt(x: Int): Float = x.toFloat
69+
def toInt(x: Float): Int = x.toInt
70+
def toLong(x: Float): Long = x.toLong
71+
def toFloat(x: Float): Float = x
72+
def toDouble(x: Float): Double = x.toDouble
73+
// logic in Numeric base trait mishandles abs(-0.0f)
74+
override def abs(x: Float): Float = math.abs(x)
75+
// Added from Scala 2.13; don't override to work in 2.12
76+
def parseString(str: String): Option[Float] =
77+
Try(java.lang.Float.parseFloat(str)).toOption
78+
}
79+
80+
trait FloatAsIfIntegral extends FloatIsConflicted with Integral[Float] {
81+
def quot(x: Float, y: Float): Float = (BigDecimal(x) quot BigDecimal(y)).floatValue
82+
def rem(x: Float, y: Float): Float = (BigDecimal(x) remainder BigDecimal(y)).floatValue
83+
}
84+
85+
object FloatAsIfIntegral extends FloatAsIfIntegral {
86+
override def compare(x: Float, y: Float): Int = java.lang.Float.compare(x, y)
87+
}
88+
}

0 commit comments

Comments
 (0)