Skip to content

Commit 0ea07db

Browse files
committed
Moving getOrCreateFormatter to the DateTimeFormatterHelper trait
1 parent 2a67f44 commit 0ea07db

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateFormatter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Iso8601DateFormatter(
3636
locale: Locale) extends DateFormatter with DateTimeFormatterHelper {
3737

3838
@transient
39-
private lazy val formatter = DateTimeFormatterHelper.getFormatter(pattern, locale)
39+
private lazy val formatter = getOrCreateFormatter(pattern, locale)
4040
private val UTC = ZoneId.of("UTC")
4141

4242
private def toInstant(s: String): Instant = {

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeFormatterHelper.scala

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import java.time.chrono.IsoChronology
2222
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder, ResolverStyle}
2323
import java.time.temporal.{ChronoField, TemporalAccessor, TemporalQueries}
2424
import java.util.Locale
25-
import java.util.concurrent.{Callable, TimeUnit}
2625

2726
import com.google.common.cache.CacheBuilder
2827
import com.google.common.util.concurrent.{ExecutionError, UncheckedExecutionException}
2928

29+
import org.apache.spark.sql.catalyst.util.DateTimeFormatterHelper._
30+
3031
trait DateTimeFormatterHelper {
3132
protected def toInstantWithZoneId(temporalAccessor: TemporalAccessor, zoneId: ZoneId): Instant = {
3233
val localTime = if (temporalAccessor.query(TemporalQueries.localTime) == null) {
@@ -39,15 +40,30 @@ trait DateTimeFormatterHelper {
3940
val zonedDateTime = ZonedDateTime.of(localDateTime, zoneId)
4041
Instant.from(zonedDateTime)
4142
}
43+
44+
def getOrCreateFormatter(pattern: String, locale: Locale): DateTimeFormatter = {
45+
try {
46+
val key = (pattern, locale)
47+
var formatter = cache.getIfPresent(key)
48+
if (formatter == null) {
49+
formatter = buildFormatter(pattern, locale)
50+
cache.put(key, formatter)
51+
}
52+
formatter
53+
} catch {
54+
// Cache.get() may wrap the original exception.
55+
case e @ (_: UncheckedExecutionException | _: ExecutionError) =>
56+
throw e.getCause
57+
}
58+
}
4259
}
4360

44-
object DateTimeFormatterHelper {
45-
private val cache = CacheBuilder.newBuilder()
61+
private object DateTimeFormatterHelper {
62+
val cache = CacheBuilder.newBuilder()
4663
.maximumSize(128)
4764
.build[(String, Locale), DateTimeFormatter]()
4865

49-
50-
private def buildFormatter(pattern: String, locale: Locale): DateTimeFormatter = {
66+
def buildFormatter(pattern: String, locale: Locale): DateTimeFormatter = {
5167
new DateTimeFormatterBuilder()
5268
.parseCaseInsensitive()
5369
.appendPattern(pattern)
@@ -60,20 +76,4 @@ object DateTimeFormatterHelper {
6076
.withChronology(IsoChronology.INSTANCE)
6177
.withResolverStyle(ResolverStyle.STRICT)
6278
}
63-
64-
def getFormatter(pattern: String, locale: Locale): DateTimeFormatter = {
65-
try {
66-
val key = (pattern, locale)
67-
var formatter = cache.getIfPresent(key)
68-
if (formatter == null) {
69-
formatter = buildFormatter(pattern, locale)
70-
cache.put(key, formatter)
71-
}
72-
formatter
73-
} catch {
74-
// Cache.get() may wrap the original exception.
75-
case e @ (_: UncheckedExecutionException | _: ExecutionError) =>
76-
throw e.getCause
77-
}
78-
}
7979
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TimestampFormatter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Iso8601TimestampFormatter(
5151
timeZone: TimeZone,
5252
locale: Locale) extends TimestampFormatter with DateTimeFormatterHelper {
5353
@transient
54-
private lazy val formatter = DateTimeFormatterHelper.getFormatter(pattern, locale)
54+
private lazy val formatter = getOrCreateFormatter(pattern, locale)
5555

5656
private def toInstant(s: String): Instant = {
5757
val temporalAccessor = formatter.parse(s)

0 commit comments

Comments
 (0)