Repository: spark
Updated Branches:
  refs/heads/branch-1.4 a3c853c3f -> 180ae25d9


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

(cherry picked from commit ddec45279ed1061f4c05fd0760309a53581d03f5)
Signed-off-by: Reynold Xin <[email protected]>


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

Branch: refs/heads/branch-1.4
Commit: 180ae25d90fb1a7b235f646cdc8180deede32608
Parents: a3c853c
Author: Liang-Chi Hsieh <[email protected]>
Authored: Sat Jun 13 16:39:52 2015 -0700
Committer: Reynold Xin <[email protected]>
Committed: Sun Jul 19 20:59:56 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/180ae25d/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 a878559..42e57b6 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}
 
@@ -325,7 +326,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/180ae25d/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 3fcb39d..e302f8b 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
@@ -851,6 +851,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