Skip to content

Commit 6bdbe7d

Browse files
committed
[SPARK-17884][SQL] To resolve Null pointer exception when casting from empty string to interval type.
## What changes were proposed in this pull request? This change adds a check in castToInterval method of Cast expression , such that if converted value is null , then isNull variable should be set to true. Earlier, the expression Cast(Literal(), CalendarIntervalType) was throwing NullPointerException because of the above mentioned reason. ## How was this patch tested? Added test case in CastSuite.scala jira entry for detail: https://issues.apache.org/jira/browse/SPARK-17884 Author: prigarg <[email protected]> Closes #15449 from priyankagargnitk/SPARK-17884.
1 parent 18b173c commit 6bdbe7d

File tree

2 files changed

+7
-1
lines changed
  • sql/catalyst/src
    • main/scala/org/apache/spark/sql/catalyst/expressions
    • test/scala/org/apache/spark/sql/catalyst/expressions

2 files changed

+7
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,12 @@ case class Cast(child: Expression, dataType: DataType)
656656
private[this] def castToIntervalCode(from: DataType): CastFunction = from match {
657657
case StringType =>
658658
(c, evPrim, evNull) =>
659-
s"$evPrim = CalendarInterval.fromString($c.toString());"
659+
s"""$evPrim = CalendarInterval.fromString($c.toString());
660+
if(${evPrim} == null) {
661+
${evNull} = true;
662+
}
663+
""".stripMargin
664+
660665
}
661666

662667
private[this] def decimalToTimestampCode(d: String): String =

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
764764
test("cast between string and interval") {
765765
import org.apache.spark.unsafe.types.CalendarInterval
766766

767+
checkEvaluation(Cast(Literal(""), CalendarIntervalType), null)
767768
checkEvaluation(Cast(Literal("interval -3 month 7 hours"), CalendarIntervalType),
768769
new CalendarInterval(-3, 7 * CalendarInterval.MICROS_PER_HOUR))
769770
checkEvaluation(Cast(Literal.create(

0 commit comments

Comments
 (0)