array_first

SinceVersion 2.0

array_first

description

Returns the first element in the array for which func(arr1[i]) returns something other than 0.

Syntax

  1. T array_first(lambda, ARRAY<T>)

Use a lambda bool expression and an array as the input parameters, the lambda expression is used to evaluate the internal data of other input ARRAY parameters.

notice

Only supported in vectorized engine

example

  1. mysql> select array_first(x->x>2, [1,2,3,0]) ;
  2. +------------------------------------------------------------------------------------------------+
  3. | array_first(array_filter(ARRAY(1, 2, 3, 0), array_map([x] -> x(0) > 2, ARRAY(1, 2, 3, 0))), -1) |
  4. +------------------------------------------------------------------------------------------------+
  5. | 3 |
  6. +------------------------------------------------------------------------------------------------+
  7. mysql> select array_first(x->x>4, [1,2,3,0]) ;
  8. +------------------------------------------------------------------------------------------------+
  9. | array_first(array_filter(ARRAY(1, 2, 3, 0), array_map([x] -> x(0) > 4, ARRAY(1, 2, 3, 0))), -1) |
  10. +------------------------------------------------------------------------------------------------+
  11. | NULL |
  12. +------------------------------------------------------------------------------------------------+
  13. mysql> select array_first(x->x>1, [1,2,3,0]) ;
  14. +---------------------------------------------------------------------------------------------+
  15. | array_first(array_filter(ARRAY(1, 2, 3, 0), array_map([x] -> x > 1, ARRAY(1, 2, 3, 0))), 1) |
  16. +---------------------------------------------------------------------------------------------+
  17. | 2 |
  18. +---------------------------------------------------------------------------------------------+

keywords

ARRAY, LAST, array_first