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 @@ -1688,7 +1688,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
* {{{
* [TYPE] '[VALUE]'
* }}}
* Currently Date, Timestamp and Binary typed literals are supported.
* Currently Date, Timestamp, Interval and Binary typed literals are supported.
*/
override def visitTypeConstructor(ctx: TypeConstructorContext): Literal = withOrigin(ctx) {
val value = string(ctx.STRING)
Expand All @@ -1704,6 +1704,8 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
case "TIMESTAMP" =>
val zoneId = getZoneId(SQLConf.get.sessionLocalTimeZone)
toLiteral(stringToTimestamp(_, zoneId), TimestampType)
case "INTERVAL" =>
Literal(CalendarInterval.fromString(value), CalendarIntervalType)
case "X" =>
val padding = if (value.length % 2 != 0) "0" else ""
Literal(DatatypeConverter.parseHexBinary(padding + value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,12 @@ class ExpressionParserSuite extends AnalysisTest {
Literal(Timestamp.valueOf("2016-03-11 20:54:00.000")))
intercept("timestamP '2016-33-11 20:54:00.000'")

// Interval.
assertEqual("InterVal 'interval 3 month 1 hour'",
Literal(CalendarInterval.fromString("interval 3 month 1 hour")))
assertEqual("Interval 'interval 3 monthsss 1 hoursss'",
Literal(null, CalendarIntervalType))

// Binary.
assertEqual("X'A'", Literal(Array(0x0a).map(_.toByte)))
assertEqual("x'A10C'", Literal(Array(0xa1, 0x0c).map(_.toByte)))
Expand Down
4 changes: 4 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 @@ -108,3 +108,7 @@ SELECT 3.14, -3.14, 3.14e8, 3.14e-8, -3.14e8, -3.14e-8, 3.14e+8, 3.14E8, 3.14E-8

-- map + interval test
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';
18 changes: 17 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: 44
-- Number of queries: 46


-- !query 0
Expand Down Expand Up @@ -422,3 +422,19 @@ select map(1, interval 1 day, 2, interval 3 week)
struct<map(1, interval 1 days, 2, interval 3 weeks):map<int,interval>>
-- !query 43 output
{1:interval 1 days,2:interval 3 weeks}


-- !query 44
select interval 'interval 3 year 1 hour'
-- !query 44 schema
struct<interval 3 years 1 hours:interval>
-- !query 44 output
interval 3 years 1 hours


-- !query 45
select interval '3 year 1 hour'
-- !query 45 schema
struct<CAST(NULL AS INTERVAL):interval>
-- !query 45 output
NULL
Expand Down