ALL_MATCH

Updated at:

Checks whether all elements in Array a meet the predicate condition.

Syntax

boolean all_match(array<T> <a>, function<T, boolean> <predicate>)

Parameters

  • a: required. This parameter specifies an array. T in array<T> specifies the data type of the elements in the array. The elements can be of any data type.

  • predicate: required. This parameter specifies the built-in function, user-defined function, or expression used to evaluate each element in array a. The data type of the input parameter must match the data type of the elements in array a.

Return value

A value of the BOOLEAN type is returned. The return value varies based on the following rules:

  • If all elements in Array a meet the predicate condition or the array is empty, True is returned.

  • If one or more elements in Array a do not meet the predicate condition, false is returned.

  • If an element in Array a is null and other elements meet the predicate condition, NULL is returned.

Examples

  • Example 1: Check whether all elements in array(4, 5, 6) meet the x x > 3 condition. This condition specifies that all elements are greater than 3. Sample statement:

    -- The return value is true. 
    select all_match(array(4, 5, 6), x -> x>3);
  • Example 2: The array is empty. Sample statement:

    -- The return value is true. 
    select all_match(array(), x -> x>3);
  • Example 3: Check whether all elements in array(1, 2, -10, 100, -30) meet the x-> x > 3 condition. Sample statement:

    -- The return value is false. 
    select all_match(array(1, 2, -10, 100, -30), x -> x>3);
  • Example 4: Check whether all elements in array(10, 100, 30, null) meet the x-> x > 3 condition. Sample statement:

    -- The return value is 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.