Skip to content

Commit

Permalink
[SPARK-17884][SQL] To resolve Null pointer exception when casting fro…
Browse files Browse the repository at this point in the history
…m 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.
  • Loading branch information
priyankagar authored and rxin committed Oct 12, 2016
1 parent 8880fd1 commit d5580eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,12 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
private[this] def castToIntervalCode(from: DataType): CastFunction = from match {
case StringType =>
(c, evPrim, evNull) =>
s"$evPrim = CalendarInterval.fromString($c.toString());"
s"""$evPrim = CalendarInterval.fromString($c.toString());
if(${evPrim} == null) {
${evNull} = true;
}
""".stripMargin

}

private[this] def decimalToTimestampCode(d: String): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
test("cast between string and interval") {
import org.apache.spark.unsafe.types.CalendarInterval

checkEvaluation(Cast(Literal(""), CalendarIntervalType), null)
checkEvaluation(Cast(Literal("interval -3 month 7 hours"), CalendarIntervalType),
new CalendarInterval(-3, 7 * CalendarInterval.MICROS_PER_HOUR))
checkEvaluation(Cast(Literal.create(
Expand Down

0 comments on commit d5580eb

Please sign in to comment.