Github user davies commented on a diff in the pull request: https://github.com/apache/spark/pull/7353#discussion_r34490284 --- 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 == ':') { + segments(i) = currentSegmentValue + currentSegmentValue = 0 + i += 1 + } else { + return null + } + } else if (i < 7) { + if (b == 'Z') { + segments(i) = currentSegmentValue + currentSegmentValue = 0 + i += 1 + timeZone = Some(43) + } else if (b == '-' || b == '+') { + segments(i) = currentSegmentValue + currentSegmentValue = 0 + i += 1 + timeZone = Some(b) + } else if (b == '.' && i == 5) { + segments(i) = currentSegmentValue + currentSegmentValue = 0 + i += 1 + } else { + return null + } + if (i == 6 && b != '.') { + i += 1 + } + } else if (i > 6) { + if (b == ':') { + segments(i) = currentSegmentValue + currentSegmentValue = 0 + i += 1 + } else { + return null + } + } + } else { + if (i == 6) { + digitsMilli += 1 + } + currentSegmentValue = currentSegmentValue * 10 + parsedValue + } + j += 1 + } + if (i > 8) { + return null + } + segments(i) = currentSegmentValue + + // Hive compatibility 2011-05-06 07:08:09.1000 == 2011-05-06 07:08:09.1 + if (digitsMilli == 4) { + segments(6) = segments(6) / 10 + } + + // 18:3:1.1 is equals to 18:3:1:100 + if (digitsMilli == 1) { + segments(6) = segments(6) * 100 + } else if (digitsMilli == 2) { + segments(6) = segments(6) * 10 + } + + if (segments(0) < 0 || segments(0) > 9999 || segments(1) < 1 || segments(1) > 12 || + segments(2) < 1 || segments(2) > 31 || segments(3) < 0 || segments(3) > 23 || + segments(4) < 0 || segments(4) > 59 || segments(5) < 0 || segments(5) > 59 || + segments(6) < 0 || segments(6) > 999 || segments(7) < 0 || segments(7) > 14 || + segments(8) < 0 || segments(8) > 59) { + return null + } + val c = if (timeZone.isEmpty) { + Calendar.getInstance() + } else { + Calendar.getInstance( + TimeZone.getTimeZone(f"GMT${timeZone.get.toChar}${segments(7)}%02d:${segments(8)}%02d")) + } + c.set(segments(0), segments(1) - 1, segments(2), segments(3), segments(4), segments(5)) + c.set(Calendar.MILLISECOND, segments(6)) + new Timestamp(c.getTimeInMillis) + } + + /** + * Parses a given UTF8 date string to the corresponding [[Date]] 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 *`, `yyyy-[m]m-[d]dT*` + */ + def stringToDate(s: UTF8String): Date = { + if (s == null) { + return null + } + val segments: Array[Int] = Array[Int](1, 1, 1) + var i = 0 + var currentSegmentValue = 0 + val bytes = s.getBytes + var j = 0 + while (j < bytes.length && (i < 3 && !(bytes(j) == ' ' || bytes(j) == 'T'))) { + val b = bytes(j) + if (i < 2 && b == '-') { + segments(i) = currentSegmentValue + currentSegmentValue = 0 + i += 1 + } else { + val parsedValue = b - 48 --- End diff -- I think the compiler could take care it (constant folding)
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org