srowen commented on a change in pull request #23462: [SPARK-26546][SQL] Caching
of java.time.format.DateTimeFormatter
URL: https://github.com/apache/spark/pull/23462#discussion_r245513064
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeFormatterHelper.scala
##########
@@ -22,10 +22,40 @@ import java.time.chrono.IsoChronology
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder,
ResolverStyle}
import java.time.temporal.{ChronoField, TemporalAccessor, TemporalQueries}
import java.util.Locale
+import java.util.concurrent.ConcurrentHashMap
trait DateTimeFormatterHelper {
+ protected def toInstantWithZoneId(temporalAccessor: TemporalAccessor,
zoneId: ZoneId): Instant = {
+ val localTime = if (temporalAccessor.query(TemporalQueries.localTime) ==
null) {
+ LocalTime.ofNanoOfDay(0)
+ } else {
+ LocalTime.from(temporalAccessor)
+ }
+ val localDate = LocalDate.from(temporalAccessor)
+ val localDateTime = LocalDateTime.of(localDate, localTime)
+ val zonedDateTime = ZonedDateTime.of(localDateTime, zoneId)
+ Instant.from(zonedDateTime)
+ }
+}
+
+object DateTimeFormatterHelper {
+ private val cache = new ConcurrentHashMap[(String, Locale),
DateTimeFormatter]()
+
+ def getFormatter(pattern: String, locale: Locale): DateTimeFormatter = {
+ val key = (pattern, locale)
+ var formatter = cache.get(key)
Review comment:
Commons lang3 supports Java 7, so couldn't use computeIfAbsent. I presume it
would if it could. I don't feel super strongly about it, but think we can take
advantage of Java 8 here. It saves a second lookup, and in so doing, avoids the
(fairly harmless) race condition here -- multiple threads can find the instance
isn't cached and compute and try to put the result. It is still correct but not
optimal.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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]