Repository: spark
Updated Branches:
  refs/heads/master af31335ad -> ddec45279


[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 #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.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/ddec4527
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/ddec4527
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/ddec4527

Branch: refs/heads/master
Commit: ddec45279ed1061f4c05fd0760309a53581d03f5
Parents: af31335
Author: Liang-Chi Hsieh <[email protected]>
Authored: Sat Jun 13 16:39:52 2015 -0700
Committer: Michael Armbrust <[email protected]>
Committed: Sat Jun 13 16:39:52 2015 -0700

----------------------------------------------------------------------
 .../scala/org/apache/spark/sql/catalyst/expressions/Cast.scala   | 3 ++-
 .../org/apache/spark/sql/hive/execution/SQLQuerySuite.scala      | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/ddec4527/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
index 05a04bd..b20086b 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
@@ -17,6 +17,7 @@
 
 package org.apache.spark.sql.catalyst.expressions
 
+import java.math.{BigDecimal => JavaBigDecimal}
 import java.sql.{Date, Timestamp}
 import java.text.{DateFormat, SimpleDateFormat}
 
@@ -320,7 +321,7 @@ case class Cast(child: Expression, dataType: DataType) 
extends UnaryExpression w
   private[this] def castToDecimal(from: DataType, target: DecimalType): Any => 
Any = from match {
     case StringType =>
       buildCast[UTF8String](_, s => try {
-        changePrecision(Decimal(s.toString.toDouble), target)
+        changePrecision(Decimal(new JavaBigDecimal(s.toString)), target)
       } catch {
         case _: NumberFormatException => null
       })

http://git-wip-us.apache.org/repos/asf/spark/blob/ddec4527/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
index c8e5e24..f890876 100644
--- 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
+++ 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
@@ -875,6 +875,10 @@ class SQLQuerySuite extends QueryTest {
     }
   }
 
+  test("Cast STRING to BIGINT") {
+    checkAnswer(sql("SELECT CAST('775983671874188101' as BIGINT)"), 
Row(775983671874188101L))
+  }
+
   // `Math.exp(1.0)` has different result for different jdk version, so not 
use createQueryTest
   test("udf_java_method") {
     checkAnswer(sql(


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to