Skip to content
Closed
Changes from 1 commit
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
Next Next commit
[SPARK-35111][SQL][FOLLOWUP] Support Cast string to year-month interval
  • Loading branch information
AngersZhuuuu committed May 5, 2021
commit 09a7479ef1ca09d9d2c8c4329b1eda5934a0ea39
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ object IntervalUtils {
result
}

private val yearMonthPattern = "^([+|-])?(\\d+)-(\\d+)$".r
private val yearMonthStringPattern =
"(?i)^(INTERVAL\\s+)([+|-])?(')([+|-])?(\\d+)-(\\d+)(')(\\s+YEAR\\s+TO\\s+MONTH)$".r
private val unquotedYearMonthPattern = "^([+|-])?(\\d+)-(\\d+)$".r
private val quotedYearMonthPattern = (s"^$unquotedYearMonthPattern$$").r
private val yearMonthLiteralPattern =
"(?i)^INTERVAL\\s+([+|-])?'([+|-])?(\\d+)-(\\d+)'\\s+YEAR\\s+TO\\s+MONTH$".r

def castStringToYMInterval(input: UTF8String): Int = {
input.trimAll().toString match {
case yearMonthPattern("-", year, month) => toYMInterval(year, month, -1)
case yearMonthPattern(_, year, month) => toYMInterval(year, month, 1)
case yearMonthStringPattern(_, firstSign, _, secondSign, year, month, _, _) =>
case quotedYearMonthPattern("-", year, month) => toYMInterval(year, month, -1)
case quotedYearMonthPattern(_, year, month) => toYMInterval(year, month, 1)
case yearMonthLiteralPattern(firstSign, secondSign, year, month) =>
(firstSign, secondSign) match {
case ("-", "-") => toYMInterval(year, month, 1)
case ("-", _) => toYMInterval(year, month, -1)
Expand All @@ -128,9 +129,9 @@ object IntervalUtils {
def fromYearMonthString(input: String): CalendarInterval = {
require(input != null, "Interval year-month string must be not null")
input.trim match {
case yearMonthPattern("-", yearStr, monthStr) =>
case quotedYearMonthPattern("-", yearStr, monthStr) =>
new CalendarInterval(toYMInterval(yearStr, monthStr, -1), 0, 0)
case yearMonthPattern(_, yearStr, monthStr) =>
case quotedYearMonthPattern(_, yearStr, monthStr) =>
new CalendarInterval(toYMInterval(yearStr, monthStr, 1), 0, 0)
case _ =>
throw new IllegalArgumentException(
Expand Down