Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -589,22 +589,43 @@ case class DateFormatClass(left: Expression, right: Expression, timeZoneId: Opti

override def inputTypes: Seq[AbstractDataType] = Seq(TimestampType, StringType)

override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression =
var formatter: Option[TimestampFormatter] = None

override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression = {
if (formatter.isEmpty && right.foldable) {
val format = right.eval().toString
formatter = Some(TimestampFormatter(
format,
DateTimeUtils.getZoneId(timeZoneId),
Locale.US))
}
copy(timeZoneId = Option(timeZoneId))
}

override protected def nullSafeEval(timestamp: Any, format: Any): Any = {
val df = TimestampFormatter(format.toString, zoneId)
UTF8String.fromString(df.format(timestamp.asInstanceOf[Long]))
val tf = if (formatter.isEmpty) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about .getOrElse?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.getOrElse has some overhead of calling the lambda function. I explicitly avoided its usage in the interpreted mode. For consistency, I could do the same in the codegen function but I don't think it does matter.

TimestampFormatter(format.toString, zoneId, Locale.US)
} else {
formatter.get
}
UTF8String.fromString(tf.format(timestamp.asInstanceOf[Long]))
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val tf = TimestampFormatter.getClass.getName.stripSuffix("$")
val zid = ctx.addReferenceObj("zoneId", zoneId, classOf[ZoneId].getName)
val locale = ctx.addReferenceObj("locale", Locale.US)
defineCodeGen(ctx, ev, (timestamp, format) => {
s"""UTF8String.fromString($tf$$.MODULE$$.apply($format.toString(), $zid, $locale)
formatter.map { tf =>
val timestampFormatter = ctx.addReferenceObj("timestampFormatter", tf)
defineCodeGen(ctx, ev, (timestamp, _) => {
s"""UTF8String.fromString($timestampFormatter.format($timestamp))"""
})
}.getOrElse {
val tf = TimestampFormatter.getClass.getName.stripSuffix("$")
val zid = ctx.addReferenceObj("zoneId", zoneId, classOf[ZoneId].getName)
val locale = ctx.addReferenceObj("locale", Locale.US)
defineCodeGen(ctx, ev, (timestamp, format) => {
s"""UTF8String.fromString($tf$$.MODULE$$.apply($format.toString(), $zid, $locale)
.format($timestamp))"""
})
})
}
}

override def prettyName: String = "date_format"
Expand Down