You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "vinodkc (via GitHub)" <gi...@apache.org> on 2026/07/01 04:49:56 UTC
Re: [PR] [SPARK-54588][SQL] Add time_format function to convert TIME values to formatted string representations [spark]
vinodkc commented on code in PR #53320:
URL: https://github.com/apache/spark/pull/53320#discussion_r3503352091
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/timeExpressions.scala:
##########
@@ -1036,3 +1036,82 @@ case class TimeToMicros(child: Expression)
override protected def withNewChildInternal(newChild: Expression): TimeToMicros =
copy(child = newChild)
}
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(time, format) - Converts a time to a value of string in the format specified by the date format given by the second argument.",
+ arguments = """
+ Arguments:
+ * time - A time value to be converted to string.
+ * format - Time format pattern to follow. See <a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html">Datetime Patterns</a> for valid
+ time format patterns. Note: Only time-related patterns (H, h, m, s, S, a) are meaningful for TIME values.
+ """,
+ examples = """
+ Examples:
+ > SELECT _FUNC_(TIME'14:30:45', 'HH:mm:ss');
+ 14:30:45
+ > SELECT _FUNC_(TIME'14:30:45', 'hh:mm:ss a');
+ 02:30:45 PM
+ > SELECT _FUNC_(TIME'14:30:45.123456', 'HH:mm:ss.SSSSSS');
+ 14:30:45.123456
+ > SELECT _FUNC_(TIME'09:05:00', 'h:mm a');
+ 9:05 AM
+ """,
+ group = "datetime_funcs",
+ since = "4.2.0")
+// scalastyle:on line.size.limit
+case class TimeFormat(left: Expression, right: Expression)
+ extends BinaryExpression
+ with ImplicitCastInputTypes {
+
+ override def nullIntolerant: Boolean = true
+
+ override def inputTypes: Seq[AbstractDataType] =
+ Seq(AnyTimeType, StringTypeWithCollation(supportsTrimCollation = true))
+
+ override def dataType: DataType = StringType
+
+ // Cache the formatter if the format string is a foldable expression
+ @transient private lazy val formatterOption: Option[TimeFormatter] =
+ if (right.foldable) {
+ Option(right.eval()).map { format =>
+ TimeFormatter(format.toString, TimeFormatter.defaultLocale, isParsing = false)
+ }
+ } else {
+ None
+ }
+
+ override protected def nullSafeEval(time: Any, format: Any): Any = {
+ val nanos = time.asInstanceOf[Long]
+ val formatter = formatterOption.getOrElse {
+ TimeFormatter(format.toString, TimeFormatter.defaultLocale, isParsing = false)
+ }
+ UTF8String.fromString(formatter.format(nanos))
Review Comment:
Fixed by wrapping the format call in a try/catch for `DateTimeException` and raising `INVALID_PARAMETER_VALUE.PATTERN` instead.
--
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.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org