Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2816#discussion_r19001551
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala ---
    @@ -301,33 +301,75 @@ 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)
    -      }
    -    | NULL ^^^ Literal(null, NullType)
    -    | floatLit ^^ {case f => Literal(f.toDouble) }
    +    ( signedNumericLiteral
    +    | unsignedNumericLiteral
    +    | booleanLiteral
         | stringLit ^^ {case s => Literal(s, StringType) }
    +    | NULL ^^^ Literal(null, NullType)
    +    )
    +
    +  protected lazy val booleanLiteral: Parser[Literal] =
    +    ( TRUE ^^^ Literal(true, BooleanType)
    +    | FALSE ^^^ Literal(false, BooleanType)
    +    )
    +
    +  protected lazy val numericLiteral: Parser[Literal] =
    +    signedNumericLiteral | unsignedNumericLiteral
    +
    +  protected lazy val sign: Parser[String] =
    +    "+" | "-"
    +
    +  protected lazy val signedNumericLiteral: Parser[Literal] =
    +    ( sign ~ numericLit  ^^ { case s ~ l => 
Literal(toNarrowestIntegerType(s + l)) }
    +    | sign ~ floatLit ^^ { case s ~ f => Literal((s + f).toDouble) }
    +    )
    +
    +  protected lazy val unsignedNumericLiteral: Parser[Literal] =
    +    ( numericLit ^^ { n => Literal(toNarrowestIntegerType(n)) }
    +    | 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
    +    }
    +  }
    +
       protected lazy val floatLit: Parser[String] =
    -    elem("decimal", _.isInstanceOf[lexical.FloatLit]) ^^ (_.chars)
    +    ( "." ~> unsignedNumericLiteral ^^ { u => "0." + u }
    +    | elem("decimal", _.isInstanceOf[lexical.FloatLit]) ^^ (_.chars)
    +    )
     
       protected lazy val baseExpression: PackratParser[Expression] =
    --- End diff --
    
    Now the `baseExpression` don't have recursion explicitly, we can define it 
as normal `Parser`, and define `primary` as `PackratParser`


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to