Skip to content

Commit 697688a

Browse files
committed
Switching to DateFormatter
1 parent e09c972 commit 697688a

File tree

3 files changed

+7
-17
lines changed

3 files changed

+7
-17
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ object DateTimeUtils {
7676
}
7777
}
7878

79-
// `SimpleDateFormat` is not thread-safe.
8079
private val threadLocalTimestampFormat = new ThreadLocal[TimestampFormatter] {
8180
override def initialValue(): TimestampFormatter = {
8281
TimestampFormatter("yyyy-MM-dd HH:mm:ss", TimeZoneUTC, Locale.US)
@@ -88,18 +87,13 @@ object DateTimeUtils {
8887
timestampFormatter.withTimeZone(timeZone)
8988
}
9089

91-
// `SimpleDateFormat` is not thread-safe.
92-
private val threadLocalDateFormat = new ThreadLocal[DateFormat] {
93-
override def initialValue(): SimpleDateFormat = {
94-
new SimpleDateFormat("yyyy-MM-dd", Locale.US)
90+
private val threadLocalDateFormat = new ThreadLocal[DateFormatter] {
91+
override def initialValue(): DateFormatter = {
92+
DateFormatter("yyyy-MM-dd", Locale.US)
9593
}
9694
}
9795

98-
def getThreadLocalDateFormat(timeZone: TimeZone): DateFormat = {
99-
val sdf = threadLocalDateFormat.get()
100-
sdf.setTimeZone(timeZone)
101-
sdf
102-
}
96+
def getThreadLocalDateFormat(): DateFormatter = threadLocalDateFormat.get()
10397

10498
private val computedTimeZones = new ConcurrentHashMap[String, TimeZone]
10599
private val computeTimeZone = new JFunction[String, TimeZone] {
@@ -133,11 +127,7 @@ object DateTimeUtils {
133127
}
134128

135129
def dateToString(days: SQLDate): String =
136-
getThreadLocalDateFormat(defaultTimeZone()).format(toJavaDate(days))
137-
138-
def dateToString(days: SQLDate, timeZone: TimeZone): String = {
139-
getThreadLocalDateFormat(timeZone).format(toJavaDate(days))
140-
}
130+
getThreadLocalDateFormat().format(days)
141131

142132
// Converts Timestamp to string according to Hive TimestampWritable convention.
143133
def timestampToString(us: SQLTimestamp): String = {

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/PartitioningUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ object PartitioningUtils {
457457
val dateTry = Try {
458458
// try and parse the date, if no exception occurs this is a candidate to be resolved as
459459
// DateType
460-
DateTimeUtils.getThreadLocalDateFormat(DateTimeUtils.defaultTimeZone()).parse(raw)
460+
DateTimeUtils.getThreadLocalDateFormat().parse(raw)
461461
// SPARK-23436: Casting the string to date may still return null if a bad Date is provided.
462462
// This can happen since DateFormat.parse may not use the entire text of the given string:
463463
// so if there are extra-characters after the date, it returns correctly.

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JDBCRelation.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private[sql] object JDBCRelation extends Logging {
187187
def dateTimeToString(): String = {
188188
val timeZone = DateTimeUtils.getTimeZone(timeZoneId)
189189
val dateTimeStr = columnType match {
190-
case DateType => DateTimeUtils.dateToString(value.toInt, timeZone)
190+
case DateType => DateTimeUtils.dateToString(value.toInt)
191191
case TimestampType => DateTimeUtils.timestampToString(value, timeZone)
192192
}
193193
s"'$dateTimeStr'"

0 commit comments

Comments
 (0)