Skip to content

Commit

Permalink
Fix bug of converting from String
Browse files Browse the repository at this point in the history
  • Loading branch information
chenghao-intel committed Apr 3, 2014
1 parent 6fc8100 commit e529168
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {
}

def castToDouble: Any => Double = child.dataType match {
case StringType => nullOrCast[String, Double](_, s => try s.toInt catch {
case StringType => nullOrCast[String, Double](_, s => try s.toDouble catch {
case _: NFE => null.asInstanceOf[Int]
})
case BooleanType => nullOrCast[Boolean, Double](_, b => if(b) 1 else 0)
Expand All @@ -163,7 +163,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {
}

def castToFloat: Any => Float = child.dataType match {
case StringType => nullOrCast[String, Float](_, s => try s.toInt catch {
case StringType => nullOrCast[String, Float](_, s => try s.toFloat catch {
case _: NFE => null.asInstanceOf[Int]
})
case BooleanType => nullOrCast[Boolean, Float](_, b => if(b) 1 else 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ class ExpressionEvaluationSuite extends FunSuite {
checkEvaluation(Literal(false) cast IntegerType, 0)
checkEvaluation(Cast(Literal(1) cast BooleanType, IntegerType), 1)
checkEvaluation(Cast(Literal(0) cast BooleanType, IntegerType), 0)
checkEvaluation("23" cast DoubleType, 23)
checkEvaluation("23" cast IntegerType, 23)
checkEvaluation("23" cast FloatType, 23)
checkEvaluation("23" cast DecimalType, 23)
checkEvaluation("23" cast ByteType, 23)
checkEvaluation("23" cast ShortType, 23)

intercept[Exception] {evaluate(Literal(1) cast BinaryType, null)}
}
Expand Down

0 comments on commit e529168

Please sign in to comment.