-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-26546][SQL] Caching of java.time.format.DateTimeFormatter #23462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
aa3c146
f08c71e
61038ea
68ec759
5c2d99d
2a67f44
0ea07db
b7413a4
0737991
545a710
c68778c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,10 +22,34 @@ 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.{Callable, TimeUnit} | ||
|
|
||
| import com.google.common.cache.CacheBuilder | ||
| import com.google.common.util.concurrent.{ExecutionError, UncheckedExecutionException} | ||
|
|
||
| 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 = CacheBuilder.newBuilder() | ||
| .initialCapacity(8) | ||
| .maximumSize(128) | ||
| .expireAfterAccess(1, TimeUnit.HOURS) | ||
|
||
| .build[(String, Locale), DateTimeFormatter]() | ||
|
|
||
|
|
||
| protected def buildFormatter(pattern: String, locale: Locale): DateTimeFormatter = { | ||
| private def buildFormatter(pattern: String, locale: Locale): DateTimeFormatter = { | ||
| new DateTimeFormatterBuilder() | ||
| .parseCaseInsensitive() | ||
| .appendPattern(pattern) | ||
|
|
@@ -39,15 +63,17 @@ trait DateTimeFormatterHelper { | |
| .withResolverStyle(ResolverStyle.STRICT) | ||
| } | ||
|
|
||
| protected def toInstantWithZoneId(temporalAccessor: TemporalAccessor, zoneId: ZoneId): Instant = { | ||
| val localTime = if (temporalAccessor.query(TemporalQueries.localTime) == null) { | ||
| LocalTime.ofNanoOfDay(0) | ||
| } else { | ||
| LocalTime.from(temporalAccessor) | ||
| def getFormatter(pattern: String, locale: Locale): DateTimeFormatter = { | ||
HyukjinKwon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| cache.get( | ||
| (pattern, locale), | ||
| new Callable[DateTimeFormatter]() { | ||
srowen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| override def call = buildFormatter(pattern, locale) | ||
| }) | ||
| } catch { | ||
| // Cache.get() may wrap the original exception. | ||
| case e @ (_: UncheckedExecutionException | _: ExecutionError) => | ||
| throw e.getCause | ||
| } | ||
| val localDate = LocalDate.from(temporalAccessor) | ||
| val localDateTime = LocalDateTime.of(localDate, localTime) | ||
| val zonedDateTime = ZonedDateTime.of(localDateTime, zoneId) | ||
| Instant.from(zonedDateTime) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.