Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ quotedIdentifier
number
: DECIMAL_VALUE #decimalLiteral
| SCIENTIFIC_DECIMAL_VALUE #scientificDecimalLiteral
| MINUS INTEGER_VALUE #negativeIntegerLiteral
| INTEGER_VALUE #integerLiteral
Copy link
Contributor

@hvanhovell hvanhovell Aug 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add change this case into: MINUS? INTEGER_LITERAL #integerLiteral. That also works and this would save quite a bit of code in the AstBuilder.

| BIGINT_LITERAL #bigIntLiteral
| SMALLINT_LITERAL #smallIntLiteral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1255,17 +1255,31 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging {
}
}

/**
* Create a negative integral literal expression. The code selects the most narrow integral type
* possible, either a BigDecimal, a Long or an Integer is returned.
*/
override def visitNegativeIntegerLiteral(
ctx: NegativeIntegerLiteralContext): Literal = withOrigin(ctx) {
createIntegralLiteral(ctx.getText)
}

/**
* Create an integral literal expression. The code selects the most narrow integral type
* possible, either a BigDecimal, a Long or an Integer is returned.
*/
override def visitIntegerLiteral(ctx: IntegerLiteralContext): Literal = withOrigin(ctx) {
BigDecimal(ctx.getText) match {
case v if v.isValidInt =>
Literal(v.intValue())
case v if v.isValidLong =>
Literal(v.longValue())
case v => Literal(v.underlying())
createIntegralLiteral(ctx.getText)
}

private def createIntegralLiteral(s: String): Literal = {
val bigDecimal = BigDecimal(s)
if (bigDecimal.isValidInt) {
Literal(bigDecimal.intValue())
} else if (bigDecimal.isValidLong) {
Literal(bigDecimal.longValue())
} else {
Literal(bigDecimal.underlying())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
-- !query 0
select 1, -1
-- !query 0 schema
struct<1:int,(-1):int>
struct<1:int,-1:int>
-- !query 0 output
1 -1


-- !query 1
select 2147483648, -2147483649
-- !query 1 schema
struct<2147483648:bigint,(-2147483649):bigint>
struct<2147483648:bigint,-2147483649:bigint>
-- !query 1 output
2147483648 -2147483649


-- !query 2
select 9223372036854775807, -9223372036854775808
-- !query 2 schema
struct<9223372036854775807:bigint,(-9223372036854775808):decimal(19,0)>
struct<9223372036854775807:bigint,-9223372036854775808:bigint>
-- !query 2 output
9223372036854775807 -9223372036854775808


-- !query 3
select 9223372036854775808, -9223372036854775809
-- !query 3 schema
struct<9223372036854775808:decimal(19,0),(-9223372036854775809):decimal(19,0)>
struct<9223372036854775808:decimal(19,0),-9223372036854775809:decimal(19,0)>
-- !query 3 output
9223372036854775808 -9223372036854775809

Expand Down