Skip to content

Commit

Permalink
Fixed precision for double.
Browse files Browse the repository at this point in the history
  • Loading branch information
rxin committed Apr 8, 2014
1 parent 2adb235 commit 18aacd3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {
// Timestamp to long, converting milliseconds to seconds
private def timestampToLong(ts: Timestamp) = ts.getTime / 1000

private def timestampToDouble(ts: Timestamp) = ts.getTime.toDouble / 1000
private def timestampToDouble(ts: Timestamp) = {
// First part is the seconds since the beginning of time, followed by nanosecs.
ts.getTime / 1000 + ts.getNanos.toDouble / 1000000000
}

def castToLong: Any => Any = child.dataType match {
case StringType => nullOrCast[String](_, s => try s.toLong catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ class ExpressionEvaluationSuite extends FunSuite {
}

test("timestamp casting") {
val millis = 15 * 1000 + 1
val millis = 15 * 1000 + 2
val ts = new Timestamp(millis)
val ts1 = new Timestamp(15 * 1000) // a timestamp without the milliseconds part
checkEvaluation(ts cast ShortType, 15)
checkEvaluation(ts cast IntegerType, 15)
checkEvaluation(ts cast LongType, 15)
checkEvaluation(ts cast FloatType, 15.001f)
checkEvaluation(ts cast DoubleType, 15.001)
checkEvaluation(Cast(ts, ShortType), 15)
checkEvaluation(Cast(ts, IntegerType), 15)
checkEvaluation(Cast(ts, LongType), 15)
checkEvaluation(Cast(ts, FloatType), 15.002f)
checkEvaluation(Cast(ts, DoubleType), 15.002)
checkEvaluation(Cast(Cast(ts, ShortType), TimestampType), ts1)
checkEvaluation(Cast(Cast(ts, IntegerType), TimestampType), ts1)
checkEvaluation(Cast(Cast(ts, LongType), TimestampType), ts1)
Expand All @@ -261,6 +261,9 @@ class ExpressionEvaluationSuite extends FunSuite {
checkEvaluation(Cast(Cast(millis.toDouble / 1000, TimestampType), DoubleType),
millis.toDouble / 1000)
checkEvaluation(Cast(Literal(BigDecimal(1)) cast TimestampType, DecimalType), 1)

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

0 comments on commit 18aacd3

Please sign in to comment.