[SPARK-2209][SQL] Cast shouldn't do null check twice.

Also took the chance to clean up cast a little bit. Too many arrows on each line before!

According to the [API documentation](http://www.scala-lang.org/api/current/index.html#scala.math.BigDecimal@longValue():Long), `d.longValue()` and `d.intValue()` may be negative:

```scala
scala> var d: BigDecimal = 1000000000
d: BigDecimal = 1000000000

scala> d = d * d * d * d
d: BigDecimal = 1.000000000000000000000000000000000E+36

scala> d.longValue
res0: Long = -5527149226598858752
```