Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 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 @@ -18,6 +18,7 @@
package org.apache.spark.sql.catalyst.parser

import java.util.Locale
import java.util.concurrent.TimeUnit
import javax.xml.bind.DatatypeConverter

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -2302,12 +2303,24 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
}

/**
* Create a [[CalendarInterval]] literal expression. Two syntaxes are supported:
* Create a [[CalendarInterval]] or ANSI interval literal expression.
* Two syntaxes are supported:
* - multiple unit value pairs, for instance: interval 2 months 2 days.
* - from-to unit, for instance: interval '1-2' year to month.
*/
override def visitInterval(ctx: IntervalContext): Literal = withOrigin(ctx) {
Literal(parseIntervalLiteral(ctx), CalendarIntervalType)
val parsedInterval = parseIntervalLiteral(ctx)
if (SQLConf.get.legacyIntervalEnabled) {
Comment thread
MaxGekk marked this conversation as resolved.
Outdated
Literal(parsedInterval, CalendarIntervalType)
} else if (parsedInterval.months != 0) {
if (parsedInterval.days != 0 || parsedInterval.microseconds != 0) {
throw QueryParsingErrors.mixedIntervalError(ctx)
}
Literal(parsedInterval.months, YearMonthIntervalType)
} else {
val micros = IntervalUtils.getDuration(parsedInterval, TimeUnit.MICROSECONDS)
Literal(micros, DayTimeIntervalType)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.antlr.v4.runtime.ParserRuleContext
import org.apache.spark.sql.catalyst.parser.ParseException
import org.apache.spark.sql.catalyst.parser.SqlBaseParser._
import org.apache.spark.sql.catalyst.trees.Origin
import org.apache.spark.sql.internal.SQLConf

/**
* Object for grouping all error messages of the query parsing.
Expand Down Expand Up @@ -367,4 +368,10 @@ object QueryParsingErrors {
new ParseException("LOCAL is supported only with file: scheme", ctx)
}

def mixedIntervalError(ctx: IntervalContext): Throwable = {
new ParseException(
"Mixing of year-month and day-time fields is not allowed. " +

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does ANSI allow mixing daytime interval with same unit e.g. day and day

s"Set '${SQLConf.LEGACY_INTERVAL_ENABLED.key}' to true to enable the legacy interval type " +
"which supports mixed fields.", ctx)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,49 +291,47 @@ select timestamp '2019-01-01中文'
-- !query
select timestamp'2011-11-11 11:11:11' + interval '2' day
-- !query schema
struct<TIMESTAMP '2011-11-11 11:11:11' + INTERVAL '2 days':timestamp>
struct<TIMESTAMP '2011-11-11 11:11:11' + 172800000000:timestamp>
Comment thread
MaxGekk marked this conversation as resolved.
Outdated
-- !query output
2011-11-13 11:11:11


-- !query
select timestamp'2011-11-11 11:11:11' - interval '2' day
-- !query schema
struct<TIMESTAMP '2011-11-11 11:11:11' - INTERVAL '2 days':timestamp>
struct<TIMESTAMP '2011-11-11 11:11:11' - 172800000000:timestamp>
-- !query output
2011-11-09 11:11:11


-- !query
select date'2011-11-11 11:11:11' + interval '2' second
-- !query schema
struct<>
struct<DATE '2011-11-11' + 2000000:timestamp>
-- !query output
java.lang.IllegalArgumentException
requirement failed: Cannot add hours, minutes or seconds, milliseconds, microseconds to a date
2011-11-11 00:00:02


-- !query
select date'2011-11-11 11:11:11' - interval '2' second
-- !query schema
struct<>
struct<DATE '2011-11-11' - 2000000:timestamp>
-- !query output
java.lang.IllegalArgumentException
requirement failed: Cannot add hours, minutes or seconds, milliseconds, microseconds to a date
2011-11-10 23:59:58


-- !query
select '2011-11-11' - interval '2' day
-- !query schema
struct<2011-11-11 - INTERVAL '2 days':string>
struct<2011-11-11 - 172800000000:string>
-- !query output
2011-11-09 00:00:00


-- !query
select '2011-11-11 11:11:11' - interval '2' second
-- !query schema
struct<2011-11-11 11:11:11 - INTERVAL '2 seconds':string>
struct<2011-11-11 11:11:11 - 2000000:string>
-- !query output
2011-11-11 11:11:09

Expand All @@ -353,7 +351,7 @@ select 1 - interval '2' second
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve '1 + (- INTERVAL '2 seconds')' due to data type mismatch: argument 1 requires timestamp type, however, '1' is of int type.; line 1 pos 7
cannot resolve '1 + (- 2000000)' due to data type mismatch: argument 1 requires timestamp type, however, '1' is of int type.; line 1 pos 7


-- !query
Expand Down
Loading