Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
override def nullable = (child.dataType, dataType) match {
case (StringType, _: NumericType) => true
case (StringType, TimestampType) => true
case (DoubleType, TimestampType) => true
case (FloatType, TimestampType) => true
case (StringType, DateType) => true
case (_: NumericType, DateType) => true
case (BooleanType, DateType) => true
Expand Down Expand Up @@ -115,10 +117,18 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
buildCast[Decimal](_, d => decimalToTimestamp(d))
// TimestampWritable.doubleToTimestamp
case DoubleType =>
buildCast[Double](_, d => decimalToTimestamp(Decimal(d)))
buildCast[Double](_, d => try {
decimalToTimestamp(Decimal(d))
} catch {
case _: NumberFormatException => null
})
// TimestampWritable.floatToTimestamp
case FloatType =>
buildCast[Float](_, f => decimalToTimestamp(Decimal(f)))
buildCast[Float](_, f => try {
decimalToTimestamp(Decimal(f))
} catch {
case _: NumberFormatException => null
})
}

private[this] def decimalToTimestamp(d: Decimal) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ class ExpressionEvaluationSuite extends FunSuite {

// A test for higher precision than millis
checkEvaluation(Cast(Cast(0.00000001, TimestampType), DoubleType), 0.00000001)

checkEvaluation(Cast(Literal(Double.NaN), TimestampType), null)
checkEvaluation(Cast(Literal(1.0 / 0.0), TimestampType), null)
checkEvaluation(Cast(Literal(Float.NaN), TimestampType), null)
checkEvaluation(Cast(Literal(1.0f / 0.0f), TimestampType), null)
}

test("null checking") {
Expand Down