Skip to content

Commit

Permalink
[SPARK-17013][SQL] Parse negative numeric literals
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?
This patch updates the SQL parser to parse negative numeric literals as numeric literals, instead of unary minus of positive literals.

This allows the parser to parse the minimal value for each data type, e.g. "-32768S".

## How was this patch tested?
Updated test cases.

Author: petermaxlee <[email protected]>

Closes apache#14608 from petermaxlee/SPARK-17013.
  • Loading branch information
petermaxlee authored and rxin committed Aug 12, 2016
1 parent abff92b commit 00e103a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -625,13 +625,13 @@ quotedIdentifier
;

number
: DECIMAL_VALUE #decimalLiteral
| SCIENTIFIC_DECIMAL_VALUE #scientificDecimalLiteral
| INTEGER_VALUE #integerLiteral
| BIGINT_LITERAL #bigIntLiteral
| SMALLINT_LITERAL #smallIntLiteral
| TINYINT_LITERAL #tinyIntLiteral
| DOUBLE_LITERAL #doubleLiteral
: MINUS? DECIMAL_VALUE #decimalLiteral
| MINUS? SCIENTIFIC_DECIMAL_VALUE #scientificDecimalLiteral
| MINUS? INTEGER_VALUE #integerLiteral
| MINUS? BIGINT_LITERAL #bigIntLiteral
| MINUS? SMALLINT_LITERAL #smallIntLiteral
| MINUS? TINYINT_LITERAL #tinyIntLiteral
| MINUS? DOUBLE_LITERAL #doubleLiteral
;

nonReserved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ case class UnaryMinus(child: Expression) extends UnaryExpression
}
}

override def sql: String = s"(-${child.sql})"
override def sql: String = s"(- ${child.sql})"
}

@ExpressionDescription(
Expand All @@ -76,7 +76,7 @@ case class UnaryPositive(child: Expression)

protected override def nullSafeEval(input: Any): Any = input

override def sql: String = s"(+${child.sql})"
override def sql: String = s"(+ ${child.sql})"
}

