Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 2 additions & 2 deletions sql/core/benchmarks/DateTimeBenchmark-results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ Java HotSpot(TM) 64-Bit Server VM 1.8.0_202-b08 on Mac OS X 10.14.3
Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
format date: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
------------------------------------------------------------------------------------------------
format date wholestage off 7180 / 7181 1.4 718.0 1.0X
format date wholestage on 7051 / 7194 1.4 705.1 1.0X
format date wholestage off 6642 / 6666 1.4 664.2 1.0X
format date wholestage on 6556 / 6565 1.5 655.6 1.0X


================================================================================================
Expand Down