diff --git a/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 b/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 index ba65f2a889a9..c6a4740094fc 100644 --- a/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 +++ b/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 @@ -627,6 +627,7 @@ quotedIdentifier number : DECIMAL_VALUE #decimalLiteral | SCIENTIFIC_DECIMAL_VALUE #scientificDecimalLiteral + | MINUS INTEGER_VALUE #negativeIntegerLiteral | INTEGER_VALUE #integerLiteral | BIGINT_LITERAL #bigIntLiteral | SMALLINT_LITERAL #smallIntLiteral diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala index c7fdc287d199..3a27f3505456 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala @@ -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()) } } diff --git a/sql/core/src/test/resources/sql-tests/results/number-format.sql.out b/sql/core/src/test/resources/sql-tests/results/number-format.sql.out index 82a1d39c0a0b..65eac8a61da3 100644 --- a/sql/core/src/test/resources/sql-tests/results/number-format.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/number-format.sql.out @@ -5,7 +5,7 @@ -- !query 0 select 1, -1 -- !query 0 schema -struct<1:int,(-1):int> +struct<1:int,-1:int> -- !query 0 output 1 -1 @@ -13,7 +13,7 @@ struct<1:int,(-1):int> -- !query 1 select 2147483648, -2147483649 -- !query 1 schema -struct<2147483648:bigint,(-2147483649):bigint> +struct<2147483648:bigint,-2147483649:bigint> -- !query 1 output 2147483648 -2147483649 @@ -21,7 +21,7 @@ struct<2147483648:bigint,(-2147483649):bigint> -- !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 @@ -29,7 +29,7 @@ struct<9223372036854775807:bigint,(-9223372036854775808):decimal(19,0)> -- !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