Github user adrian-wang commented on a diff in the pull request:
https://github.com/apache/spark/pull/7644#discussion_r35756028
--- 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
+ val sdf = new SimpleDateFormat(formatString)
+ Try(sdf.parse(time.asInstanceOf[UTF8String].toString).getTime /
1000L).getOrElse(null)
+ }
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ left.dataType match {
+ case StringType =>
+ val sdf = classOf[SimpleDateFormat].getName
+ nullSafeCodeGen(ctx, ev, (string, format) => {
+ s"""
+ try {
+ ${ev.primitive} =
+ (new
$sdf($format.toString())).parse($string.toString()).getTime() / 1000L;
+ } catch (java.lang.Throwable e) {
+ ${ev.isNull} = true;
--- End diff --
done.
---
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]