Peng-Lei commented on a change in pull request #34494:
URL: https://github.com/apache/spark/pull/34494#discussion_r750947143



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -1253,4 +1255,115 @@ object IntervalUtils {
     }
     intervalString
   }
+
+  def intToYearMonthInterval(v: Int, endField: Byte): Int = {
+    endField match {
+      case YEAR =>
+        try {
+          Math.multiplyExact(v, MONTHS_PER_YEAR)
+        } catch {
+          case _: ArithmeticException =>
+            throw QueryExecutionErrors.castingCauseOverflowError(v, 
YM(endField).catalogString)
+        }
+      case MONTH => v
+    }
+  }
+
+  def longToYearMonthInterval(v: Long, endField: Byte): Int = {
+    val vInt = v.toInt
+    if (v != vInt) {
+      throw QueryExecutionErrors.castingCauseOverflowError(v, 
YM(endField).catalogString)
+    }
+    intToYearMonthInterval(vInt, endField)
+  }
+
+  def yearMonthIntervalToInt(v: Int, endFiled: Byte): Int = {
+    endFiled match {
+      case YEAR => v / MONTHS_PER_YEAR
+      case MONTH => v
+    }
+  }
+
+  def yearMonthIntervalToShort(v: Int, endFiled: Byte): Short = {
+    val vInt = yearMonthIntervalToInt(v, endFiled)
+    val vShort = vInt.toShort
+    if (vInt != vShort) {
+      throw QueryExecutionErrors.castingCauseOverflowError(v, 
ShortType.catalogString);

Review comment:
       Seems more intuitive that print `v` with interval format. A small burden 
is the need to add a parameter `startField` to build the interval format. eg 
`INTERVAL 'v' startField TO endField`.




-- 
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.

To unsubscribe, e-mail: [email protected]

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