diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala index 026a2a677bae..74c9b12a109d 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala @@ -2612,10 +2612,17 @@ object Sequence { val stepDays = step.days val stepMicros = step.microseconds + if (scale == MICROS_PER_DAY && stepMonths == 0 && stepDays == 0) { + throw new IllegalArgumentException( + "sequence step must be a day interval if start and end values are dates") + } + if (stepMonths == 0 && stepMicros == 0 && scale == MICROS_PER_DAY) { + // Adding pure days to date start/end backedSequenceImpl.eval(start, stop, fromLong(stepDays)) } else if (stepMonths == 0 && stepDays == 0 && scale == 1) { + // Adding pure microseconds to timestamp start/end backedSequenceImpl.eval(start, stop, fromLong(stepMicros)) } else { @@ -2674,11 +2681,24 @@ object Sequence { |${genSequenceLengthCode(ctx, startMicros, stopMicros, intervalInMicros, arrLength)} """.stripMargin + val check = if (scale == MICROS_PER_DAY) { + s""" + |if ($stepMonths == 0 && $stepDays == 0) { + | throw new IllegalArgumentException( + | "sequence step must be a day interval if start and end values are dates"); + |} + """.stripMargin + } else { + "" + } + s""" |final int $stepMonths = $step.months; |final int $stepDays = $step.days; |final long $stepMicros = $step.microseconds; | + |$check + | |if ($stepMonths == 0 && $stepMicros == 0 && ${scale}L == ${MICROS_PER_DAY}L) { | ${backedSequenceImpl.genCode(ctx, start, stop, stepDays, arr, elemType)}; | diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala index 3a0c02b29d92..856c1fad9b20 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala @@ -933,6 +933,13 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper Literal(negateExact(stringToInterval("interval 1 month")))), EmptyRow, s"sequence boundaries: 0 to 2678400000000 by -${28 * MICROS_PER_DAY}") + + // SPARK-32133: Sequence step must be a day interval if start and end values are dates + checkExceptionInExpression[IllegalArgumentException](Sequence( + Cast(Literal("2011-03-01"), DateType), + Cast(Literal("2011-04-01"), DateType), + Option(Literal(stringToInterval("interval 1 hour")))), null, + "sequence step must be a day interval if start and end values are dates") } }