Skip to content

Commit 2073be2

Browse files
committed
[SPARK-21590][SS]Window start time should support negative values
1 parent 84454d7 commit 2073be2

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TimeWindow.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,13 @@ case class TimeWindow(
7979
if (slideDuration <= 0) {
8080
return TypeCheckFailure(s"The slide duration ($slideDuration) must be greater than 0.")
8181
}
82-
if (startTime < 0) {
83-
return TypeCheckFailure(s"The start time ($startTime) must be greater than or equal to 0.")
84-
}
8582
if (slideDuration > windowDuration) {
8683
return TypeCheckFailure(s"The slide duration ($slideDuration) must be less than or equal" +
8784
s" to the windowDuration ($windowDuration).")
8885
}
89-
if (startTime >= slideDuration) {
90-
return TypeCheckFailure(s"The start time ($startTime) must be less than the " +
91-
s"slideDuration ($slideDuration).")
86+
if (startTime.abs >= slideDuration) {
87+
return TypeCheckFailure(s"The absolute value of start time ($startTime) must be less " +
88+
s"than the slideDuration ($slideDuration).")
9289
}
9390
}
9491
dataTypeCheck

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,28 @@ class AnalysisErrorSuite extends AnalysisTest {
334334
"start time greater than slide duration in time window",
335335
testRelation.select(
336336
TimeWindow(Literal("2016-01-01 01:01:01"), "1 second", "1 second", "1 minute").as("window")),
337-
"The start time " :: " must be less than the slideDuration " :: Nil
337+
"The absolute value of start time " :: " must be less than the slideDuration " :: Nil
338338
)
339339

340340
errorTest(
341341
"start time equal to slide duration in time window",
342342
testRelation.select(
343343
TimeWindow(Literal("2016-01-01 01:01:01"), "1 second", "1 second", "1 second").as("window")),
344-
"The start time " :: " must be less than the slideDuration " :: Nil
344+
"The absolute value of start time " :: " must be less than the slideDuration " :: Nil
345+
)
346+
347+
errorTest(
348+
"SPARK-21590: absolute value of start time greater than slide duration in time window",
349+
testRelation.select(
350+
TimeWindow(Literal("2016-01-01 01:01:01"), "1 second", "1 second", "-1 minute").as("window")),
351+
"The absolute value of start time " :: " must be less than the slideDuration " :: Nil
352+
)
353+
354+
errorTest(
355+
"SPARK-21590: absolute value of start time equal to slide duration in time window",
356+
testRelation.select(
357+
TimeWindow(Literal("2016-01-01 01:01:01"), "1 second", "1 second", "-1 second").as("window")),
358+
"The absolute value of start time " :: " must be less than the slideDuration " :: Nil
345359
)
346360

347361
errorTest(
@@ -372,13 +386,6 @@ class AnalysisErrorSuite extends AnalysisTest {
372386
"The slide duration" :: " must be greater than 0." :: Nil
373387
)
374388

375-
errorTest(
376-
"negative start time in time window",
377-
testRelation.select(
378-
TimeWindow(Literal("2016-01-01 01:01:01"), "1 second", "1 second", "-5 second").as("window")),
379-
"The start time" :: "must be greater than or equal to 0." :: Nil
380-
)
381-
382389
errorTest(
383390
"generator nested in expressions",
384391
listRelation.select(Explode('list) + 1),

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ class TimeWindowSuite extends SparkFunSuite with ExpressionEvalHelper with Priva
7777
}
7878
}
7979

80+
test("SPARK-21590: Start time works with negative values and return microseconds") {
81+
val validDuration = "10 minutes"
82+
for ((text, seconds) <- Seq(
83+
("-10 seconds", -10000000), // -1e7
84+
("-1 minute", -60000000),
85+
("-1 hour", -3600000000L))) { // -6e7
86+
assert(TimeWindow(Literal(10L), validDuration, validDuration, "interval " + text).startTime
87+
=== seconds)
88+
assert(TimeWindow(Literal(10L), validDuration, validDuration, text).startTime
89+
=== seconds)
90+
}
91+
}
92+
8093
private val parseExpression = PrivateMethod[Long]('parseExpression)
8194

8295
test("parse sql expression for duration in microseconds - string") {

0 commit comments

Comments
 (0)