Skip to content

Commit ddf127d

Browse files
committed
Remove withStrongLegacy
1 parent b14ee41 commit ddf127d

2 files changed

Lines changed: 33 additions & 21 deletions

File tree

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import java.time.temporal.IsoFields
2323
import java.util.{Locale, TimeZone}
2424

2525
import scala.util.control.NonFatal
26-
2726
import org.apache.commons.text.StringEscapeUtils
28-
2927
import org.apache.spark.sql.AnalysisException
3028
import org.apache.spark.sql.catalyst.InternalRow
3129
import org.apache.spark.sql.catalyst.expressions.codegen._
3230
import org.apache.spark.sql.catalyst.expressions.codegen.Block._
33-
import org.apache.spark.sql.catalyst.util.{DateTimeUtils, TimestampFormatter}
31+
import org.apache.spark.sql.catalyst.util.LegacyDateFormats.SIMPLE_DATE_FORMAT
32+
import org.apache.spark.sql.catalyst.util.{DateTimeUtils, LegacyDateFormats, TimestampFormatter}
3433
import org.apache.spark.sql.catalyst.util.DateTimeConstants._
3534
import org.apache.spark.sql.catalyst.util.DateTimeUtils._
3635
import org.apache.spark.sql.types._
@@ -622,13 +621,15 @@ case class DateFormatClass(left: Expression, right: Expression, timeZoneId: Opti
622621

623622
@transient private lazy val formatter: Option[TimestampFormatter] = {
624623
if (right.foldable) {
625-
Option(right.eval()).map(format => TimestampFormatter(format.toString, zoneId))
624+
Option(right.eval()).map { format =>
625+
TimestampFormatter(format.toString, zoneId, legacyFormat = SIMPLE_DATE_FORMAT)
626+
}
626627
} else None
627628
}
628629

629630
override protected def nullSafeEval(timestamp: Any, format: Any): Any = {
630631
val tf = if (formatter.isEmpty) {
631-
TimestampFormatter.withStrongLegacy(format.toString, zoneId)
632+
TimestampFormatter(format.toString, zoneId, legacyFormat = SIMPLE_DATE_FORMAT)
632633
} else {
633634
formatter.get
634635
}
@@ -643,10 +644,14 @@ case class DateFormatClass(left: Expression, right: Expression, timeZoneId: Opti
643644
})
644645
}.getOrElse {
645646
val tf = TimestampFormatter.getClass.getName.stripSuffix("$")
647+
val ldf = LegacyDateFormats.getClass.getName.stripSuffix("$")
646648
val zid = ctx.addReferenceObj("zoneId", zoneId, classOf[ZoneId].getName)
647649
defineCodeGen(ctx, ev, (timestamp, format) => {
648-
s"""UTF8String.fromString($tf$$.MODULE$$.withStrongLegacy($format.toString(), $zid)
649-
.format($timestamp))"""
650+
s"""|UTF8String.fromString($tf$$.MODULE$$.apply(
651+
| $format.toString(),
652+
| $zid,
653+
| $ldf$$.MODULE$$.SIMPLE_DATE_FORMAT())
654+
|.format($timestamp))""".stripMargin
650655
})
651656
}
652657
}
@@ -758,7 +763,7 @@ abstract class ToTimestamp
758763
private lazy val constFormat: UTF8String = right.eval().asInstanceOf[UTF8String]
759764
private lazy val formatter: TimestampFormatter =
760765
try {
761-
TimestampFormatter.withStrongLegacy(constFormat.toString, zoneId)
766+
TimestampFormatter(constFormat.toString, zoneId, legacyFormat = SIMPLE_DATE_FORMAT)
762767
} catch {
763768
case NonFatal(_) => null
764769
}
@@ -791,8 +796,8 @@ abstract class ToTimestamp
791796
} else {
792797
val formatString = f.asInstanceOf[UTF8String].toString
793798
try {
794-
TimestampFormatter.withStrongLegacy(formatString, zoneId).parse(
795-
t.asInstanceOf[UTF8String].toString) / downScaleFactor
799+
TimestampFormatter(formatString, zoneId, legacyFormat = SIMPLE_DATE_FORMAT)
800+
.parse(t.asInstanceOf[UTF8String].toString) / downScaleFactor
796801
} catch {
797802
case NonFatal(_) => null
798803
}
@@ -832,11 +837,15 @@ abstract class ToTimestamp
832837
case StringType =>
833838
val zid = ctx.addReferenceObj("zoneId", zoneId, classOf[ZoneId].getName)
834839
val tf = TimestampFormatter.getClass.getName.stripSuffix("$")
840+
val ldf = LegacyDateFormats.getClass.getName.stripSuffix("$")
835841
nullSafeCodeGen(ctx, ev, (string, format) => {
836842
s"""
837843
try {
838-
${ev.value} = $tf$$.MODULE$$.withStrongLegacy($format.toString(), $zid)
839-
.parse($string.toString()) / $downScaleFactor;
844+
${ev.value} = $tf$$.MODULE$$.apply(
845+
$format.toString(),
846+
$zid,
847+
$ldf$$.MODULE$$.SIMPLE_DATE_FORMAT())
848+
.parse($string.toString()) / $downScaleFactor;
840849
} catch (java.lang.IllegalArgumentException e) {
841850
${ev.isNull} = true;
842851
} catch (java.text.ParseException e) {
@@ -921,7 +930,7 @@ case class FromUnixTime(sec: Expression, format: Expression, timeZoneId: Option[
921930
private lazy val constFormat: UTF8String = right.eval().asInstanceOf[UTF8String]
922931
private lazy val formatter: TimestampFormatter =
923932
try {
924-
TimestampFormatter.withStrongLegacy(constFormat.toString, zoneId)
933+
TimestampFormatter(constFormat.toString, zoneId, legacyFormat = SIMPLE_DATE_FORMAT)
925934
} catch {
926935
case NonFatal(_) => null
927936
}
@@ -947,8 +956,9 @@ case class FromUnixTime(sec: Expression, format: Expression, timeZoneId: Option[
947956
null
948957
} else {
949958
try {
950-
UTF8String.fromString(TimestampFormatter.withStrongLegacy(f.toString, zoneId)
951-
.format(time.asInstanceOf[Long] * MICROS_PER_SECOND))
959+
UTF8String.fromString(
960+
TimestampFormatter(f.toString, zoneId, legacyFormat = SIMPLE_DATE_FORMAT)
961+
.format(time.asInstanceOf[Long] * MICROS_PER_SECOND))
952962
} catch {
953963
case NonFatal(_) => null
954964
}
@@ -980,11 +990,13 @@ case class FromUnixTime(sec: Expression, format: Expression, timeZoneId: Option[
980990
} else {
981991
val zid = ctx.addReferenceObj("zoneId", zoneId, classOf[ZoneId].getName)
982992
val tf = TimestampFormatter.getClass.getName.stripSuffix("$")
993+
val ldf = LegacyDateFormats.getClass.getName.stripSuffix("$")
983994
nullSafeCodeGen(ctx, ev, (seconds, f) => {
984995
s"""
985996
try {
986-
${ev.value} = UTF8String.fromString($tf$$.MODULE$$.withStrongLegacy($f.toString(), $zid).
987-
format($seconds * 1000000L));
997+
${ev.value} = UTF8String.fromString(
998+
$tf$$.MODULE$$.apply($f.toString(), $zid, $ldf$$.MODULE$$.SIMPLE_DATE_FORMAT())
999+
.format($seconds * 1000000L));
9881000
} catch (java.lang.IllegalArgumentException e) {
9891001
${ev.isNull} = true;
9901002
}"""

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ object TimestampFormatter {
240240
getFormatter(Some(format), zoneId, locale, legacyFormat)
241241
}
242242

243+
def apply(format: String, zoneId: ZoneId, legacyFormat: LegacyDateFormat): TimestampFormatter = {
244+
getFormatter(Some(format), zoneId, defaultLocale, legacyFormat)
245+
}
246+
243247
def apply(format: String, zoneId: ZoneId): TimestampFormatter = {
244248
getFormatter(Some(format), zoneId)
245249
}
@@ -251,8 +255,4 @@ object TimestampFormatter {
251255
def getFractionFormatter(zoneId: ZoneId): TimestampFormatter = {
252256
new FractionTimestampFormatter(zoneId)
253257
}
254-
255-
def withStrongLegacy(format: String, zoneId: ZoneId): TimestampFormatter = {
256-
apply(format, zoneId, defaultLocale, SIMPLE_DATE_FORMAT)
257-
}
258258
}

0 commit comments

Comments
 (0)