AngersZhuuuu commented on a change in pull request #32266:
URL: https://github.com/apache/spark/pull/32266#discussion_r622806738
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
##########
@@ -1774,6 +1774,51 @@ class CastSuite extends CastSuiteBase {
assert(e3.contains("Casting 2147483648 to int causes overflow"))
}
}
+
+ test("SPARK-35111: Cast string to year-month interval") {
+ checkEvaluation(cast(Literal.create("INTERVAL '1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("0-0"), YearMonthIntervalType), 0)
+ checkEvaluation(cast(Literal.create("INTERVAL '-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL -'-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'+1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("-1-0"), YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL '10-1' YEAR TO MONTH"),
+ YearMonthIntervalType), 121)
+ checkEvaluation(cast(Literal.create("10-1"), YearMonthIntervalType), 121)
+ checkEvaluation(cast(Literal.create("interval '10-1' year TO MONTH"),
Review comment:
Done
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
##########
@@ -1774,6 +1774,51 @@ class CastSuite extends CastSuiteBase {
assert(e3.contains("Casting 2147483648 to int causes overflow"))
}
}
+
+ test("SPARK-35111: Cast string to year-month interval") {
+ checkEvaluation(cast(Literal.create("INTERVAL '1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("0-0"), YearMonthIntervalType), 0)
+ checkEvaluation(cast(Literal.create("INTERVAL '-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL -'-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'+1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("-1-0"), YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL '10-1' YEAR TO MONTH"),
+ YearMonthIntervalType), 121)
+ checkEvaluation(cast(Literal.create("10-1"), YearMonthIntervalType), 121)
Review comment:
Done
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
##########
@@ -1774,6 +1774,51 @@ class CastSuite extends CastSuiteBase {
assert(e3.contains("Casting 2147483648 to int causes overflow"))
}
}
+
+ test("SPARK-35111: Cast string to year-month interval") {
+ checkEvaluation(cast(Literal.create("INTERVAL '1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("0-0"), YearMonthIntervalType), 0)
+ checkEvaluation(cast(Literal.create("INTERVAL '-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL -'-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'-1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'+1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("INTERVAL +'1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("-1-0"), YearMonthIntervalType), -12)
+ checkEvaluation(cast(Literal.create("INTERVAL '10-1' YEAR TO MONTH"),
Review comment:
Done
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
##########
@@ -1774,6 +1774,51 @@ class CastSuite extends CastSuiteBase {
assert(e3.contains("Casting 2147483648 to int causes overflow"))
}
}
+
+ test("SPARK-35111: Cast string to year-month interval") {
+ checkEvaluation(cast(Literal.create("INTERVAL '1-0' YEAR TO MONTH"),
+ YearMonthIntervalType), 12)
+ checkEvaluation(cast(Literal.create("0-0"), YearMonthIntervalType), 0)
Review comment:
Done
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -127,20 +123,21 @@ object IntervalUtils {
require(input != null, "Interval year-month string must be not null")
input.trim match {
case yearMonthPattern("-", yearStr, monthStr) =>
- toYMInterval(yearStr, monthStr, -1)
+ new CalendarInterval(toYMInterval(yearStr, monthStr, -1), 0, 0)
case yearMonthPattern(_, yearStr, monthStr) =>
- toYMInterval(yearStr, monthStr, 1)
+ new CalendarInterval(toYMInterval(yearStr, monthStr, 1), 0, 0)
case _ =>
throw new IllegalArgumentException(
s"Interval string does not match year-month format of 'y-m': $input")
}
}
- def toYMInterval(yearStr: String, monthStr: String, sign: Int):
CalendarInterval = {
+ def toYMInterval(yearStr: String, monthStr: String, sign: Int): Int = {
try {
val years = toLongWithRange(YEAR, yearStr, 0, Integer.MAX_VALUE /
MONTHS_PER_YEAR)
val totalMonths = sign * (years * MONTHS_PER_YEAR +
toLongWithRange(MONTH, monthStr, 0, 11))
- new CalendarInterval(Math.toIntExact(totalMonths), 0, 0)
+// new CalendarInterval(Math.toIntExact(totalMonths), 0, 0)
Review comment:
Done
--
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]