Skip to content

Commit b13815e

Browse files
committed
Merge pull request #141 from mbautin/csd-1.5_half_even
Use half-even rounding
2 parents 058e935 + 63f6630 commit b13815e

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ case class Logarithm(left: Expression, right: Expression)
620620
case class Round(child: Expression, scale: Expression)
621621
extends BinaryExpression with ImplicitCastInputTypes {
622622

623-
import BigDecimal.RoundingMode.HALF_UP
623+
import BigDecimal.RoundingMode.HALF_EVEN
624624

625625
def this(child: Expression) = this(child, Literal(0))
626626

@@ -679,26 +679,26 @@ case class Round(child: Expression, scale: Expression)
679679
val decimal = input1.asInstanceOf[Decimal]
680680
if (decimal.changePrecision(decimal.precision, _scale)) decimal else null
681681
case ByteType =>
682-
BigDecimal(input1.asInstanceOf[Byte]).setScale(_scale, HALF_UP).toByte
682+
BigDecimal(input1.asInstanceOf[Byte]).setScale(_scale, HALF_EVEN).toByte
683683
case ShortType =>
684-
BigDecimal(input1.asInstanceOf[Short]).setScale(_scale, HALF_UP).toShort
684+
BigDecimal(input1.asInstanceOf[Short]).setScale(_scale, HALF_EVEN).toShort
685685
case IntegerType =>
686-
BigDecimal(input1.asInstanceOf[Int]).setScale(_scale, HALF_UP).toInt
686+
BigDecimal(input1.asInstanceOf[Int]).setScale(_scale, HALF_EVEN).toInt
687687
case LongType =>
688-
BigDecimal(input1.asInstanceOf[Long]).setScale(_scale, HALF_UP).toLong
688+
BigDecimal(input1.asInstanceOf[Long]).setScale(_scale, HALF_EVEN).toLong
689689
case FloatType =>
690690
val f = input1.asInstanceOf[Float]
691691
if (f.isNaN || f.isInfinite) {
692692
f
693693
} else {
694-
BigDecimal(f).setScale(_scale, HALF_UP).toFloat
694+
BigDecimal(f).setScale(_scale, HALF_EVEN).toFloat
695695
}
696696
case DoubleType =>
697697
val d = input1.asInstanceOf[Double]
698698
if (d.isNaN || d.isInfinite) {
699699
d
700700
} else {
701-
BigDecimal(d).setScale(_scale, HALF_UP).toDouble
701+
BigDecimal(d).setScale(_scale, HALF_EVEN).toDouble
702702
}
703703
}
704704
}
@@ -718,31 +718,31 @@ case class Round(child: Expression, scale: Expression)
718718
if (_scale < 0) {
719719
s"""
720720
${ev.primitive} = new java.math.BigDecimal(${ce.primitive}).
721-
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_UP).byteValue();"""
721+
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_EVEN).byteValue();"""
722722
} else {
723723
s"${ev.primitive} = ${ce.primitive};"
724724
}
725725
case ShortType =>
726726
if (_scale < 0) {
727727
s"""
728728
${ev.primitive} = new java.math.BigDecimal(${ce.primitive}).
729-
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_UP).shortValue();"""
729+
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_EVEN).shortValue();"""
730730
} else {
731731
s"${ev.primitive} = ${ce.primitive};"
732732
}
733733
case IntegerType =>
734734
if (_scale < 0) {
735735
s"""
736736
${ev.primitive} = new java.math.BigDecimal(${ce.primitive}).
737-
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_UP).intValue();"""
737+
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_EVEN).intValue();"""
738738
} else {
739739
s"${ev.primitive} = ${ce.primitive};"
740740
}
741741
case LongType =>
742742
if (_scale < 0) {
743743
s"""
744744
${ev.primitive} = new java.math.BigDecimal(${ce.primitive}).
745-
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_UP).longValue();"""
745+
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_EVEN).longValue();"""
746746
} else {
747747
s"${ev.primitive} = ${ce.primitive};"
748748
}
@@ -760,7 +760,7 @@ case class Round(child: Expression, scale: Expression)
760760
${ev.primitive} = ${ce.primitive};
761761
} else {
762762
${ev.primitive} = java.math.BigDecimal.valueOf(${ce.primitive}).
763-
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_UP).floatValue();
763+
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_EVEN).floatValue();
764764
}"""
765765
}
766766
case DoubleType => // if child eval to NaN or Infinity, just return it.
@@ -777,7 +777,7 @@ case class Round(child: Expression, scale: Expression)
777777
${ev.primitive} = ${ce.primitive};
778778
} else {
779779
${ev.primitive} = java.math.BigDecimal.valueOf(${ce.primitive}).
780-
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
780+
setScale(${_scale}, java.math.BigDecimal.ROUND_HALF_EVEN).doubleValue();
781781
}"""
782782
}
783783
}

0 commit comments

Comments
 (0)