/**
Expand Down
26 changes: 13 additions & 13 deletions sql/core/src/test/resources/sql-tests/results/arithmetic.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- !query 0
select -100
-- !query 0 schema
struct<(-100):int>
struct<-100:int>
-- !query 0 output
-100

Expand All @@ -21,7 +21,7 @@ struct<230:int>
-- !query 2
select -5.2
-- !query 2 schema
struct<(-5.2):decimal(2,1)>
struct<-5.2:decimal(2,1)>
-- !query 2 output
-5.2

Expand All @@ -37,63 +37,63 @@ struct<6.8:double>
-- !query 4
select -key, +key from testdata where key = 2
-- !query 4 schema
struct<(-key):int,key:int>
struct<(- key):int,key:int>
-- !query 4 output
-2 2


-- !query 5
select -(key + 1), - key + 1, +(key + 5) from testdata where key = 1
-- !query 5 schema
struct<(-(key + 1)):int,((-key) + 1):int,(key + 5):int>
struct<(- (key + 1)):int,((- key) + 1):int,(key + 5):int>
-- !query 5 output
-2 0 6


-- !query 6
select -max(key), +max(key) from testdata
-- !query 6 schema
struct<(-max(key)):int,max(key):int>
struct<(- max(key)):int,max(key):int>
-- !query 6 output
-100 100


-- !query 7
select - (-10)
-- !query 7 schema
struct<(-(-10)):int>
struct<(- -10):int>
-- !query 7 output
10


-- !query 8
select + (-key) from testdata where key = 32
-- !query 8 schema
struct<(-key):int>
struct<(- key):int>
-- !query 8 output
-32


-- !query 9
select - (+max(key)) from testdata
-- !query 9 schema
struct<(-max(key)):int>
struct<(- max(key)):int>
-- !query 9 output
-100


-- !query 10
select - - 3
-- !query 10 schema
struct<(-(-3)):int>
struct<(- -3):int>
-- !query 10 output
3


-- !query 11
select - + 20
-- !query 11 schema
struct<(-20):int>
struct<(- 20):int>
-- !query 11 output
-20

Expand All @@ -109,15 +109,15 @@ struct<100:int>
-- !query 13
select - - max(key) from testdata
-- !query 13 schema
struct<(-(-max(key))):int>
struct<(- (- max(key))):int>
-- !query 13 output
100


-- !query 14
select + - key from testdata where key = 33
-- !query 14 schema
struct<(-key):int>
struct<(- key):int>
-- !query 14 output
-33

Expand Down Expand Up @@ -173,6 +173,6 @@ struct<(5 % 3):int>
-- !query 21
select pmod(-7, 3)
-- !query 21 schema
struct<pmod((-7), 3):int>
struct<pmod(-7, 3):int>
-- !query 21 output
2
44 changes: 13 additions & 31 deletions sql/core/src/test/resources/sql-tests/results/literals.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@ struct<1:tinyint>
-- !query 3
select 127Y, -128Y
-- !query 3 schema
struct<>
struct<127:tinyint,-128:tinyint>
-- !query 3 output
org.apache.spark.sql.catalyst.parser.ParseException

Value out of range. Value:"128" Radix:10(line 1, pos 14)

== SQL ==
select 127Y, -128Y
--------------^^^
127 -128


-- !query 4
Expand Down Expand Up @@ -65,15 +59,9 @@ struct<1:smallint>
-- !query 6
select 32767S, -32768S
-- !query 6 schema
struct<>
struct<32767:smallint,-32768:smallint>
-- !query 6 output
org.apache.spark.sql.catalyst.parser.ParseException

Value out of range. Value:"32768" Radix:10(line 1, pos 16)

== SQL ==
select 32767S, -32768S
----------------^^^
32767 -32768


-- !query 7
Expand Down Expand Up @@ -101,15 +89,9 @@ struct<1:bigint,2147483648:bigint>
-- !query 9
select 9223372036854775807L, -9223372036854775808L
-- !query 9 schema
struct<>
struct<9223372036854775807:bigint,-9223372036854775808:bigint>
-- !query 9 output
org.apache.spark.sql.catalyst.parser.ParseException

For input string: "9223372036854775808"(line 1, pos 30)

== SQL ==
select 9223372036854775807L, -9223372036854775808L
------------------------------^^^
9223372036854775807 -9223372036854775808


-- !query 10
Expand All @@ -129,31 +111,31 @@ select 9223372036854775808L
-- !query 11
select 1, -1
-- !query 11 schema
struct<1:int,(-1):int>
struct<1:int,-1:int>
-- !query 11 output
1 -1


-- !query 12
select 2147483647, -2147483648
-- !query 12 schema
struct<2147483647:int,(-2147483648):bigint>
struct<2147483647:int,-2147483648:int>
-- !query 12 output
2147483647 -2147483648


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


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

Expand Down Expand Up @@ -193,7 +175,7 @@ struct<1.0:double,1.2:double,1.0E10:double,150000.0:double,0.1:double,0.1:double
-- !query 18
select -1D, -1.2D, -1e10, -1.5e5, -.10D, -0.10D, -.1e5
-- !query 18 schema
struct<(-1.0):double,(-1.2):double,(-1.0E10):double,(-150000.0):double,(-0.1):double,(-0.1):double,(-10000.0):double>
struct<-1.0:double,-1.2:double,-1.0E10:double,-150000.0:double,-0.1:double,-0.1:double,-10000.0:double>
-- !query 18 output
-1.0 -1.2 -1.0E10 -150000.0 -0.1 -0.1 -10000.0

Expand All @@ -215,15 +197,15 @@ select .e3
-- !query 20
select 1E309, -1E309
-- !query 20 schema
struct<Infinity:double,(-Infinity):double>
struct<Infinity:double,-Infinity:double>
-- !query 20 output
Infinity -Infinity


-- !query 21
select 0.3, -0.8, .5, -.18, 0.1111, .1111
-- !query 21 schema
struct<0.3:decimal(1,1),(-0.8):decimal(1,1),0.5:decimal(1,1),(-0.18):decimal(2,2),0.1111:decimal(4,4),0.1111:decimal(4,4)>
struct<0.3:decimal(1,1),-0.8:decimal(1,1),0.5:decimal(1,1),-0.18:decimal(2,2),0.1111:decimal(4,4),0.1111:decimal(4,4)>
-- !query 21 output
0.3 -0.8 0.5 -0.18 0.1111 0.1111

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class ExpressionSQLBuilderSuite extends SQLBuilderTest {
checkSQL('a.int / 'b.int, "(`a` / `b`)")
checkSQL('a.int % 'b.int, "(`a` % `b`)")

checkSQL(-'a.int, "(-`a`)")
checkSQL(-('a.int + 'b.int), "(-(`a` + `b`))")
checkSQL(-'a.int, "(- `a`)")
checkSQL(-('a.int + 'b.int), "(- (`a` + `b`))")
}

test("window specification") {
Expand Down

0 comments on commit 00e103a

Please sign in to comment.