Array functions and operators
Subscript operator: [] {subscript_operator}
The []
operator is used to access an element of an array and is
indexed starting from one:
Concatenation operator: || {concatenation_operator}
The ||
operator is used to concatenate an array with an array or an
element of the same type:
Array functions
allmatch()
allmatch(array(T), function(T,boolean))
→ boolean
Returns whether all elements of an array match the given predicate.
Returns true
if all the elements match the predicate (a special case
is when the array is empty); false
if one or more elements don’t
match; NULL
if the predicate function returns NULL
for one or more
elements and true
for all other elements.
anymatch()
any_match(array(T), function(T,boolean))
→ boolean
Returns whether any elements of an array match the given predicate.
Returns true
if one or more elements match the predicate; false
if
none of the elements matches (a special case is when the array is
empty); NULL
if the predicate function returns NULL
for one or more
elements and false
for all other elements.
array_distinct()
array_distinct(x)
→ array
Remove duplicate values from the array x
.
array_except()
array_except(x, y)
→ array
Returns an array of elements in x
but not in y
, without duplicates.
array_histogram()
array_histogram(x)
→ map<K, bigint>
Returns a map where the keys are the unique elements in the input array x
and the values are the number of times that each element appears in x
. Null values are ignored.
Returns an empty map if the input array has no non-null elements.
array_intersect()
array_intersect(x, y)
→ array
Returns an array of the elements in the intersection of x
and y
, without duplicates.
array_join()
array_join(x, delimiter, null_replacement)
→ varchar
Concatenates the elements of the given array using the delimiter and an optional string to replace nulls.
array_max()
array_max(x)
→ x
Returns the maximum value of input array.
array_min()
array_min(x)
→ x
Returns the minimum value of input array.
array_position()
array_position(x, element)
→ bigint
Returns the position of the f occurrence of the element in the array. Returns NULL
if the element is not found. The position is counted from 1.
array_remove()
array_remove(x, element)
→ array
Returns an array of elements in x
without the element element
.
array_sort()
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. The sort is stable, meaning that the relative order of elements that are equal is preserved.
array_sort(array(T), function(T,T,int))
→ array(T)
Sorts and returns the array
based on the given comparator function
. The comparator will take two nullable arguments representing two nullable elements of the array
. It returns -1, 0, or 1 as the f nullable element is less than, equal to, or greater than the second nullable element. If the comparator function returns other values (including NULL
), the query will fail and raise an error.
array_union()
array_union(x, y)
→ array
Returns an array of the elements in the union of x
and y
, without duplicates.
array_overlap()
array_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.
cardinality()
cardinality(x)
→ bigint
Returns the cardinality (size) of the array x.
concat()
concat(array1, array2, ..., arrayN)
→ array
Concatenates the arrays array1, array2, …, arrayN. This function provides the same functionality as the SQL-standard concatenation operator (||).
combinations()
combinations(array(T), n)
-> array(array(T))
Returns n-element sub-groups of input array. If the input array has no duplicates, combinations returns n-element subsets.
Order of sub-groups is deterministic but unspecified. Order of elements within a sub-group deterministic but unspecified. n must be not be greater than 5, and the total size of sub-groups generated must be smaller than 100,000.
contains()
contains(x, element)
→ boolean
Returns true if the array x contains the element.
contains_sequence()
contains_sequence(x, seq)
→ boolean
Return true if array x contains all of array seq as a subsequence (all values in the same consecutive order).
element_at()
element_at(array(E), index)
→ E
Returns element of array at given index. If index > 0, this function provides the same functionality as the SQL-standard subscript operator ([]), except that the function returns NULL when accessing an index larger than array length, whereas the subscript operator would fail in such a case. If index < 0, element_at accesses elements from the last to the first.
filter()
filter(array(T), function(T, boolean))
-> array(T)
Constructs an array from those elements of array for which function returns true:
flatten()
flatten(x)
→ array
Flattens an array(array(T)) to an array(T) by concatenating the contained arrays.
ngrams()
ngrams(array(T), n)
-> array(array(T))
Returns n-grams (sub-sequences of adjacent n elements) for the array. The order of the n-grams in the result is unspecified.
none_match()
none_match(array(T), function(T, boolean))
→ boolean
Returns whether no elements of an array match the given predicate. Returns true if none of the elements matches the predicate (a special case is when the array is empty); false if one or more elements match; NULL if the predicate function returns NULL for one or more elements and false for all other elements.
reduce()
reduce(array(T), initialState S, inputFunction(S, T, S), outputFunction(S, R))
→ R
Returns a single value reduced from array. inputFunction will be invoked for each element in array in order. In addition to taking the element, inputFunction takes the current state, initially initialState, and returns the new state. outputFunction will be invoked to turn the final state into the result value. It may be the identity function (i -> i).
repeat()
repeat(element, count)
→ array
Repeat element for count times.
reverse()
reverse(x)
→ array
Returns an array which has the reversed order of array x.
sequence()
sequence(start, stop)
Generate a sequence of integers from start to stop, incrementing by 1 if start is less than or equal to stop, otherwise -1.
sequence(start, stop, step)
Generate a sequence of integers from start to stop, incrementing by step.
sequence(start, stop)
Generate a sequence of dates from start date to stop date, incrementing by 1 day if start date is less than or equal to stop date, otherwise -1 day.
sequence(start, stop, step)
Generate a sequence of dates from start to stop, incrementing by step. The type of step can be either INTERVAL DAY TO SECOND or INTERVAL YEAR TO MONTH.
sequence(start, stop, step)
Generate a sequence of timestamps from start to stop, incrementing by step. The type of step can be either INTERVAL DAY TO SECOND or INTERVAL YEAR TO MONTH.
shuffle()
shuffle(x)
→ array
Generate a random permutation of the given array x.
slice()
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.
trim_array()
trim_array(x, n)
→ array
Remove n elements from the end of array:
transform()
transform(array(T), function(T, R))
→ array(R)
Returns an array of the results of applying the given function to each element of the given array. The function must be deterministic and must return the same type for each invocation with the same argument. If the function returns NULL, the result of the transform is NULL.
zip()
zip(array1, array2[, ...])
→ array(row)
Merges the given arrays, element-wise, into a single array of rows. The M-th element of the N-th argument will be the N-th field of the M-th output element. If the arguments have an uneven length, missing values are filled with NULL.
zip_with()
zip_with(array1, array2, function)
→ array(R)
Merges the given arrays, element-wise, into a single array using function. The M-th element of the N-th argument will be the N-th argument of the M-th invocation of function. If the arguments have an uneven length, missing values are filled with NULL.
Was this page helpful?