Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1143#discussion_r14007430
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
 ---
    @@ -104,85 +121,118 @@ case class Cast(child: Expression, dataType: 
DataType) extends UnaryExpression {
       }
     
       // Timestamp to long, converting milliseconds to seconds
    -  private def timestampToLong(ts: Timestamp) = ts.getTime / 1000
    +  private[this] def timestampToLong(ts: Timestamp) = ts.getTime / 1000
     
    -  private def timestampToDouble(ts: Timestamp) = {
    +  private[this] def timestampToDouble(ts: Timestamp) = {
         // First part is the seconds since the beginning of time, followed by 
nanosecs.
         ts.getTime / 1000 + ts.getNanos.toDouble / 1000000000
       }
     
    -  def castToLong: Any => Any = child.dataType match {
    -    case StringType => nullOrCast[String](_, s => try s.toLong catch {
    -      case _: NumberFormatException => null
    -    })
    -    case BooleanType => nullOrCast[Boolean](_, b => if(b) 1L else 0L)
    -    case TimestampType => nullOrCast[Timestamp](_, t => timestampToLong(t))
    -    case DecimalType => nullOrCast[BigDecimal](_, _.toLong)
    -    case x: NumericType => b => 
x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
    -  }
    -
    -  def castToInt: Any => Any = child.dataType match {
    -    case StringType => nullOrCast[String](_, s => try s.toInt catch {
    -      case _: NumberFormatException => null
    -    })
    -    case BooleanType => nullOrCast[Boolean](_, b => if(b) 1 else 0)
    -    case TimestampType => nullOrCast[Timestamp](_, t => 
timestampToLong(t).toInt)
    -    case DecimalType => nullOrCast[BigDecimal](_, _.toInt)
    -    case x: NumericType => b => 
x.numeric.asInstanceOf[Numeric[Any]].toInt(b)
    -  }
    -
    -  def castToShort: Any => Any = child.dataType match {
    -    case StringType => nullOrCast[String](_, s => try s.toShort catch {
    -      case _: NumberFormatException => null
    -    })
    -    case BooleanType => nullOrCast[Boolean](_, b => if(b) 1.toShort else 
0.toShort)
    -    case TimestampType => nullOrCast[Timestamp](_, t => 
timestampToLong(t).toShort)
    -    case DecimalType => nullOrCast[BigDecimal](_, _.toShort)
    -    case x: NumericType => b => 
x.numeric.asInstanceOf[Numeric[Any]].toInt(b).toShort
    -  }
    -
    -  def castToByte: Any => Any = child.dataType match {
    -    case StringType => nullOrCast[String](_, s => try s.toByte catch {
    -      case _: NumberFormatException => null
    -    })
    -    case BooleanType => nullOrCast[Boolean](_, b => if(b) 1.toByte else 
0.toByte)
    -    case TimestampType => nullOrCast[Timestamp](_, t => 
timestampToLong(t).toByte)
    -    case DecimalType => nullOrCast[BigDecimal](_, _.toByte)
    -    case x: NumericType => b => 
x.numeric.asInstanceOf[Numeric[Any]].toInt(b).toByte
    -  }
    -
    -  def castToDecimal: Any => Any = child.dataType match {
    -    case StringType => nullOrCast[String](_, s => try 
BigDecimal(s.toDouble) catch {
    -      case _: NumberFormatException => null
    -    })
    -    case BooleanType => nullOrCast[Boolean](_, b => if(b) BigDecimal(1) 
else BigDecimal(0))
    +  private[this] def castToLong: Any => Any = child.dataType match {
    +    case StringType =>
    +      buildCast[String](_, s => try s.toLong catch {
    +        case _: NumberFormatException => null
    +      })
    --- End diff --
    
    Maybe we can simplify this to:
    
    ```scala
    Try(s.toLong).getOrElse(null)
    ```
    
    (`s.toLong` throws `NumerFormatException` only.)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to