dongjoon-hyun commented on a change in pull request #25220: [SPARK-28459][SQL]
Add `make_timestamp` function
URL: https://github.com/apache/spark/pull/25220#discussion_r308022742
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
##########
@@ -1657,3 +1657,156 @@ case class MakeDate(year: Expression, month:
Expression, day: Expression)
override def prettyName: String = "make_date"
}
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(year, month, day, hour, min, sec[, timezone]) - Create
timestamp from year, month, day, hour, min, sec and timezone fields.",
+ arguments = """
+ Arguments:
+ * year - the year to represent, from 1 to 9999
+ * month - the month-of-year to represent, from 1 (January) to 12
(December)
+ * day - the day-of-month to represent, from 1 to 31
+ * hour - the hour-of-day to represent, from 0 to 23
+ * min - the minute-of-hour to represent, from 0 to 59
+ * sec - the second-of-minute and its micro-fraction to represent, from
+ 0 to 60. If the second argument equals to 60, the seconds
field is set
+ to 0 and 1 minute is added to the final timestamp.
+ * timezone - the time zone identifier. For example, CET, UTC and etc.
+ """,
+ examples = """
+ Examples:
+ > SELECT _FUNC_(2014, 12, 28, 6, 30, 45.887);
+ 2014-12-28 06:30:45.887
+ > SELECT _FUNC_(2014, 12, 28, 6, 30, 45.887, 'CET');
+ 2014-12-28 10:30:45.887
+ > SELECT _FUNC_(2019, 6, 30, 23, 59, 60)
+ 2019-07-01 00:00:00
+ > SELECT _FUNC_(2019, 13, 1, 10, 11, 12, 13);
+ NULL
+ > SELECT _FUNC_(null, 7, 22, 15, 30, 0);
+ NULL
+ """,
+ since = "3.0.0")
+// scalastyle:on line.size.limit
+case class MakeTimestamp(
+ year: Expression,
+ month: Expression,
+ day: Expression,
+ hour: Expression,
+ min: Expression,
+ sec: Expression,
+ timezone: Option[Expression] = None,
+ timeZoneId: Option[String] = None)
+ extends SeptenaryExpression with TimeZoneAwareExpression with
ImplicitCastInputTypes {
+
+ def this(
+ year: Expression,
+ month: Expression,
+ day: Expression,
+ hour: Expression,
+ min: Expression,
+ sec: Expression) = {
+ this(year, month, day, hour, min, sec, None, None)
+ }
+
+ def this(
+ year: Expression,
+ month: Expression,
+ day: Expression,
+ hour: Expression,
+ min: Expression,
+ sec: Expression,
+ timezone: Expression) = {
+ this(year, month, day, hour, min, sec, Some(timezone), None)
+ }
+
+ override def children: Seq[Expression] = Seq(year, month, day, hour, min,
sec) ++ timezone
+ override def inputTypes: Seq[AbstractDataType] =
+ Seq(IntegerType, IntegerType, IntegerType, IntegerType, IntegerType,
DoubleType) ++
+ timezone.map(_ => StringType)
+ override def dataType: DataType = TimestampType
+ override def nullable: Boolean = true
+
+ override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression =
+ copy(timeZoneId = Option(timeZoneId))
+
+ private def toMicros(
+ year: Int,
+ month: Int,
+ day: Int,
+ hour: Int,
+ min: Int,
+ secAndNanos: Double,
+ zoneId: ZoneId): Any = {
+ try {
+ val seconds = secAndNanos.toInt
+ val nanos = ((secAndNanos - seconds) * NANOS_PER_SECOND).toInt
+ val ldt = if (seconds == 60) {
+ if (nanos == 0) {
+ // This case of sec = 60 and nanos = 0 is supported for
compatibility with PostgreSQL
+ LocalDateTime.of(year, month, day, hour, min, 0, 0).plusMinutes(1)
+ } else {
+ throw new DateTimeException("The fraction of sec must be zero. Valid
range is [0, 60].")
Review comment:
Note for me. This exception message is not used.
----------------------------------------------------------------
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]