Skip to content

Commit 2669b4d

Browse files
wajdaueshin
authored andcommitted
[SPARK-23927][SQL] Add "sequence" expression
## What changes were proposed in this pull request? The PR adds the SQL function ```sequence```. https://issues.apache.org/jira/browse/SPARK-23927 The behavior of the function is based on Presto's one. Ref: https://prestodb.io/docs/current/functions/array.html - ```sequence(start, stop) → array<bigint>``` Generate a sequence of integers from ```start``` to ```stop```, incrementing by ```1``` if ```start``` is less than or equal to ```stop```, otherwise ```-1```. - ```sequence(start, stop, step) → array<bigint>``` Generate a sequence of integers from ```start``` to ```stop```, incrementing by ```step```. - ```sequence(start_date, stop_date) → array<date>``` Generate a sequence of dates from ```start_date``` to ```stop_date```, incrementing by ```interval 1 day``` if ```start_date``` is less than or equal to ```stop_date```, otherwise ```- interval 1 day```. - ```sequence(start_date, stop_date, step_interval) → array<date>``` Generate a sequence of dates from ```start_date``` to ```stop_date```, incrementing by ```step_interval```. The type of ```step_interval``` is ```CalendarInterval```. - ```sequence(start_timestemp, stop_timestemp) → array<timestamp>``` Generate a sequence of timestamps from ```start_timestamps``` to ```stop_timestamps```, incrementing by ```interval 1 day``` if ```start_date``` is less than or equal to ```stop_date```, otherwise ```- interval 1 day```. - ```sequence(start_timestamp, stop_timestamp, step_interval) → array<timestamp>``` Generate a sequence of timestamps from ```start_timestamps``` to ```stop_timestamps```, incrementing by ```step_interval```. The type of ```step_interval``` is ```CalendarInterval```. ## How was this patch tested? Added unit tests. Author: Vayda, Oleksandr: IT (PRG) <[email protected]> Closes #21155 from wajda/feature/array-api-sequence.
1 parent d08f53d commit 2669b4d

File tree

6 files changed

+777
-2
lines changed

6 files changed

+777
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ object FunctionRegistry {
432432
expression[Reverse]("reverse"),
433433
expression[Concat]("concat"),
434434
expression[Flatten]("flatten"),
435+
expression[Sequence]("sequence"),
435436
expression[ArrayRepeat]("array_repeat"),
436437
expression[ArrayRemove]("array_remove"),
437438
expression[ArrayDistinct]("array_distinct"),

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,13 @@ object TypeCoercion {
544544
case None => aj
545545
}
546546

547+
case s @ Sequence(_, _, _, timeZoneId) if !haveSameType(s.coercibleChildren) =>
548+
val types = s.coercibleChildren.map(_.dataType)
549+
findWiderCommonType(types) match {
550+
case Some(widerDataType) => s.castChildrenTo(widerDataType)
551+
case None => s
552+
}
553+
547554
case m @ CreateMap(children) if m.keys.length == m.values.length &&
548555
(!haveSameType(m.keys) || !haveSameType(m.values)) =>
549556
val newKeys = if (haveSameType(m.keys)) {

0 commit comments

Comments
 (0)