Skip to content

Commit f80f0f3

Browse files
committed
refine tests
1 parent 988b51c commit f80f0f3

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import scala.language.implicitConversions
2121

2222
import org.apache.spark.SparkFunSuite
2323
import org.apache.spark.sql.catalyst.util.DateTimeConstants._
24-
import org.apache.spark.sql.catalyst.util.IntervalUtils.stringToInterval
24+
import org.apache.spark.sql.catalyst.util.IntervalUtils.{safeStringToInterval, stringToInterval}
2525
import org.apache.spark.sql.internal.SQLConf
2626
import org.apache.spark.sql.types.Decimal
2727
import org.apache.spark.unsafe.types.{CalendarInterval, UTF8String}
@@ -198,19 +198,15 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
198198
}
199199

200200
test("multiply") {
201-
def check(
202-
interval: String,
203-
num: Double,
204-
expected: String,
205-
checkException: Boolean = false): Unit = {
201+
def check(interval: String, num: Double, expected: String): Unit = {
202+
val expr = MultiplyInterval(Literal(stringToInterval(interval)), Literal(num))
203+
val expectedRes = safeStringToInterval(expected)
206204
Seq("true", "false").foreach { v =>
207205
withSQLConf(SQLConf.ANSI_ENABLED.key -> v) {
208-
if (checkException) {
209-
checkExceptionInExpression[ArithmeticException](
210-
MultiplyInterval(Literal(stringToInterval(interval)), Literal(num)), expected)
206+
if (expectedRes == null) {
207+
checkExceptionInExpression[ArithmeticException](expr, expected)
211208
} else {
212-
checkEvaluation(MultiplyInterval(Literal(stringToInterval(interval)), Literal(num)),
213-
if (expected == null) null else stringToInterval(expected))
209+
checkEvaluation(expr, expectedRes)
214210
}
215211
}
216212
}
@@ -224,23 +220,19 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
224220
check("-100 years -1 millisecond", 0.5, "-50 years -500 microseconds")
225221
check("2 months 4 seconds", -0.5, "-1 months -2 seconds")
226222
check("1 month 2 microseconds", 1.5, "1 months 15 days 3 microseconds")
227-
check("2 months", Int.MaxValue, "integer overflow", checkException = true)
223+
check("2 months", Int.MaxValue, "integer overflow")
228224
}
229225

230226
test("divide") {
231-
def check(
232-
interval: String,
233-
num: Double,
234-
expected: String,
235-
checkException: Boolean = false): Unit = {
227+
def check(interval: String, num: Double, expected: String): Unit = {
228+
val expr = DivideInterval(Literal(stringToInterval(interval)), Literal(num))
229+
val expectedRes = safeStringToInterval(expected)
236230
Seq("true", "false").foreach { v =>
237231
withSQLConf(SQLConf.ANSI_ENABLED.key -> v) {
238-
if (checkException) {
239-
checkExceptionInExpression[ArithmeticException](
240-
DivideInterval(Literal(stringToInterval(interval)), Literal(num)), expected)
232+
if (expectedRes == null) {
233+
checkExceptionInExpression[ArithmeticException](expr, expected)
241234
} else {
242-
checkEvaluation(DivideInterval(Literal(stringToInterval(interval)), Literal(num)),
243-
if (expected == null) null else stringToInterval(expected))
235+
checkEvaluation(expr, expectedRes)
244236
}
245237
}
246238
}
@@ -253,8 +245,8 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
253245
check("2 years -8 seconds", 0.5, "4 years -16 seconds")
254246
check("-1 month 2 microseconds", -0.25, "4 months -8 microseconds")
255247
check("1 month 3 microsecond", 1.5, "20 days 2 microseconds")
256-
check("1 second", 0, "divide by zero", checkException = true)
257-
check(s"${Int.MaxValue} months", 0.9, "integer overflow", checkException = true)
248+
check("1 second", 0, "divide by zero")
249+
check(s"${Int.MaxValue} months", 0.9, "integer overflow")
258250
}
259251

260252
test("make interval") {

0 commit comments

Comments
 (0)