[ 
https://issues.apache.org/jira/browse/SPARK-8892?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14633032#comment-14633032
 ] 

Yijie Shen commented on SPARK-8892:
-----------------------------------

The bug was caused by precision lose introduced when converting from Long to 
Double:
{code:title=branch-1.4: Cast.scala#castToDecimal}
private[this] def castToDecimal(from: DataType, target: DecimalType): Any => 
Any = from match {
    case StringType =>
      buildCast[UTF8String](_, s => try {
        changePrecision(Decimal(s.toString.toDouble), target)
      } catch {
        case _: NumberFormatException => null
      })
{code}
which was fixed by [SPARK-8052|https://issues.apache.org/jira/browse/SPARK-8052]
{code:title=master: Cast.scala#castToDecimal}
private[this] def castToDecimal(from: DataType, target: DecimalType): Any => 
Any = from match {
    case StringType =>
      buildCast[UTF8String](_, s => try {
        changePrecision(Decimal(new JavaBigDecimal(s.toString)), target)
      } catch {
        case _: NumberFormatException => null
      })
{code}

Therefore, apply [SPARK-8052|https://issues.apache.org/jira/browse/SPARK-8052] 
also on branch-1.4 will resolve this bug.

> Column.cast(LongType) does not work for large values
> ----------------------------------------------------
>
>                 Key: SPARK-8892
>                 URL: https://issues.apache.org/jira/browse/SPARK-8892
>             Project: Spark
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 1.4.0
>            Reporter: Jason Moore
>
> It seems that casting a column from String to Long seems to go through an 
> intermediate step of being cast to a Double (hits Cast.scala line 328 in 
> castToDecimal).  The result is that for large values, the wrong value is 
> returned.
> This test reveals this bug:
> {code:java}
> import org.apache.spark.sql.types._
> import org.apache.spark.sql.{Row, SQLContext}
> import org.apache.spark.{SparkConf, SparkContext}
> import org.scalatest.FlatSpec
> import scala.util.Random
> class DataFrameCastBug extends FlatSpec {
>   "DataFrame" should "cast StringType to LongType correctly" in {
>     val sc = new SparkContext(new 
> SparkConf().setMaster("local").setAppName("app"))
>     val qc = new SQLContext(sc)
>     val values = Seq.fill(100000)(Random.nextLong)
>     val source = qc.createDataFrame(
>       sc.parallelize(values.map(v => Row(v))),
>       StructType(Seq(StructField("value", LongType))))
>     val result = source.select(source("value"), 
> source("value").cast(StringType).cast(LongType).as("castValue"))
>     assert(result.where(result("value") !== result("castValue")).count() === 
> 0)
>   }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

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

Reply via email to