Skip to content
Closed
Changes from 3 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 @@ -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 unquotedYearMonthRegex = "([+|-])?(\\d+)-(\\d+)"
private val quotedYearMonthPattern = (s"^$unquotedYearMonthRegex$$").r
Copy link
Contributor

@cloud-fan cloud-fan May 7, 2021

Choose a reason for hiding this comment

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

I don't really see what "quoted" means here...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't really see what "quoted" means here...

he may want to mean it without ^ and $

Copy link
Contributor

Choose a reason for hiding this comment

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

that's not quoted. To me it's more like regex string vs compiled regex.

How about yearMonthPatternString, yearMonthRegex and yearMontnLiteralRegex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Look good, done

private val yearMonthLiteralPattern =
(s"(?i)^INTERVAL\\s+([+|-])?'$unquotedYearMonthRegex'\\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 All @@ -150,11 +151,11 @@ object IntervalUtils {
}
}

private val unquotedDaySecondPattern =
private val unquotedDaySecondRegex =
"([+|-])?(\\d+) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})(\\.\\d{1,9})?"
private val quotedDaySecondPattern = (s"^$unquotedDaySecondPattern$$").r
private val quotedDaySecondPattern = (s"^$unquotedDaySecondRegex$$").r
private val daySecondLiteralPattern =
(s"(?i)^INTERVAL\\s+([+|-])?\\'$unquotedDaySecondPattern\\'\\s+DAY\\s+TO\\s+SECOND$$").r
(s"(?i)^INTERVAL\\s+([+|-])?\\'$unquotedDaySecondRegex\\'\\s+DAY\\s+TO\\s+SECOND$$").r

def castStringToDTInterval(input: UTF8String): Long = {
def secondAndMicro(second: String, micro: String): String = {
Expand Down