Skip to content

Commit ddec452

Browse files
viiryamarmbrus
authored andcommitted
[SPARK-8052] [SQL] Use java.math.BigDecimal for casting String to Decimal instead of using toDouble
JIRA: https://issues.apache.org/jira/browse/SPARK-8052 Author: Liang-Chi Hsieh <[email protected]> Closes apache#6645 from viirya/cast_string_integraltype and squashes the following commits: e19c6a3 [Liang-Chi Hsieh] For comment. c3e472a [Liang-Chi Hsieh] Add test. 7ced9b0 [Liang-Chi Hsieh] Use java.math.BigDecimal for casting String to Decimal instead of using toDouble.
1 parent af31335 commit ddec452

File tree

2 files changed

+6
-1
lines changed
  • sql
    • catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions
    • hive/src/test/scala/org/apache/spark/sql/hive/execution

2 files changed

+6
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.spark.sql.catalyst.expressions
1919

20+
import java.math.{BigDecimal => JavaBigDecimal}
2021
import java.sql.{Date, Timestamp}
2122
import java.text.{DateFormat, SimpleDateFormat}
2223

@@ -320,7 +321,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
320321
private[this] def castToDecimal(from: DataType, target: DecimalType): Any => Any = from match {
321322
case StringType =>
322323
buildCast[UTF8String](_, s => try {
323-
changePrecision(Decimal(s.toString.toDouble), target)
324+
changePrecision(Decimal(new JavaBigDecimal(s.toString)), target)
324325
} catch {
325326
case _: NumberFormatException => null
326327
})

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala

+4
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,10 @@ class SQLQuerySuite extends QueryTest {
875875
}
876876
}
877877

878+
test("Cast STRING to BIGINT") {
879+
checkAnswer(sql("SELECT CAST('775983671874188101' as BIGINT)"), Row(775983671874188101L))
880+
}
881+
878882
// `Math.exp(1.0)` has different result for different jdk version, so not use createQueryTest
879883
test("udf_java_method") {
880884
checkAnswer(sql(

0 commit comments

Comments
 (0)