Github user adrian-wang commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7644#discussion_r35842378
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala
 ---
    @@ -254,19 +255,231 @@ 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.util.Date($timestamp / 1000)))"""
         })
       }
     }
     
     /**
    + * Converts time string with given pattern
    + * (see 
[http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html])
    + * to Unix time stamp (in seconds), returns null if fail.
    + * Note that hive Language Manual says it returns 0 if fail, but in fact 
it returns null.
    + * 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(timeExp: Expression, format: Expression)
    +  extends BinaryExpression with ExpectsInputTypes {
    +
    +  override def left: Expression = timeExp
    +  override def right: Expression = format
    +
    +  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
    +
    +  lazy val constFormat: UTF8String = right.eval().asInstanceOf[UTF8String]
    +
    +  override def eval(input: InternalRow): Any = {
    +    val t = left.eval(input)
    +    if (t == null) {
    +      null
    +    } else {
    +      left.dataType match {
    +        case DateType =>
    +          DateTimeUtils.daysToMillis(t.asInstanceOf[Int]) / 1000L
    +        case TimestampType =>
    +          t.asInstanceOf[Long] / 1000000L
    +        case StringType if right.foldable =>
    +          if (constFormat != null) {
    +            Try(new SimpleDateFormat(constFormat.toString).parse(
    --- End diff --
    
    There are many expressions rely on `SimpleDateFormat`, including some 
merged expressions such as `DateFormatClass`. Let's do this later, maybe we can 
extract a common trait for this kind of expressions.


---
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.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to