MaxGekk commented on a change in pull request #25716: [SPARK-29012][SQL]
Support special timestamp values
URL: https://github.com/apache/spark/pull/25716#discussion_r322590203
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala
##########
@@ -848,4 +852,46 @@ object DateTimeUtils {
val sinceEpoch = BigDecimal(timestamp) / MICROS_PER_SECOND + offset
new Decimal().set(sinceEpoch, 20, 6)
}
+
+ def currentTimestamp(): SQLTimestamp = instantToMicros(Instant.now())
+
+ private def today(zoneId: ZoneId): ZonedDateTime = {
+ Instant.now().atZone(zoneId).`with`(LocalTime.MIDNIGHT)
+ }
+
+ private val specialValue =
"""(EPOCH|NOW|TODAY|TOMORROW|YESTERDAY)\p{Blank}*(.*)""".r
+
+ /**
+ * Converts notational shorthands that are converted to ordinary timestamps.
+ * @param input - a trimmed string
+ * @param zoneId - zone identifier used to get the current date.
+ * @return some of microseconds since the epoch if the conversion completed
+ * successfully otherwise None.
+ */
+ def convertSpecialTimestamp(input: String, zoneId: ZoneId):
Option[SQLTimestamp] = {
+ def isValidZoneId(z: String): Boolean = {
+ z == "" || Try { getZoneId(z) }.isSuccess
+ }
+
+ if (input.length < 3 || !input(0).isLetter) return None
+ input.toUpperCase(Locale.US) match {
+ case specialValue("EPOCH", z) if isValidZoneId(z) => Some(0)
+ case specialValue("NOW", "") => Some(currentTimestamp())
+ case specialValue("TODAY", z) if isValidZoneId(z) =>
+ Some(instantToMicros(today(zoneId).toInstant))
+ case specialValue("TOMORROW", z) if isValidZoneId(z) =>
+ Some(instantToMicros(today(zoneId).plusDays(1).toInstant))
+ case specialValue("YESTERDAY", z) if isValidZoneId(z) =>
+ Some(instantToMicros(today(zoneId).minusDays(1).toInstant))
+ case _ => None
+ }
+ }
+
+ private def convertSpecialTimestamp(bytes: Array[Byte], zoneId: ZoneId):
Option[SQLTimestamp] = {
Review comment:
Because I need `String` inside of `extractSpecialValue`, and
`UTF8String.fromString` converts `UTF8String` to `String` via `Array[Byte]`.
Why should we convert the same string to bytes twice?
----------------------------------------------------------------
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]