MaxGekk commented on code in PR #41078:
URL: https://github.com/apache/spark/pull/41078#discussion_r1192342220
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TimestampFormatter.scala:
##########
@@ -163,27 +165,66 @@ class Iso8601TimestampFormatter(
protected lazy val legacyFormatter = TimestampFormatter.getLegacyFormatter(
pattern, zoneId, locale, legacyFormat)
+ override def parseOptional(s: String): Option[Long] = {
+ try {
+ val parsed = formatter.parseUnresolved(s, new ParsePosition(0))
+ if (parsed != null) {
+ val (epochSeconds, microsOfSecond) = extractSeconds(parsed)
+ Some(Math.addExact(Math.multiplyExact(epochSeconds,
MICROS_PER_SECOND), microsOfSecond))
Review Comment:
Could you move the common part:
```scala
Math.addExact(Math.multiplyExact(epochSeconds, MICROS_PER_SECOND),
microsOfSecond)
```
to `extractSeconds()`
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TimestampFormatter.scala:
##########
@@ -163,27 +165,66 @@ class Iso8601TimestampFormatter(
protected lazy val legacyFormatter = TimestampFormatter.getLegacyFormatter(
pattern, zoneId, locale, legacyFormat)
+ override def parseOptional(s: String): Option[Long] = {
+ try {
+ val parsed = formatter.parseUnresolved(s, new ParsePosition(0))
+ if (parsed != null) {
+ val (epochSeconds, microsOfSecond) = extractSeconds(parsed)
+ Some(Math.addExact(Math.multiplyExact(epochSeconds,
MICROS_PER_SECOND), microsOfSecond))
+ } else {
+ None
+ }
+ } catch {
+ case NonFatal(_) => None
+ }
+ }
+
+ private def extractSeconds(parsed: TemporalAccessor): (Long, Long) = {
+ val parsedZoneId = parsed.query(TemporalQueries.zone())
+ val timeZoneId = if (parsedZoneId == null) zoneId else parsedZoneId
+ val zonedDateTime = toZonedDateTime(parsed, timeZoneId)
+ val epochSeconds = zonedDateTime.toEpochSecond
+ val microsOfSecond = zonedDateTime.get(MICRO_OF_SECOND)
+ (epochSeconds, microsOfSecond)
+ }
Review Comment:
```suggestion
private def extractMicros(parsed: TemporalAccessor): Long = {
val parsedZoneId = parsed.query(TemporalQueries.zone())
val timeZoneId = if (parsedZoneId == null) zoneId else parsedZoneId
val zonedDateTime = toZonedDateTime(parsed, timeZoneId)
val epochSeconds = zonedDateTime.toEpochSecond
val microsOfSecond = zonedDateTime.get(MICRO_OF_SECOND)
Math.addExact(Math.multiplyExact(epochSeconds, MICROS_PER_SECOND),
microsOfSecond)
}
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]