You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/06/18 21:17:24 UTC

[GitHub] [spark] MaxGekk commented on a change in pull request #32940: [SPARK-35768][SQL] Take into account year-month interval fields in cast

MaxGekk commented on a change in pull request #32940:
URL: https://github.com/apache/spark/pull/32940#discussion_r653869032



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -108,18 +108,22 @@ object IntervalUtils {
 
   def castStringToYMInterval(
       input: UTF8String,
-      // TODO(SPARK-35768): Take into account year-month interval fields in cast
       startField: Byte,
       endField: Byte): Int = {
+
+    def truncatedMonth(month: String) : String = {
+      if (endField == YearMonthIntervalType.YEAR) "0" else month
+    }
+
     input.trimAll().toString match {
-      case yearMonthRegex("-", year, month) => toYMInterval(year, month, -1)
-      case yearMonthRegex(_, year, month) => toYMInterval(year, month, 1)
+      case yearMonthRegex("-", year, month) => toYMInterval(year, truncatedMonth(month), -1)
+      case yearMonthRegex(_, year, month) => toYMInterval(year, truncatedMonth(month), 1)
       case yearMonthLiteralRegex(firstSign, secondSign, year, month) =>

Review comment:
       The regex
   ```scala
     private val yearMonthLiteralRegex =
       (s"(?i)^INTERVAL\\s+([+|-])?'$yearMonthPatternString'\\s+YEAR\\s+TO\\s+MONTH$$").r
   ```
   doesn't support other year-month interval types, for instance `INTERVAL MONTH`. @cloud-fan Again, this is a duplicate of SQL parser logic.

##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -108,18 +108,22 @@ object IntervalUtils {
 
   def castStringToYMInterval(
       input: UTF8String,
-      // TODO(SPARK-35768): Take into account year-month interval fields in cast
       startField: Byte,
       endField: Byte): Int = {
+
+    def truncatedMonth(month: String) : String = {
+      if (endField == YearMonthIntervalType.YEAR) "0" else month
+    }
+
     input.trimAll().toString match {
-      case yearMonthRegex("-", year, month) => toYMInterval(year, month, -1)
-      case yearMonthRegex(_, year, month) => toYMInterval(year, month, 1)
+      case yearMonthRegex("-", year, month) => toYMInterval(year, truncatedMonth(month), -1)
+      case yearMonthRegex(_, year, month) => toYMInterval(year, truncatedMonth(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)
+          case ("-", "-") => toYMInterval(year, truncatedMonth(month), 1)
+          case ("-", _) => toYMInterval(year, truncatedMonth(month), -1)
+          case (_, "-") => toYMInterval(year, truncatedMonth(month), -1)
+          case (_, _) => toYMInterval(year, truncatedMonth(month), 1)
         }
       case _ => throw new IllegalArgumentException(

Review comment:
       Could you change the error message, please. It should reflect `startField` and `endField`.

##########
File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
##########
@@ -651,4 +652,54 @@ class CastSuite extends CastSuiteBase {
         checkEvaluation(cast(cast(interval, StringType), YearMonthIntervalType()), period)
       }
   }
+
+  test("SPARK-35768: Take into account year-month interval fields in cast") {
+    checkEvaluation(cast(Literal.create("1-1"),
+      YearMonthIntervalType(YEAR, YEAR)), 12)
+    checkEvaluation(cast(Literal.create("1-1"),
+      YearMonthIntervalType(YEAR, MONTH)), 13)
+    checkEvaluation(cast(Literal.create("1-1"),
+      YearMonthIntervalType(MONTH, MONTH)), 13)
+    checkEvaluation(cast(Literal.create("-1-1"),
+      YearMonthIntervalType(YEAR, YEAR)), -12)
+    checkEvaluation(cast(Literal.create("-1-1"),
+      YearMonthIntervalType(YEAR, MONTH)), -13)
+    checkEvaluation(cast(Literal.create("-1-1"),
+      YearMonthIntervalType(MONTH, MONTH)), -13)
+
+    checkEvaluation(cast(Literal.create("INTERVAL '1-1' YEAR TO MONTH"),

Review comment:
       Could you rewrite this like a loop over a sequence of
   (interval string, target type) -> <expected months>

##########
File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
##########
@@ -651,4 +652,54 @@ class CastSuite extends CastSuiteBase {
         checkEvaluation(cast(cast(interval, StringType), YearMonthIntervalType()), period)
       }
   }
+
+  test("SPARK-35768: Take into account year-month interval fields in cast") {

Review comment:
       Need to test `INTERVAL '...' YEAR` and `INTERVAL '...' MONTH` as well.




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org