cloud-fan commented on a change in pull request #32940:
URL: https://github.com/apache/spark/pull/32940#discussion_r657268568



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -105,25 +105,62 @@ object IntervalUtils {
   private val yearMonthRegex = (s"^$yearMonthPatternString$$").r
   private val yearMonthLiteralRegex =
     
(s"(?i)^INTERVAL\\s+([+|-])?'$yearMonthPatternString'\\s+YEAR\\s+TO\\s+MONTH$$").r
+  private val yearMonthIndividualLiteralRegex =
+    (s"(?i)^INTERVAL\\s+([+|-])?'([+|-])?(\\d+)'\\s+(YEAR|MONTH)$$").r
+
+  private def getSign(firstSign: String, secondSign: String): Int = {
+    (firstSign, secondSign) match {
+      case ("-", "-") => 1
+      case ("-", _) => -1
+      case (_, "-") => -1
+      case (_, _) => 1
+    }
+  }
 
   def castStringToYMInterval(
       input: UTF8String,
-      // TODO(SPARK-35768): Take into account year-month interval fields in 
cast
       startField: Byte,
       endField: Byte): Int = {
+
+    val supportedFormat = Map(
+      (YM.YEAR, YM.MONTH) -> Seq("[+|-]y-m", "INTERVAL [+|-]'[+|-]y-m' YEAR TO 
MONTH"),
+      (YM.YEAR, YM.YEAR) -> Seq("INTERVAL [+|-]'[+|-]y' YEAR"),
+      (YM.MONTH, YM.MONTH) -> Seq("INTERVAL [+|-]'[+|-]m' MONTH")
+    )
+
+    def checkStringIntervalType(targetStartField: Byte, targetEndField: Byte): 
Unit = {
+      if (startField != targetStartField || endField != targetEndField) {
+        throw new IllegalArgumentException(s"Interval string does not match 
year-month format of " +
+          s"${supportedFormat((targetStartField, targetStartField))
+            .map(format => s"`$format`").mkString(", ")} " +
+          s"when cast to ${YM(startField, endField).typeName}: 
${input.toString}")
+      }
+    }
+
     input.trimAll().toString match {
-      case yearMonthRegex("-", year, month) => toYMInterval(year, month, -1)
-      case yearMonthRegex(_, year, month) => toYMInterval(year, month, 1)
+      case yearMonthRegex("-", year, month) =>
+        checkStringIntervalType(YM.YEAR, YM.MONTH)
+        toYMInterval(year, month, -1)
+      case yearMonthRegex(_, year, month) =>
+        checkStringIntervalType(YM.YEAR, YM.MONTH)
+        toYMInterval(year, month, 1)
       case yearMonthLiteralRegex(firstSign, secondSign, year, month) =>
-        (firstSign, secondSign) match {
-          case ("-", "-") => toYMInterval(year, month, 1)
-          case ("-", _) => toYMInterval(year, month, -1)
-          case (_, "-") => toYMInterval(year, month, -1)
-          case (_, _) => toYMInterval(year, month, 1)
+        checkStringIntervalType(YM.YEAR, YM.MONTH)
+        toYMInterval(year, month, getSign(firstSign, secondSign))
+      case yearMonthIndividualLiteralRegex(firstSign, secondSign, value, 
suffix) =>
+        val sign = getSign(firstSign, secondSign)
+        if ("YEAR".equalsIgnoreCase(suffix)) {
+          checkStringIntervalType(YM.YEAR, YM.YEAR)
+          sign * Math.toIntExact(value.toLong * MONTHS_PER_YEAR)

Review comment:
       or we can create a new method to do the try-catch and reuse it.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to