Array Functions and Operators
Extreme DPS provides a range of array functions and operators to help you use and process data of the array type.
Array operators
Currently, Extreme DPS supports the subscript operator []
and the concatenation operator ||
.
[]
Accesses the element of an array at a specified subscript number. The subscript numbers of elements in an array start from 1.
Null is returned in the following scenarios:
-
The specified subscript number is out of bounds.
-
The specified subscript number is negative.
-
The specified subscript number is 0.
Following are some examples:
SELECT array_col[1] FROM (VALUES(ARRAY[11, 22, 33])) as t1(array_col);
-> 11
SELECT array_col[8] FROM (VALUES(ARRAY[11, 22, 33])) as t1(array_col);
-> null
SELECT array_col[0] FROM (VALUES(ARRAY[11, 22, 33])) as t1(array_col);
-> null
SELECT array_col[-1] FROM (VALUES(ARRAY[11, 22, 33])) as t1(array_col);
-> null
||
Concatenates two arrays with the same type of elements.
Here are some examples:
SELECT ARRAY[1,2,3] || ARRAY[4,5,6];
-> [1, 2, 3, 4, 5, 6]
SELECT 3 || ARRAY[4,5,6];
-> [3, 4, 5, 6]
SELECT ARRAY[4,5,6] || 7;
-> [4, 5, 6, 7]
Array functions
This section introduces the array functions supported by Extreme DPS.
ARRAY_SORT
Returns the input array with its elements sorted in ascending order. The elements of the array must be sortable. NULL elements are placed at the end of the array.
Syntax
ARRAY_SORT(<array>)
Arguments
<array>
: the array of which elements need to be sorted.
Returns
The same type as the input.
Examples
SELECT ARRAY_SORT(ARRAY[2, 1, 3]);
-> {1, 2, 3}
ARRAY_SORT_DESC
Returns the array with its elements sorted in descending order. The elements of the array must be sortable. NULL elements are placed at the end of the array.
Syntax
ARRAY_SORT_DESC(<array>)
Arguments
<array>
: the array of which elements need to be sorted.
Returns
The same type as the input.
Examples
SELECT ARRAY_SORT_DESC(ARRAY[2, 1, 3]);
-> {3, 2, 1}
ARRAY_ELEMENT_SUM
Returns the sum of all non-NULL elements in the array.
Syntax
ARRAY_ELEMENT_SUM(<array>)
Arguments
<array>
: the array whose elements need to be summed up, where the element type can be smallint
, integer
, bigint
, real
, double precision
.
Returns
-
For input type
smallint
,integer
, orbigint
, the return type isbigint
. -
For input type
real
ordouble precision
, the return type isdouble precision
.
Examples
SELECT ARRAY_ELEMENT_SUM(ARRAY[1, null, 2, 3]);
-> 6
SELECT ARRAY_ELEMENT_SUM(ARRAY[]::integer array);
-> 0
CARDINALITY
Returns the number of elements in an array. If the array is empty, 0
will be returned.