Array functions
Note
To see which AWS data source integrations support this SQL command, see Supported OpenSearch SQL commands and functions.
Function | Description |
---|---|
array(expr, ...) | Returns an array with the given elements. |
array_append(array, element) | Add the element at the end of the array passed as first argument. Type of element should be similar to type of the elements of the array. Null element is also appended into the array. But if the array passed, is NULL output is NULL |
array_compact(array) | Removes null values from the array. |
array_contains(array, value) | Returns true if the array contains the value. |
array_distinct(array) | Removes duplicate values from the array. |
array_except(array1, array2) | Returns an array of the elements in array1 but not in array2, without duplicates. |
array_insert(x, pos, val) | Places val into index pos of array x. Array indices start at 1. The maximum negative index is -1 for which the function inserts new element after the current last element. Index above array size appends the array, or prepends the array if index is negative, with 'null' elements. |
array_intersect(array1, array2) | Returns an array of the elements in the intersection of array1 and array2, without duplicates. |
array_join(array, delimiter[, nullReplacement]) | Concatenates the elements of the given array using the delimiter and an optional string to replace nulls. If no value is set for nullReplacement, any null value is filtered. |
array_max(array) | Returns the maximum value in the array. NaN is greater than any non-NaN elements for double/float type. NULL elements are skipped. |
array_min(array) | Returns the minimum value in the array. NaN is greater than any non-NaN elements for double/float type. NULL elements are skipped. |
array_position(array, element) | Returns the (1-based) index of the first matching element of the array as long, or 0 if no match is found. |
array_prepend(array, element) | Add the element at the beginning of the array passed as first argument. Type of element should be the same as the type of the elements of the array. Null element is also prepended to the array. But if the array passed is NULL output is NULL |
array_remove(array, element) | Remove all elements that equal to element from array. |
array_repeat(element, count) | Returns the array containing element count times. |
array_union(array1, array2) | Returns an array of the elements in the union of array1 and array2, without duplicates. |
arrays_overlap(a1, a2) | Returns true if a1 contains at least a non-null element present also in a2. If the arrays have no common element and they are both non-empty and either of them contains a null element null is returned, false otherwise. |
arrays_zip(a1, a2, ...) | Returns a merged array of structs in which the N-th struct contains all N-th values of input arrays. |
flatten(arrayOfArrays) | Transforms an array of arrays into a single array. |
get(array, index) | Returns element of array at given (0-based) index. If the index points outside of the array boundaries, then this function returns NULL. |
sequence(start, stop, step) | Generates an array of elements from start to stop (inclusive), incrementing by step. The type of the returned elements is the same as the type of argument expressions. Supported types are: byte, short, integer, long, date, timestamp. The start and stop expressions must resolve to the same type. If start and stop expressions resolve to the 'date' or 'timestamp' type then the step expression must resolve to the 'interval' or 'year-month interval' or 'day-time interval' type, otherwise to the same type as the start and stop expressions. |
shuffle(array) | Returns a random permutation of the given array. |
slice(x, start, length) | Subsets array x starting from index start (array indices start at 1, or starting from the end if start is negative) with the specified length. |
sort_array(array[, ascendingOrder]) | Sorts the input array in ascending or descending order according to the natural ordering of the array elements. NaN is greater than any non-NaN elements for double/float type. Null elements will be placed at the beginning of the returned array in ascending order or at the end of the returned array in descending order. |
Examples
-- array SELECT array(1, 2, 3); +--------------+ |array(1, 2, 3)| +--------------+ | [1, 2, 3]| +--------------+ -- array_append SELECT array_append(array('b', 'd', 'c', 'a'), 'd'); +----------------------------------+ |array_append(array(b, d, c, a), d)| +----------------------------------+ | [b, d, c, a, d]| +----------------------------------+ SELECT array_append(array(1, 2, 3, null), null); +----------------------------------------+ |array_append(array(1, 2, 3, NULL), NULL)| +----------------------------------------+ | [1, 2, 3, NULL, N...| +----------------------------------------+ SELECT array_append(CAST(null as Array<Int>), 2); +---------------------+ |array_append(NULL, 2)| +---------------------+ | NULL| +---------------------+ -- array_compact SELECT array_compact(array(1, 2, 3, null)); +-----------------------------------+ |array_compact(array(1, 2, 3, NULL))| +-----------------------------------+ | [1, 2, 3]| +-----------------------------------+ SELECT array_compact(array("a", "b", "c")); +-----------------------------+ |array_compact(array(a, b, c))| +-----------------------------+ | [a, b, c]| +-----------------------------+ -- array_contains SELECT array_contains(array(1, 2, 3), 2); +---------------------------------+ |array_contains(array(1, 2, 3), 2)| +---------------------------------+ | true| +---------------------------------+ -- array_distinct SELECT array_distinct(array(1, 2, 3, null, 3)); +---------------------------------------+ |array_distinct(array(1, 2, 3, NULL, 3))| +---------------------------------------+ | [1, 2, 3, NULL]| +---------------------------------------+ -- array_except SELECT array_except(array(1, 2, 3), array(1, 3, 5)); +--------------------------------------------+ |array_except(array(1, 2, 3), array(1, 3, 5))| +--------------------------------------------+ | [2]| +--------------------------------------------+ -- array_insert SELECT array_insert(array(1, 2, 3, 4), 5, 5); +-------------------------------------+ |array_insert(array(1, 2, 3, 4), 5, 5)| +-------------------------------------+ | [1, 2, 3, 4, 5]| +-------------------------------------+ SELECT array_insert(array(5, 4, 3, 2), -1, 1); +--------------------------------------+ |array_insert(array(5, 4, 3, 2), -1, 1)| +--------------------------------------+ | [5, 4, 3, 2, 1]| +--------------------------------------+ SELECT array_insert(array(5, 3, 2, 1), -4, 4); +--------------------------------------+ |array_insert(array(5, 3, 2, 1), -4, 4)| +--------------------------------------+ | [5, 4, 3, 2, 1]| +--------------------------------------+ -- array_intersect SELECT array_intersect(array(1, 2, 3), array(1, 3, 5)); +-----------------------------------------------+ |array_intersect(array(1, 2, 3), array(1, 3, 5))| +-----------------------------------------------+ | [1, 3]| +-----------------------------------------------+ -- array_join SELECT array_join(array('hello', 'world'), ' '); +----------------------------------+ |array_join(array(hello, world), )| +----------------------------------+ | hello world| +----------------------------------+ SELECT array_join(array('hello', null ,'world'), ' '); +----------------------------------------+ |array_join(array(hello, NULL, world), )| +----------------------------------------+ | hello world| +----------------------------------------+ SELECT array_join(array('hello', null ,'world'), ' ', ','); +-------------------------------------------+ |array_join(array(hello, NULL, world), , ,)| +-------------------------------------------+ | hello , world| +-------------------------------------------+ -- array_max SELECT array_max(array(1, 20, null, 3)); +--------------------------------+ |array_max(array(1, 20, NULL, 3))| +--------------------------------+ | 20| +--------------------------------+ -- array_min SELECT array_min(array(1, 20, null, 3)); +--------------------------------+ |array_min(array(1, 20, NULL, 3))| +--------------------------------+ | 1| +--------------------------------+ -- array_position SELECT array_position(array(312, 773, 708, 708), 708); +----------------------------------------------+ |array_position(array(312, 773, 708, 708), 708)| +----------------------------------------------+ | 3| +----------------------------------------------+ SELECT array_position(array(312, 773, 708, 708), 414); +----------------------------------------------+ |array_position(array(312, 773, 708, 708), 414)| +----------------------------------------------+ | 0| +----------------------------------------------+ -- array_prepend SELECT array_prepend(array('b', 'd', 'c', 'a'), 'd'); +-----------------------------------+ |array_prepend(array(b, d, c, a), d)| +-----------------------------------+ | [d, b, d, c, a]| +-----------------------------------+ SELECT array_prepend(array(1, 2, 3, null), null); +-----------------------------------------+ |array_prepend(array(1, 2, 3, NULL), NULL)| +-----------------------------------------+ | [NULL, 1, 2, 3, N...| +-----------------------------------------+ SELECT array_prepend(CAST(null as Array<Int>), 2); +----------------------+ |array_prepend(NULL, 2)| +----------------------+ | NULL| +----------------------+ -- array_remove SELECT array_remove(array(1, 2, 3, null, 3), 3); +----------------------------------------+ |array_remove(array(1, 2, 3, NULL, 3), 3)| +----------------------------------------+ | [1, 2, NULL]| +----------------------------------------+ -- array_repeat SELECT array_repeat('123', 2); +--------------------+ |array_repeat(123, 2)| +--------------------+ | [123, 123]| +--------------------+ -- array_union SELECT array_union(array(1, 2, 3), array(1, 3, 5)); +-------------------------------------------+ |array_union(array(1, 2, 3), array(1, 3, 5))| +-------------------------------------------+ | [1, 2, 3, 5]| +-------------------------------------------+ -- arrays_overlap SELECT arrays_overlap(array(1, 2, 3), array(3, 4, 5)); +----------------------------------------------+ |arrays_overlap(array(1, 2, 3), array(3, 4, 5))| +----------------------------------------------+ | true| +----------------------------------------------+ -- arrays_zip SELECT arrays_zip(array(1, 2, 3), array(2, 3, 4)); +------------------------------------------+ |arrays_zip(array(1, 2, 3), array(2, 3, 4))| +------------------------------------------+ | [{1, 2}, {2, 3}, ...| +------------------------------------------+ SELECT arrays_zip(array(1, 2), array(2, 3), array(3, 4)); +-------------------------------------------------+ |arrays_zip(array(1, 2), array(2, 3), array(3, 4))| +-------------------------------------------------+ | [{1, 2, 3}, {2, 3...| +-------------------------------------------------+ -- flatten SELECT flatten(array(array(1, 2), array(3, 4))); +----------------------------------------+ |flatten(array(array(1, 2), array(3, 4)))| +----------------------------------------+ | [1, 2, 3, 4]| +----------------------------------------+ -- get SELECT get(array(1, 2, 3), 0); +----------------------+ |get(array(1, 2, 3), 0)| +----------------------+ | 1| +----------------------+ SELECT get(array(1, 2, 3), 3); +----------------------+ |get(array(1, 2, 3), 3)| +----------------------+ | NULL| +----------------------+ SELECT get(array(1, 2, 3), -1); +-----------------------+ |get(array(1, 2, 3), -1)| +-----------------------+ | NULL| +-----------------------+ -- sequence SELECT sequence(1, 5); +---------------+ | sequence(1, 5)| +---------------+ |[1, 2, 3, 4, 5]| +---------------+ SELECT sequence(5, 1); +---------------+ | sequence(5, 1)| +---------------+ |[5, 4, 3, 2, 1]| +---------------+ SELECT sequence(to_date('2018-01-01'), to_date('2018-03-01'), interval 1 month); +----------------------------------------------------------------------+ |sequence(to_date(2018-01-01), to_date(2018-03-01), INTERVAL '1' MONTH)| +----------------------------------------------------------------------+ | [2018-01-01, 2018...| +----------------------------------------------------------------------+ SELECT sequence(to_date('2018-01-01'), to_date('2018-03-01'), interval '0-1' year to month); +--------------------------------------------------------------------------------+ |sequence(to_date(2018-01-01), to_date(2018-03-01), INTERVAL '0-1' YEAR TO MONTH)| +--------------------------------------------------------------------------------+ | [2018-01-01, 2018...| +--------------------------------------------------------------------------------+ -- shuffle SELECT shuffle(array(1, 20, 3, 5)); +---------------------------+ |shuffle(array(1, 20, 3, 5))| +---------------------------+ | [5, 1, 20, 3]| +---------------------------+ SELECT shuffle(array(1, 20, null, 3)); +------------------------------+ |shuffle(array(1, 20, NULL, 3))| +------------------------------+ | [1, NULL, 20, 3]| +------------------------------+ -- slice SELECT slice(array(1, 2, 3, 4), 2, 2); +------------------------------+ |slice(array(1, 2, 3, 4), 2, 2)| +------------------------------+ | [2, 3]| +------------------------------+ SELECT slice(array(1, 2, 3, 4), -2, 2); +-------------------------------+ |slice(array(1, 2, 3, 4), -2, 2)| +-------------------------------+ | [3, 4]| +-------------------------------+ -- sort_array SELECT sort_array(array('b', 'd', null, 'c', 'a'), true); +-----------------------------------------+ |sort_array(array(b, d, NULL, c, a), true)| +-----------------------------------------+ | [NULL, a, b, c, d]| +-----------------------------------------+