Skip to content

Commit

Permalink
Let BigDecimal do checking type compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Nov 11, 2014
1 parent 3c2cff4 commit 1bdc69d
Showing 1 changed file with 3 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,13 @@ class SqlParser extends AbstractSparkSQLParser {
| floatLit ^^ { f => Literal(f.toDouble) }
)

private val longMax = BigDecimal(s"${Long.MaxValue}")
private val longMin = BigDecimal(s"${Long.MinValue}")
private val intMax = BigDecimal(s"${Int.MaxValue}")
private val intMin = BigDecimal(s"${Int.MinValue}")

private def toNarrowestIntegerType(value: String) = {
val bigIntValue = BigDecimal(value)

bigIntValue match {
case v if v < longMin || v > longMax => v
case v if v < intMin || v > intMax => v.toLong
case v => v.toInt
case v if bigIntValue.isValidInt => v.toIntExact
case v if bigIntValue.isValidLong => v.toLongExact
case v => v
}
}

Expand Down

0 comments on commit 1bdc69d

Please sign in to comment.