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 @@ -1769,7 +1769,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
* {{{
* [TYPE] '[VALUE]'
* }}}
* Currently Date, Timestamp, Interval and Binary typed literals are supported.
* Currently Date, Timestamp, Interval, Binary and INTEGER typed literals are supported.
*/
override def visitTypeConstructor(ctx: TypeConstructorContext): Literal = withOrigin(ctx) {
val value = string(ctx.STRING)
Expand Down Expand Up @@ -1799,6 +1799,16 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
case "X" =>
val padding = if (value.length % 2 != 0) "0" else ""
Literal(DatatypeConverter.parseHexBinary(padding + value))
case "INTEGER" =>
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, how many such typed literals are there left?

val i = try {
value.toInt
} catch {
case e: NumberFormatException =>
val ex = new ParseException(s"Cannot parse the Int value: $value, $e", ctx)
ex.setStackTrace(e.getStackTrace)
throw ex
}
Literal(i, IntegerType)
case other =>
throw new ParseException(s"Literals of type '$other' are currently not supported.", ctx)
}
Expand Down
5 changes: 5 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/literals.sql
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,8 @@ select map(1, interval 1 day, 2, interval 3 week);
-- typed interval expression
select interval 'interval 3 year 1 hour';
select interval '3 year 1 hour';

-- typed integer expression
select integer '7';
select integer'7';
select integer '2147483648';
32 changes: 31 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/literals.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 59
-- Number of queries: 62


-- !query 0
Expand Down Expand Up @@ -542,3 +542,33 @@ select interval '3 year 1 hour'
struct<interval 3 years 1 hours:interval>
-- !query 58 output
interval 3 years 1 hours


-- !query 59
select integer '7'
-- !query 59 schema
struct<7:int>
-- !query 59 output
7


-- !query 60
select integer'7'
-- !query 60 schema
struct<7:int>
-- !query 60 output
7


-- !query 61
select integer '2147483648'
-- !query 61 schema
struct<>
-- !query 61 output
org.apache.spark.sql.catalyst.parser.ParseException

Cannot parse the Int value: 2147483648, java.lang.NumberFormatException: For input string: "2147483648"(line 1, pos 7)

== SQL ==
select integer '2147483648'
-------^^^
Expand Down