title: Spark Scala: Cannot up cast from string to int as it may truncate
body: <p>I got this exception while playing with spark.</p>
  
  <blockquote>
    <p>Exception in thread "main" org.apache.spark.sql.AnalysisException:
    Cannot up cast <code>price</code> from string to int as it may truncate
        The type path of the target object is:
        - field (class: "scala.Int", name: "price")
        - root class: "org.spark.code.executable.Main.Record"
        You can either add an explicit cast to the input data or choose a higher precision type of the field in the target object;</p>
  </blockquote>
  
  <p>How Can this exception be solved?  Here is the code</p>
  
  <pre><code>object Main {
  
   case class Record(transactionDate: Timestamp, product: String, price: Int, paymentType: String, name: String, city: String, state: String, country: String,
                  accountCreated: Timestamp, lastLogin: Timestamp, latitude: String, longitude: String)
   def main(args: Array[String]) {
  
     System.setProperty("hadoop.home.dir", "C:\\winutils\\");
  
     val schema = Encoders.product[Record].schema
  
     val df = SparkConfig.sparkSession.read
    .option("header", "true")
    .csv("SalesJan2009.csv");
  
     import SparkConfig.sparkSession.implicits._
     val ds = df.as[Record]
  
    //ds.groupByKey(body =&gt; body.state).count().show()
  
    import org.apache.spark.sql.expressions.scalalang.typed.{
    count =&gt; typedCount,
    sum =&gt; typedSum
  }
  
    ds.groupByKey(body =&gt; body.state)
    .agg(typedSum[Record](_.price).name("sum(price)"))
    .withColumnRenamed("value", "group")
    .alias("Summary by state")
    .show()
  }
  </code></pre>