-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-57575][SQL] Support TIME type in to_char/to_varchar formatting #56637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c833bad
09ecf81
fb57689
8617ef9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -35,7 +35,7 @@ import org.apache.spark.sql.catalyst.expressions.codegen.Block._ | |||||
| import org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke | ||||||
| import org.apache.spark.sql.catalyst.trees.CurrentOrigin.withOrigin | ||||||
| import org.apache.spark.sql.catalyst.trees.TreePattern._ | ||||||
| import org.apache.spark.sql.catalyst.util.{DateTimeUtils, LegacyDateFormats, TimestampFormatter} | ||||||
| import org.apache.spark.sql.catalyst.util.{DateTimeUtils, LegacyDateFormats, TimeFormatter, TimestampFormatter} | ||||||
| import org.apache.spark.sql.catalyst.util.DateTimeConstants._ | ||||||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils._ | ||||||
| import org.apache.spark.sql.catalyst.util.LegacyDateFormats.SIMPLE_DATE_FORMAT | ||||||
|
|
@@ -1139,34 +1139,76 @@ case class DateFormatClass(left: Expression, right: Expression, timeZoneId: Opti | |||||
| def this(left: Expression, right: Expression) = this(left, right, None) | ||||||
|
|
||||||
| override def inputTypes: Seq[AbstractDataType] = | ||||||
| Seq(TimestampType, StringTypeWithCollation(supportsTrimCollation = true)) | ||||||
| Seq(TypeCollection(TimestampType, AnyTimeType), | ||||||
| StringTypeWithCollation(supportsTrimCollation = true)) | ||||||
|
|
||||||
| override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression = | ||||||
| copy(timeZoneId = Option(timeZoneId)) | ||||||
|
|
||||||
| override protected def nullSafeEval(timestamp: Any, format: Any): Any = { | ||||||
| val formatter = formatterOption.getOrElse(getFormatter(format.toString)) | ||||||
| UTF8String.fromString(formatter.format(timestamp.asInstanceOf[Long])) | ||||||
| left.dataType match { | ||||||
| case _: TimeType => | ||||||
| val tf = timeFormatterOption.getOrElse( | ||||||
| TimeFormatter(format.toString, isParsing = false)) | ||||||
| DateFormatClass.formatTimeWithError( | ||||||
| tf, timestamp.asInstanceOf[Long], "to_char", format.toString) | ||||||
| case _ => | ||||||
| val formatter = formatterOption.getOrElse(getFormatter(format.toString)) | ||||||
| UTF8String.fromString(formatter.format(timestamp.asInstanceOf[Long])) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @transient private lazy val timeFormatterOption: Option[TimeFormatter] = | ||||||
| if (left.dataType.isInstanceOf[TimeType] && right.foldable) { | ||||||
| Option(right.eval()).map(fmt => TimeFormatter(fmt.toString, isParsing = false)) | ||||||
| } else None | ||||||
|
|
||||||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||||||
| formatterOption.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 ldf = LegacyDateFormats.getClass.getName.stripSuffix("$") | ||||||
| val zid = ctx.addReferenceObj("zoneId", zoneId, classOf[ZoneId].getName) | ||||||
| defineCodeGen(ctx, ev, (timestamp, format) => { | ||||||
| s"""|UTF8String.fromString($tf$$.MODULE$$.apply( | ||||||
| | $format.toString(), | ||||||
| | $zid, | ||||||
| | $ldf$$.MODULE$$.SIMPLE_DATE_FORMAT(), | ||||||
| | false) | ||||||
| |.format($timestamp))""".stripMargin | ||||||
| }) | ||||||
| left.dataType match { | ||||||
| case _: TimeType => | ||||||
| val errClass = QueryExecutionErrors.getClass.getName.stripSuffix("$") | ||||||
| timeFormatterOption.map { tf => | ||||||
| val timeFormatter = ctx.addReferenceObj("timeFormatter", tf) | ||||||
| val funcName = ctx.addReferenceObj("funcName", "to_char") | ||||||
| val fmtStr = ctx.addReferenceObj("fmtStr", right.eval().toString) | ||||||
| defineCodeGen(ctx, ev, (time, _) => { | ||||||
| s"""|((org.apache.spark.unsafe.types.UTF8String) | ||||||
| |org.apache.spark.sql.catalyst.expressions | ||||||
| |.DateFormatClass.formatTimeWithError( | ||||||
| |$timeFormatter, $time, | ||||||
| |$funcName, $fmtStr))""".stripMargin.replaceAll("\n", "") | ||||||
| }) | ||||||
| }.getOrElse { | ||||||
| val tf = TimeFormatter.getClass.getName.stripSuffix("$") | ||||||
| defineCodeGen(ctx, ev, (time, format) => { | ||||||
| s"""|((org.apache.spark.unsafe.types.UTF8String) | ||||||
| |org.apache.spark.sql.catalyst.expressions | ||||||
| |.DateFormatClass.formatTimeWithError( | ||||||
| |$tf$$.MODULE$$.apply( | ||||||
| |$format.toString(), false), | ||||||
| |$time, "to_char", | ||||||
| |$format.toString()))""".stripMargin.replaceAll("\n", "") | ||||||
| }) | ||||||
| } | ||||||
| case _ => | ||||||
| formatterOption.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 ldf = LegacyDateFormats.getClass.getName.stripSuffix("$") | ||||||
| val zid = ctx.addReferenceObj("zoneId", zoneId, classOf[ZoneId].getName) | ||||||
| defineCodeGen(ctx, ev, (timestamp, format) => { | ||||||
| s"""|UTF8String.fromString($tf$$.MODULE$$.apply( | ||||||
| | $format.toString(), | ||||||
| | $zid, | ||||||
| | $ldf$$.MODULE$$.SIMPLE_DATE_FORMAT(), | ||||||
| | false) | ||||||
| |.format($timestamp))""".stripMargin | ||||||
| }) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -1183,6 +1225,19 @@ case class DateFormatClass(left: Expression, right: Expression, timeZoneId: Opti | |||||
| final override def nodePatternsInternal(): Seq[TreePattern] = Seq(DATETIME) | ||||||
| } | ||||||
|
|
||||||
| object DateFormatClass { | ||||||
| /** Helper for codegen: formats time with proper Spark error on invalid pattern. */ | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This helper is called from both
Suggested change
|
||||||
| def formatTimeWithError( | ||||||
| tf: TimeFormatter, nanos: Long, funcName: String, pattern: String): UTF8String = { | ||||||
| try { | ||||||
| UTF8String.fromString(tf.format(nanos)) | ||||||
| } catch { | ||||||
| case e: java.time.DateTimeException => | ||||||
| throw QueryExecutionErrors.invalidPatternError(funcName, pattern, e) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following up on my own earlier suggestion to reuse this helper — having now seen the rendered message, it's misleading for these functions. Minimal fix is to pass |
||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Converts time string with given pattern. | ||||||
| * Deterministic version of [[UnixTimestamp]], must have at least one parameter. | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
errClassis never used — the error wrapping now lives insideformatTimeWithError, so this is leftover. Remove: