Skip to content
Prev Previous commit
Next Next commit
save
  • Loading branch information
AngersZhuuuu committed Apr 29, 2021
commit 616380e52783590d616159f8e871d3950cbd8890
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
case decimal: DecimalType => castToDecimalCode(from, decimal, ctx)
case TimestampType => castToTimestampCode(from, ctx)
case CalendarIntervalType => castToIntervalCode(from)
case DayTimeIntervalType => castToDayTimeIntervalCode(from, ctx)
case DayTimeIntervalType => castToDayTimeIntervalCode(from)
case BooleanType => castToBooleanCode(from)
case ByteType => castToByteCode(from, ctx)
case ShortType => castToShortCode(from, ctx)
Expand Down Expand Up @@ -1361,21 +1361,11 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit

}

private[this] def castToDayTimeIntervalCode(
from: DataType,
ctx: CodegenContext): CastFunction = from match {
private[this] def castToDayTimeIntervalCode(from: DataType): CastFunction = from match {
case StringType =>
val util = IntervalUtils.getClass.getCanonicalName.stripSuffix("$")
val longOpt = ctx.freshVariable("intOpt", classOf[Option[Long]])
(c, evPrim, evNull) =>
code"""
scala.Option<Long> $longOpt = $util.castStringToDTInterval($c).microseconds;
if ($longOpt.isDefined()) {
$evPrim = ((Long) $longOpt.get()).longValue();
} else {
$evNull = true;
}
""".stripMargin
code"$evPrim = $util.castStringToDTInterval($c).microseconds;"
}

private[this] def decimalToTimestampCode(d: ExprValue): Block = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,20 @@ object IntervalUtils {
def castStringToDTInterval(input: UTF8String): CalendarInterval = {
val intervalStr = input.trimAll().toString.toUpperCase(Locale.ROOT)
intervalStr match {
case dayTimePatternLegacy(_, _, _, _, _, _) =>
fromDayTimeString(intervalStr)
case daySecondStringPattern(_, prefixSign, _, suffixSign, _, _, _, _, _, _, _) =>
case daySecondNumPattern(_, _, _, _, _, _) =>
fromDayTimeString(intervalStr, DAY, SECOND)
case daySecondNumPattern(_, prefixSign, _, suffixSign, _, _, _, _, _, _, _) =>
val dtStr =
"^([+|-])".r.replaceAllIn(daySecondNumPattern.findFirstIn(intervalStr).get, "")
(prefixSign, suffixSign) match {
case ("-", "-") => fromDayTimeString(dtStr)
case ("-", _) => fromDayTimeString(s"-$dtStr")
case (_, _) => fromDayTimeString(dtStr)
case ("-", "-") => fromDayTimeString(dtStr, DAY, SECOND)
case ("-", _) => fromDayTimeString(s"-$dtStr", DAY, SECOND)
case (_, _) => fromDayTimeString(dtStr, DAY, SECOND)
}
case daySecondStringPattern(_, null, _, _, _, _, _, _, _, _, _) =>
val dtStr = daySecondNumPattern.findFirstIn(intervalStr).get
fromDayTimeString(dtStr)
println(dtStr)
fromDayTimeString(dtStr, DAY, SECOND)
case daySecondStringPattern(_, _, _, _, _, _, _, _, _, _, _) =>
throw new IllegalArgumentException(
s"Interval string must match day-time format of 'd h:m:s.n': ${input.toString}, " +
Expand Down