Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -580,7 +580,11 @@ case class WeekOfYear(child: Expression) extends UnaryExpression with ImplicitCa
""",
since = "1.5.0")
// scalastyle:on line.size.limit
case class DateFormatClass(left: Expression, right: Expression, timeZoneId: Option[String] = None)
case class DateFormatClass(
left: Expression,
right: Expression,
timeZoneId: Option[String] = None,
formatter: Option[TimestampFormatter] = None)
extends BinaryExpression with TimeZoneAwareExpression with ImplicitCastInputTypes {
Comment thread
HyukjinKwon marked this conversation as resolved.

def this(left: Expression, right: Expression) = this(left, right, None)
Expand All @@ -589,22 +593,41 @@ case class DateFormatClass(left: Expression, right: Expression, timeZoneId: Opti

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

override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression =
copy(timeZoneId = Option(timeZoneId))
override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression = {
val tf = if (formatter.isEmpty && right.foldable) {
val format = right.eval().toString
Comment thread
HyukjinKwon marked this conversation as resolved.
Outdated
Some(TimestampFormatter(
format,
DateTimeUtils.getZoneId(timeZoneId),
Locale.US))
Comment thread
MaxGekk marked this conversation as resolved.
Outdated
} else None
copy(formatter = tf, 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
Copy Markdown
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
Copy Markdown
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)
Comment thread
MaxGekk marked this conversation as resolved.
Outdated
} 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)
Comment thread
MaxGekk marked this conversation as resolved.
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 4787 / 4839 2.1 478.7 1.0X
format date wholestage on 4736 / 4802 2.1 473.6 1.0X
Comment thread
MaxGekk marked this conversation as resolved.


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