Skip to content

Commit 7f9b428

Browse files
author
Davies Liu
committed
Fix precision of division (follow the rule in Hive)
1 parent a2f4cdc commit 7f9b428

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,15 @@ object HiveTypeCoercion {
400400
resultType)
401401

402402
case Divide(e1 @ DecimalType.Expression(p1, s1), e2 @ DecimalType.Expression(p2, s2)) =>
403-
val resultType = DecimalType.bounded(p1 - s1 + s2 + max(6, s1 + p2 + 1),
404-
max(6, s1 + p2 + 1))
403+
var intDig = min(DecimalType.MAX_SCALE, p1 - s1 + s2)
404+
var decDig = min(DecimalType.MAX_SCALE, max(6, s1 + p2 + 1))
405+
val diff = (intDig + decDig) - DecimalType.MAX_SCALE
406+
if (diff > 0) {
407+
decDig -= diff / 2 + 1
408+
intDig = DecimalType.MAX_SCALE - decDig
409+
DecimalType.bounded(intDig + decDig, decDig)
410+
}
411+
val resultType = DecimalType.bounded(intDig + decDig, decDig)
405412
val widerType = widerDecimalType(p1, s1, p2, s2)
406413
CheckOverflow(Divide(promotePrecision(e1, widerType), promotePrecision(e2, widerType)),
407414
resultType)
@@ -744,7 +751,7 @@ object HiveTypeCoercion {
744751

745752
// If input is a numeric type but not decimal, and we expect a decimal type,
746753
// cast the input to decimal.
747-
case (d: NumericType, DecimalType) => Cast(e, DecimalType.forType(d))
754+
case (d: NumericType, _: DecimalType) => Cast(e, DecimalType.forType(d))
748755
// For any other numeric types, implicitly cast to each other, e.g. long -> int, int -> long
749756
case (_: NumericType, target: NumericType) => Cast(e, target)
750757

@@ -753,7 +760,7 @@ object HiveTypeCoercion {
753760
case (TimestampType, DateType) => Cast(e, DateType)
754761

755762
// Implicit cast from/to string
756-
case (StringType, DecimalType) => Cast(e, DecimalType.SYSTEM_DEFAULT)
763+
case (StringType, _: DecimalType) => Cast(e, DecimalType.SYSTEM_DEFAULT)
757764
case (StringType, target: NumericType) => Cast(e, target)
758765
case (StringType, DateType) => Cast(e, DateType)
759766
case (StringType, TimestampType) => Cast(e, TimestampType)

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,27 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
16271627
Row(null))
16281628
}
16291629

1630+
test("SPARK-10215 Div of Decimal returns null") {
1631+
val d = Decimal(1.12321)
1632+
val df = Seq((d, 1)).toDF("a", "b")
1633+
1634+
checkAnswer(
1635+
df.selectExpr("b * a / b"),
1636+
Seq(Row(d.toBigDecimal)))
1637+
checkAnswer(
1638+
df.selectExpr("b * a / b / b"),
1639+
Seq(Row(d.toBigDecimal)))
1640+
checkAnswer(
1641+
df.selectExpr("b * a + b"),
1642+
Seq(Row(BigDecimal(2.12321))))
1643+
checkAnswer(
1644+
df.selectExpr("b * a - b"),
1645+
Seq(Row(BigDecimal(0.12321))))
1646+
checkAnswer(
1647+
df.selectExpr("b * a * b"),
1648+
Seq(Row(d.toBigDecimal)))
1649+
}
1650+
16301651
test("external sorting updates peak execution memory") {
16311652
withSQLConf((SQLConf.EXTERNAL_SORT.key, "true")) {
16321653
val sc = sqlContext.sparkContext

0 commit comments

Comments
 (0)