Skip to content
Snippets Groups Projects
Commit a6405c5d authored by Takuya UESHIN's avatar Takuya UESHIN Committed by Michael Armbrust
Browse files

[SPARK-4270][SQL] Fix Cast from DateType to DecimalType.

`Cast` from `DateType` to `DecimalType` throws `NullPointerException`.

Author: Takuya UESHIN <ueshin@happy-camper.st>

Closes #3134 from ueshin/issues/SPARK-4270 and squashes the following commits:

7394e4b [Takuya UESHIN] Fix Cast from DateType to DecimalType.
parent 60ab80f5
No related branches found
No related tags found
No related merge requests found
...@@ -281,7 +281,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w ...@@ -281,7 +281,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
case BooleanType => case BooleanType =>
buildCast[Boolean](_, b => changePrecision(if (b) Decimal(1) else Decimal(0), target)) buildCast[Boolean](_, b => changePrecision(if (b) Decimal(1) else Decimal(0), target))
case DateType => case DateType =>
buildCast[Date](_, d => changePrecision(null, target)) // date can't cast to decimal in Hive buildCast[Date](_, d => null) // date can't cast to decimal in Hive
case TimestampType => case TimestampType =>
// Note that we lose precision here. // Note that we lose precision here.
buildCast[Timestamp](_, t => changePrecision(Decimal(timestampToDouble(t)), target)) buildCast[Timestamp](_, t => changePrecision(Decimal(timestampToDouble(t)), target))
......
...@@ -412,6 +412,8 @@ class ExpressionEvaluationSuite extends FunSuite { ...@@ -412,6 +412,8 @@ class ExpressionEvaluationSuite extends FunSuite {
checkEvaluation(Cast(d, LongType), null) checkEvaluation(Cast(d, LongType), null)
checkEvaluation(Cast(d, FloatType), null) checkEvaluation(Cast(d, FloatType), null)
checkEvaluation(Cast(d, DoubleType), null) checkEvaluation(Cast(d, DoubleType), null)
checkEvaluation(Cast(d, DecimalType.Unlimited), null)
checkEvaluation(Cast(d, DecimalType(10, 2)), null)
checkEvaluation(Cast(d, StringType), "1970-01-01") checkEvaluation(Cast(d, StringType), "1970-01-01")
checkEvaluation(Cast(Cast(d, TimestampType), StringType), "1970-01-01 00:00:00") checkEvaluation(Cast(Cast(d, TimestampType), StringType), "1970-01-01 00:00:00")
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment