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

    https://github.com/apache/spark/pull/7644#discussion_r35671995
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala
 ---
    @@ -254,19 +255,115 @@ case class DateFormatClass(left: Expression, right: 
Expression) extends BinaryEx
     
       override protected def nullSafeEval(timestamp: Any, format: Any): Any = {
         val sdf = new SimpleDateFormat(format.toString)
    -    UTF8String.fromString(sdf.format(new Date(timestamp.asInstanceOf[Long] 
/ 1000)))
    +    UTF8String.fromString(sdf.format(new 
java.util.Date(timestamp.asInstanceOf[Long] / 1000)))
       }
     
       override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): 
String = {
         val sdf = classOf[SimpleDateFormat].getName
         defineCodeGen(ctx, ev, (timestamp, format) => {
           s"""UTF8String.fromString((new $sdf($format.toString()))
    -          .format(new java.sql.Date($timestamp / 1000)))"""
    +          .format(new java.sql.Timestamp($timestamp / 1000)))"""
         })
       }
     }
     
     /**
    + * Convert time string with given pattern
    + * (see 
[http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html])
    + * to Unix time stamp (in seconds), return null if fail.
    + * If the second parameter is missing, use "yyyy-MM-dd HH:mm:ss".
    + * If no parameters provided, the first parameter will be 
current_timestamp.
    + * If the first parameter is a Date or Timestamp instead of String, we 
will ignore the
    + * second parameter.
    + */
    +case class UnixTimestamp(left: Expression, right: Expression)
    +  extends BinaryExpression with ExpectsInputTypes {
    +
    +  def this(time: Expression) = {
    +    this(time, Literal("yyyy-MM-dd HH:mm:ss"))
    +  }
    +
    +  def this() = {
    +    this(CurrentTimestamp())
    +  }
    +
    +  override def inputTypes: Seq[AbstractDataType] =
    +    Seq(TypeCollection(StringType, DateType, TimestampType), StringType)
    +
    +  override def dataType: DataType = LongType
    +
    +  override def nullSafeEval(time: Any, format: Any): Any = {
    +    left.dataType match {
    +      case DateType =>
    +        DateTimeUtils.daysToMillis(time.asInstanceOf[Int]) / 1000L
    +      case TimestampType =>
    +        time.asInstanceOf[Long] / 1000000L
    +      case StringType =>
    +        val formatString = format.asInstanceOf[UTF8String].toString
    --- End diff --
    
    This could be cached.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to