Skip to content

Commit

Permalink
[SPARK-36639][SQL] Fix an issue that sequence builtin function causes…
Browse files Browse the repository at this point in the history
… ArrayIndexOutOfBoundsException if the arguments are under the condition of start == stop && step < 0

### What changes were proposed in this pull request?

This PR fixes an issue that `sequence` builtin function causes `ArrayIndexOutOfBoundsException` if the arguments are under the condition of `start == stop && step < 0`.
This is an example.
```
SELECT sequence(timestamp'2021-08-31', timestamp'2021-08-31', -INTERVAL 1 month);
21/09/02 04:14:42 ERROR SparkSQLDriver: Failed in [SELECT sequence(timestamp'2021-08-31', timestamp'2021-08-31', -INTERVAL 1 month)]
java.lang.ArrayIndexOutOfBoundsException: 1
```
Actually, this example succeeded before SPARK-31980 (#28819) was merged.

### Why are the changes needed?

Bug fix.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

New tests.

Closes #33895 from sarutak/fix-sequence-issue.

Authored-by: Kousuke Saruta <sarutak@oss.nttdata.com>
Signed-off-by: Kousuke Saruta <sarutak@oss.nttdata.com>
(cherry picked from commit cf3bc65)
Signed-off-by: Kousuke Saruta <sarutak@oss.nttdata.com>
  • Loading branch information
sarutak committed Sep 3, 2021
1 parent 6352085 commit c1f8d75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2711,7 +2711,7 @@ object Sequence {
val maxEstimatedArrayLength =
getSequenceLength(startMicros, stopMicros, intervalStepInMicros)

val stepSign = if (stopMicros >= startMicros) +1 else -1
val stepSign = if (intervalStepInMicros > 0) +1 else -1
val exclusiveItem = stopMicros + stepSign
val arr = new Array[T](maxEstimatedArrayLength)
var t = startMicros
Expand Down Expand Up @@ -2786,7 +2786,7 @@ object Sequence {
|
| $sequenceLengthCode
|
| final int $stepSign = $stopMicros >= $startMicros ? +1 : -1;
| final int $stepSign = $intervalInMicros > 0 ? +1 : -1;
| final long $exclusiveItem = $stopMicros + $stepSign;
|
| $arr = new $elemType[$arrLength];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,24 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
Seq(Date.valueOf("2018-01-01")))
}

test("SPARK-36639: Start and end equal in month range with a negative step") {
checkEvaluation(new Sequence(
Literal(Date.valueOf("2018-01-01")),
Literal(Date.valueOf("2018-01-01")),
Literal(stringToInterval("interval -1 day"))),
Seq(Date.valueOf("2018-01-01")))
checkEvaluation(new Sequence(
Literal(Date.valueOf("2018-01-01")),
Literal(Date.valueOf("2018-01-01")),
Literal(stringToInterval("interval -1 month"))),
Seq(Date.valueOf("2018-01-01")))
checkEvaluation(new Sequence(
Literal(Date.valueOf("2018-01-01")),
Literal(Date.valueOf("2018-01-01")),
Literal(stringToInterval("interval -1 year"))),
Seq(Date.valueOf("2018-01-01")))
}

test("SPARK-33386: element_at ArrayIndexOutOfBoundsException") {
Seq(true, false).foreach { ansiEnabled =>
withSQLConf(SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString) {
Expand Down

0 comments on commit c1f8d75

Please sign in to comment.