MaxGekk commented on a change in pull request #25782: [SPARK-29074][SQL]
Optimize `date_format` for foldable `fmt`
URL: https://github.com/apache/spark/pull/25782#discussion_r324412452
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
##########
@@ -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
+ Some(TimestampFormatter(
+ format,
+ DateTimeUtils.getZoneId(timeZoneId),
+ Locale.US))
+ } 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) {
+ 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)
Review comment:
Yes, I put breakpoints in IntelliJ IDEA to both part, and checked that they
are covered by `DateFunctionsSuite` and `DateExpressionsSuite`.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]