cloud-fan commented on a change in pull request #27672: [SPARK-30919][SQL] Make
interval multiply and divide's overflow behavior consistent with other
operations
URL: https://github.com/apache/spark/pull/27672#discussion_r383713039
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/IntervalUtilsSuite.scala
##########
@@ -267,35 +267,47 @@ class IntervalUtilsSuite extends SparkFunSuite with
SQLHelper {
}
test("multiply by num") {
- var interval = new CalendarInterval(0, 0, 0)
- assert(interval === multiplyExact(interval, 0))
- interval = new CalendarInterval(123, 456, 789)
- assert(new CalendarInterval(123 * 42, 456 * 42, 789 * 42) ===
multiplyExact(interval, 42))
- interval = new CalendarInterval(-123, -456, -789)
- assert(new CalendarInterval(-123 * 42, -456 * 42, -789 * 42) ===
multiplyExact(interval, 42))
- assert(new CalendarInterval(1, 22, 12 * MICROS_PER_HOUR) ===
- multiplyExact(new CalendarInterval(1, 5, 0), 1.5))
- assert(new CalendarInterval(2, 14, 12 * MICROS_PER_HOUR) ===
- multiplyExact(new CalendarInterval(2, 2, 2 * MICROS_PER_HOUR), 1.2))
+ Seq[(CalendarInterval, Double) => CalendarInterval](multiply,
multiplyExact).foreach { func =>
+ var interval = new CalendarInterval(0, 0, 0)
+ assert(interval === func(interval, 0))
+ interval = new CalendarInterval(123, 456, 789)
+ assert(new CalendarInterval(123 * 42, 456 * 42, 789 * 42) ===
func(interval, 42))
+ interval = new CalendarInterval(-123, -456, -789)
+ assert(new CalendarInterval(-123 * 42, -456 * 42, -789 * 42) ===
func(interval, 42))
+ interval = new CalendarInterval(1, 5, 0)
+ assert(new CalendarInterval(1, 7, 12 * MICROS_PER_HOUR) ===
func(interval, 1.5))
+ interval = new CalendarInterval(2, 2, 2 * MICROS_PER_HOUR)
+ assert(new CalendarInterval(2, 2, 12 * MICROS_PER_HOUR) ===
func(interval, 1.2))
+ }
+ val interval = new CalendarInterval(2, 0, 0)
+ assert(multiply(interval, Integer.MAX_VALUE) === new
CalendarInterval(Int.MaxValue, 0, 0))
try {
- multiplyExact(new CalendarInterval(2, 0, 0), Integer.MAX_VALUE)
+ multiplyExact(interval, Integer.MAX_VALUE)
fail("Expected to throw an exception on months overflow")
} catch {
case e: ArithmeticException => assert(e.getMessage.contains("overflow"))
}
}
test("divide by num") {
- var interval = new CalendarInterval(0, 0, 0)
- assert(interval === divideExact(interval, 10))
- interval = new CalendarInterval(1, 3, 30 * MICROS_PER_SECOND)
- assert(new CalendarInterval(0, 16, 12 * MICROS_PER_HOUR + 15 *
MICROS_PER_SECOND) ===
- divideExact(interval, 2))
- assert(new CalendarInterval(2, 6, MICROS_PER_MINUTE) ===
divideExact(interval, 0.5))
- interval = new CalendarInterval(-1, 0, -30 * MICROS_PER_SECOND)
- assert(new CalendarInterval(0, -15, -15 * MICROS_PER_SECOND) ===
divideExact(interval, 2))
- assert(new CalendarInterval(-2, 0, -1 * MICROS_PER_MINUTE) ===
divideExact(interval, 0.5))
+ Seq[(CalendarInterval, Double) => CalendarInterval](divide,
divideExact).foreach { func =>
+ var interval = new CalendarInterval(0, 0, 0)
+ assert(interval === func(interval, 10))
+ interval = new CalendarInterval(1, 3, 30 * MICROS_PER_SECOND)
+ assert(new CalendarInterval(0, 1, 12 * MICROS_PER_HOUR + 15 *
MICROS_PER_SECOND) ===
+ func(interval, 2))
+ assert(new CalendarInterval(2, 6, MICROS_PER_MINUTE) === func(interval,
0.5))
+ interval = new CalendarInterval(-1, 0, -30 * MICROS_PER_SECOND)
+ assert(new CalendarInterval(0, 0, -15 * MICROS_PER_SECOND) ===
func(interval, 2))
+ assert(new CalendarInterval(-2, 0, -MICROS_PER_MINUTE) ===
func(interval, 0.5))
+ }
+
+ assert(divide(new CalendarInterval(Int.MaxValue, Int.MaxValue, 0), 0.9) ===
Review comment:
we should test `divideExact` 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:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]