Skip to content

Commit 59933d2

Browse files
committed
Fix NPE
1 parent 16371f7 commit 59933d2

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,18 @@ case class ReplaceCurrentLike(catalogManager: CatalogManager) extends Rule[Logic
126126
* if the input strings are foldable.
127127
*/
128128
object SpecialDatetimeValues extends Rule[LogicalPlan] {
129+
private val conv = Map[DataType, (String, java.time.ZoneId) => Option[Any]](
130+
DateType -> convertSpecialDate,
131+
TimestampType -> convertSpecialTimestamp,
132+
TimestampNTZType -> ((s: String, _: java.time.ZoneId) => convertSpecialTimestampNTZ(s))
133+
)
129134
def apply(plan: LogicalPlan): LogicalPlan = {
130135
plan.transformAllExpressionsWithPruning(_.containsPattern(CAST)) {
131-
case cast @ Cast(e, DateType, _, _) if e.foldable && e.dataType == StringType =>
132-
convertSpecialDate(e.eval().toString, cast.zoneId)
133-
.map(Literal(_, DateType))
134-
.getOrElse(cast)
135-
case cast @ Cast(e, TimestampType, _, _) if e.foldable && e.dataType == StringType =>
136-
convertSpecialTimestamp(e.eval().toString, cast.zoneId)
137-
.map(Literal(_, TimestampType))
138-
.getOrElse(cast)
139-
case cast @ Cast(e, TimestampNTZType, _, _) if e.foldable && e.dataType == StringType =>
140-
convertSpecialTimestampNTZ(e.eval().toString)
141-
.map(Literal(_, TimestampNTZType))
136+
case cast @ Cast(e, dt @ (DateType | TimestampType | TimestampNTZType), _, _)
137+
if e.foldable && e.dataType == StringType =>
138+
Option(e.eval())
139+
.flatMap(s => conv(dt)(s.toString, cast.zoneId))
140+
.map(Literal(_, dt))
142141
.getOrElse(cast)
143142
}
144143
}

0 commit comments

Comments
 (0)