Checks whether all elements in an array satisfy a predicate. Returns true if all elements satisfy the predicate or the array is empty, false if one or more elements do not satisfy it, and NULL if the predicate returns NULL for one or more elements while all other elements satisfy the predicate.
Syntax
boolean all_match(array<T> <a>, function<T, boolean> <predicate>)Parameters
| Parameter | Required | Description |
|---|---|---|
a | Yes | The array to evaluate. T is the data type of the elements and can be any supported type. |
predicate | Yes | A built-in function, user-defined function (UDF), or expression that evaluates each element. The input type must match the element type T. |
Return value
| Condition | Return value |
|---|---|
| All elements satisfy the predicate, or the array is empty | true |
| One or more elements do not satisfy the predicate | false |
An element is NULL and all other elements satisfy the predicate | NULL |
Examples
All examples use a lambda expression (->) as the predicate. For lambda syntax, see Lambda functions.
All elements satisfy the condition
-- Returns true
SELECT all_match(array(4, 5, 6), x -> x > 3);Empty array
-- Returns true
SELECT all_match(array(), x -> x > 3);Some elements do not satisfy the condition
-- Returns false
SELECT all_match(array(1, 2, -10, 100, -30), x -> x > 3);Array contains NULL
-- Returns NULL: 10, 100, and 30 satisfy x > 3, but the NULL element causes the predicate to return NULL
SELECT all_match(array(10, 100, 30, null), x -> x > 3);Related functions
ALL_MATCH is a complex type function. For more information about the functions that are used to process data of complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.
In the preceding examples, the combination of a hyphen and a closing angle bracket (->) is used. For more information about how to use the combination of a hyphen and a closing angle bracket (->) in Lambda functions, see Lambda functions.