Github user tarekauel commented on a diff in the pull request:
https://github.com/apache/spark/pull/7353#discussion_r34523735
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala
---
@@ -180,4 +182,169 @@ object DateTimeUtils {
val nanos = (us % MICROS_PER_SECOND) * 1000L
(day.toInt, secondsInDay * NANOS_PER_SECOND + nanos)
}
+
+ /**
+ * Parses a given UTF8 date string to the corresponding [[Timestamp]]
object. The format of the
+ * date has to be one of the following: `yyyy`, `yyyy-[m]m`,
`yyyy-[m]m-[d]d`, `yyyy-[m]m-[d]d `,
+ * `yyyy-[m]m-[d]d [h]h:[m]m:[s]s.[ms][ms][ms][ms]`,
+ * `yyyy-[m]m-[d]d [h]h:[m]m:[s]s.[ms][ms][ms][ms]Z`,
+ * `yyyy-[m]m-[d]d [h]h:[m]m:[s]s.[ms][ms][ms][ms]-[h]h:[m]m`,
+ * `yyyy-[m]m-[d]d [h]h:[m]m:[s]s.[ms][ms][ms][ms]+[h]h:[m]m`,
+ * `yyyy-[m]m-[d]dT[h]h:[m]m:[s]s.[ms][ms][ms][ms]`,
+ * `yyyy-[m]m-[d]dT[h]h:[m]m:[s]s.[ms][ms][ms][ms]Z`,
+ * `yyyy-[m]m-[d]dT[h]h:[m]m:[s]s.[ms][ms][ms][ms]-[h]h:[m]m`,
+ * `yyyy-[m]m-[d]dT[h]h:[m]m:[s]s.[ms][ms][ms][ms]+[h]h:[m]m`,
+ */
+ def stringToTimestamp(s: UTF8String): Timestamp = {
+ if (s == null) {
+ return null
+ }
+ var timeZone: Option[Byte] = None
+ val segments: Array[Int] = Array[Int](1, 1, 1, 0, 0, 0, 0, 0, 0)
+ var i = 0
+ var currentSegmentValue = 0
+ val bytes = s.getBytes
+ var j = 0
+ var digitsMilli = 0
+ while (j < bytes.length) {
+ val b = bytes(j)
+ val parsedValue = b - 48
+ if (parsedValue < 0 || parsedValue > 9) {
+ if (i < 2) {
+ if (b == '-') {
+ segments(i) = currentSegmentValue
+ currentSegmentValue = 0
+ i += 1
+ } else {
+ return null
+ }
+ } else if (i == 2) {
+ if (b == ' ' || b == 'T') {
+ segments(i) = currentSegmentValue
+ currentSegmentValue = 0
+ i += 1
+ } else {
+ return null
+ }
+ } else if (i < 5) {
+ if (b == ':') {
--- End diff --
This is equals to `i == 3 || i == 4`, because of the `if` and `elseif`
before. I am going to adjust the checks that
they are more readable.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]