Skip to content

Commit

Permalink
Fix precision of division (follow the rule in Hive)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davies Liu committed Aug 25, 2015
1 parent a2f4cdc commit 7f9b428
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,15 @@ object HiveTypeCoercion {
resultType)

case Divide(e1 @ DecimalType.Expression(p1, s1), e2 @ DecimalType.Expression(p2, s2)) =>
val resultType = DecimalType.bounded(p1 - s1 + s2 + max(6, s1 + p2 + 1),
max(6, s1 + p2 + 1))
var intDig = min(DecimalType.MAX_SCALE, p1 - s1 + s2)
var decDig = min(DecimalType.MAX_SCALE, max(6, s1 + p2 + 1))
val diff = (intDig + decDig) - DecimalType.MAX_SCALE
if (diff > 0) {
decDig -= diff / 2 + 1
intDig = DecimalType.MAX_SCALE - decDig
DecimalType.bounded(intDig + decDig, decDig)
}
val resultType = DecimalType.bounded(intDig + decDig, decDig)
val widerType = widerDecimalType(p1, s1, p2, s2)
CheckOverflow(Divide(promotePrecision(e1, widerType), promotePrecision(e2, widerType)),
resultType)
Expand Down Expand Up @@ -744,7 +751,7 @@ object HiveTypeCoercion {

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

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

// Implicit cast from/to string
case (StringType, DecimalType) => Cast(e, DecimalType.SYSTEM_DEFAULT)
case (StringType, _: DecimalType) => Cast(e, DecimalType.SYSTEM_DEFAULT)
case (StringType, target: NumericType) => Cast(e, target)
case (StringType, DateType) => Cast(e, DateType)
case (StringType, TimestampType) => Cast(e, TimestampType)
Expand Down
21 changes: 21 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,27 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
Row(null))
}

test("SPARK-10215 Div of Decimal returns null") {
val d = Decimal(1.12321)
val df = Seq((d, 1)).toDF("a", "b")

checkAnswer(
df.selectExpr("b * a / b"),
Seq(Row(d.toBigDecimal)))
checkAnswer(
df.selectExpr("b * a / b / b"),
Seq(Row(d.toBigDecimal)))
checkAnswer(
df.selectExpr("b * a + b"),
Seq(Row(BigDecimal(2.12321))))
checkAnswer(
df.selectExpr("b * a - b"),
Seq(Row(BigDecimal(0.12321))))
checkAnswer(
df.selectExpr("b * a * b"),
Seq(Row(d.toBigDecimal)))
}

test("external sorting updates peak execution memory") {
withSQLConf((SQLConf.EXTERNAL_SORT.key, "true")) {
val sc = sqlContext.sparkContext
Expand Down

0 comments on commit 7f9b428

Please sign in to comment.