Array functions - Amazon Timestream

Array functions

Timestream for LiveAnalytics supports the following array functions.

Function Output data type Description

array_distinct(x)

array

Remove duplicate values from the array x.

SELECT array_distinct(ARRAY[1,2,2,3])

Example result: [ 1,2,3 ]

array_intersect(x, y)

array

Returns an array of the elements in the intersection of x and y, without duplicates.

SELECT array_intersect(ARRAY[1,2,3], ARRAY[3,4,5])

Example result: [ 3 ]

array_union(x, y)

array

Returns an array of the elements in the union of x and y, without duplicates.

SELECT array_union(ARRAY[1,2,3], ARRAY[3,4,5])

Example result: [ 1,2,3,4,5 ]

array_except(x, y)

array

Returns an array of elements in x but not in y, without duplicates.

SELECT array_except(ARRAY[1,2,3], ARRAY[3,4,5])

Example result: [ 1,2 ]

array_join(x, delimiter, null_replacement)

varchar

Concatenates the elements of the given array using the delimiter and an optional string to replace nulls.

SELECT array_join(ARRAY[1,2,3], ';', '')

Example result: 1;2;3

array_max(x)

same as array elements

Returns the maximum value of input array.

SELECT array_max(ARRAY[1,2,3])

Example result: 3

array_min(x)

same as array elements

Returns the minimum value of input array.

SELECT array_min(ARRAY[1,2,3])

Example result: 1

array_position(x, element)

bigint

Returns the position of the first occurrence of the element in array x (or 0 if not found).

SELECT array_position(ARRAY[3,4,5,9], 5)

Example result: 3

array_remove(x, element)

array

Remove all elements that equal element from array x.

SELECT array_remove(ARRAY[3,4,5,9], 4)

Example result: [ 3,5,9 ]

array_sort(x)

array

Sorts and returns the array x. The elements of x must be orderable. Null elements will be placed at the end of the returned array.

SELECT array_sort(ARRAY[6,8,2,9,3])

Example result: [ 2,3,6,8,9 ]

arrays_overlap(x, y)

boolean

Tests if arrays x and y have any non-null elements in common. Returns null if there are no non-null elements in common but either array contains null.

SELECT arrays_overlap(ARRAY[6,8,2,9,3], ARRAY[6,8])

Example result: true

cardinality(x)

bigint

Returns the size of the array x.

SELECT cardinality(ARRAY[6,8,2,9,3])

Example result: 5

concat(array1, array2, ..., arrayN)

array

Concatenates the arrays array1, array2, ..., arrayN.

SELECT concat(ARRAY[6,8,2,9,3], ARRAY[11,32], ARRAY[6,8,2,0,14])

Example result: [ 6,8,2,9,3,11,32,6,8,2,0,14 ]

element_at(array(E), index)

E

Returns element of array at given index. If index < 0, element_at accesses elements from the last to the first.

SELECT element_at(ARRAY[6,8,2,9,3], 1)

Example result: 6

repeat(element, count)

array

Repeat element for count times.

SELECT repeat(1, 3)

Example result: [ 1,1,1 ]

reverse(x)

array

Returns an array which has the reversed order of array x.

SELECT reverse(ARRAY[6,8,2,9,3])

Example result: [ 3,9,2,8,6 ]

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.

SELECT sequence(3, 8)

Example result: [ 3,4,5,6,7,8 ]

sequence(start, stop, step)

array(bigint)

Generate a sequence of integers from start to stop, incrementing by step.

SELECT sequence(3, 15, 2)

Example result: [ 3,5,7,9,11,13,15 ]

sequence(start, stop)

array(timestamp)

Generate a sequence of timestamps from start date to stop date, incrementing by 1 day.

SELECT sequence('2023-04-02 19:26:12.941000000', '2023-04-06 19:26:12.941000000', 1d)

Example result: [ 2023-04-02 19:26:12.941000000,2023-04-03 19:26:12.941000000,2023-04-04 19:26:12.941000000,2023-04-05 19:26:12.941000000,2023-04-06 19:26:12.941000000 ]

sequence(start, stop, step)

array(timestamp)

Generate a sequence of timestamps from start to stop, incrementing by step. The data type of step is interval.

SELECT sequence('2023-04-02 19:26:12.941000000', '2023-04-10 19:26:12.941000000', 2d)

Example result: [ 2023-04-02 19:26:12.941000000,2023-04-04 19:26:12.941000000,2023-04-06 19:26:12.941000000,2023-04-08 19:26:12.941000000,2023-04-10 19:26:12.941000000 ]

shuffle(x)

array

Generate a random permutation of the given array x.

SELECT shuffle(ARRAY[6,8,2,9,3])

Example result: [ 6,3,2,9,8 ]

slice(x, start, length)

array

Subsets array x starting from index start (or starting from the end if start is negative) with a length of length.

SELECT slice(ARRAY[6,8,2,9,3], 1, 3)

Example result: [ 6,8,2 ]

zip(array1, array2[, ...])

array(row)

Merges the given arrays, element-wise, into a single array of rows. If the arguments have an uneven length, missing values are filled with NULL.

SELECT zip(ARRAY[6,8,2,9,3], ARRAY[15,24])

Example result: [ ( 6, 15 ),( 8, 24 ),( 2, - ),( 9, - ),( 3, - ) ]