Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2816#discussion_r18953740
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala ---
    @@ -301,17 +301,67 @@ class SqlParser extends AbstractSparkSQLParser {
         CAST ~ "(" ~> expression ~ (AS ~> dataType) <~ ")" ^^ { case exp ~ t 
=> Cast(exp, t) }
     
       protected lazy val literal: Parser[Literal] =
    -    ( numericLit ^^ {
    -        case i if i.toLong > Int.MaxValue => Literal(i.toLong)
    -        case i => Literal(i.toInt)
    -      }
    +    ( numericLiteral
         | NULL ^^^ Literal(null, NullType)
    -    | floatLit ^^ {case f => Literal(f.toDouble) }
    +    | booleanLiteral
         | stringLit ^^ {case s => Literal(s, StringType) }
         )
     
    +  protected lazy val booleanLiteral: Parser[Literal] =
    +    ( TRUE ^^^ Literal(true, BooleanType)
    +    | FALSE ^^^ Literal(false, BooleanType)
    +    )
    +
    +  protected lazy val numericLiteral: Parser[Literal] =
    +    signedNumericLiteral | unsignedNumericLiteral | floatLit ^^ { f => 
Literal(f.toDouble) }
    +
    +  protected lazy val sign: Parser[String] =
    +    "+" | "-"
    +
    +  protected lazy val signedNumericLiteral: Parser[Literal] =
    +    sign ~ numericLit  ^^ { case s ~ l => Literal(getProperTypeInt(s + l)) 
}
    +
    +
    +  protected lazy val unsignedNumericLiteral: Parser[Literal] =
    +    numericLit ^^ { n => Literal(getProperTypeInt(n)) }
    +
    +
    +  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 getProperTypeInt(value: String) = {
    +    val bigIntValue = BigDecimal(value)
    +
    +    if (value.startsWith("-")) {
    +      if (bigIntValue < intMin) {
    +        if (bigIntValue < longMin) {
    +          bigIntValue
    +        } else {
    +          value.toLong
    +        }
    +      } else {
    +        value.toInt
    +      }
    +    } else {
    +      if (bigIntValue > intMax) {
    +        if (bigIntValue > longMax) {
    +          bigIntValue
    +        } else {
    +          value.toLong
    +        }
    +      } else {
    +        value.toInt
    +      }
    +    }
    --- End diff --
    
    How about this:
    
    ```scala
    bigIntValue match {
      case v if v < longMin || v > longMax => v
      case v if v < intMin  || v > intMax  => v.toLong
      case v                               => v.toInt
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to