ARRAY_CONTAINS

Updated at:
Copy as MD

Checks whether an array contains a specific element.

Syntax

boolean array_contains(array<T> <a>, value <v>)

Parameters

Parameter Required Description
a Yes The array to search. T in array<T> specifies the element data type. Elements can be of any type.
v Yes The element to search for. v must be the same data type as elements in array a.

Return value

Returns true if array a contains element v. Returns false otherwise.

Examples

Sample table t_table_array with columns c1 (BIGINT) and t_array (ARRAY<STRING>):

+------------+--------------------+
| c1         | t_array            |
+------------+--------------------+
| 1000       | [k11, 86, k21, 15] |
| 1001       | [k12, 97, k22, 2]  |
| 1002       | [k13, 99, k23, 1]  |
+------------+--------------------+

Check whether t_array contains the string '1':

select c1, array_contains(t_array, '1') from t_table_array;

Output:

+------------+------+
| c1         | _c1  |
+------------+------+
| 1000       | false |
| 1001       | false |
| 1002       | true |
+------------+------+

Because t_array is of ARRAY<STRING> type, the function performs string comparison. '1' matches element 1 in row c1 = 1002, so the function returns true for that row.

Related functions

ARRAY_CONTAINS is a complex type function that processes ARRAY, MAP, STRUCT, and JSON data types. Complex type functions.