All Products
Search
Document Center

MaxCompute:SEQUENCE

Last Updated:Jul 24, 2023

Returns an array that contains the specified elements based on expressions.

Syntax

	sequence(start, stop, [step]) 

Parameters

  • start: an expression that represents the beginning of a sequence of elements. The sequence contains start.

    • start and stop support the following integer types: TINYINT, SMALLINT, INT, and BIGINT. The preceding integer types correspond to the following types supported by step: TINYINT, SMALLINT, INT, and BIGINT.

    • start and stop support the following date and time types: DATE, DATETIME, and TIMESTAMP. The preceding date and time types correspond to the IntervalDayTime or IntervalYearMonth type supported by step.

  • stop: an expression that represents the end of a sequence of elements. The sequence contains stop.

  • step: optional. The step size of a sequence of elements.

    By default, if the value of start is less than or equal to the value of stop, the value of step is 1. If the value of start is greater than the value of stop, the value of step is -1.

    If the sequence of elements is of the time type, the default value of step is 1 or -1 in days. When you configure step, take note of the following points: If the value of start is greater than the value of stop, set step to a negative value. If the value of start is less than or equal to the value of stop, set step to a positive value. If the requirements are not met, an error is returned.

Return value

An array that contains the specified elements based on expressions is returned.

  • If the value of start is greater than the value of stop and the value of step is positive, an error is returned. If the value of start is less than or equal to the value of stop and the value of step is negative, an error is returned.

  • By default, the SEQUENCE function can generate a maximum of 10,000 elements. You can configure the odps.sql.max.sequence.length parameter to change the maximum number of elements that can be generated.

Examples

  • SELECT sequence(1, 5);

    The following result is returned:

    [1, 2, 3, 4, 5]
  • SELECT sequence(5, 1);

    The following result is returned:

    [5, 4, 3, 2, 1] 
  • SELECT sequence(to_date('2018-01-01'), to_date('2018-03-01'), interval 1 month);

    The following result is returned:

    [2018-01-01,  2018-02-01,  2018-03-01]	

Related functions

SEQUENCE is a complex type function. For more information about the functions that are used to process data of complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.