array_first_index

SinceVersion 2.0

array_first_index

description

Syntax

ARRAY<T> array_first_index(lambda, ARRAY<T> array1, ...)

使用lambda表达式作为输入参数,对其他输入ARRAY参数的内部数据进行相应的表达式计算。 返回第一个使得 lambda(array1[i], ...) 返回值不为 0 的索引。如果没找到满足此条件的索引,则返回 0。

在lambda表达式中输入的参数为1个或多个,所有输入的array的元素数量必须一致。在lambda中可以执行合法的标量函数,不支持聚合函数等。

  1. array_first_index(x->x>1, array1);
  2. array_first_index(x->(x%2 = 0), array1);
  3. array_first_index(x->(abs(x)-1), array1);
  4. array_first_index((x,y)->(x = y), array1, array2);

example

  1. mysql> select array_first_index(x->x+1>3, [2, 3, 4]);
  2. +-------------------------------------------------------------------+
  3. | array_first_index(array_map([x] -> x(0) + 1 > 3, ARRAY(2, 3, 4))) |
  4. +-------------------------------------------------------------------+
  5. | 2 |
  6. +-------------------------------------------------------------------+
  7. mysql> select array_first_index(x -> x is null, [null, 1, 2]);
  8. +----------------------------------------------------------------------+
  9. | array_first_index(array_map([x] -> x(0) IS NULL, ARRAY(NULL, 1, 2))) |
  10. +----------------------------------------------------------------------+
  11. | 1 |
  12. +----------------------------------------------------------------------+
  13. mysql> select array_first_index(x->power(x,2)>10, [1, 2, 3, 4]);
  14. +---------------------------------------------------------------------------------+
  15. | array_first_index(array_map([x] -> power(x(0), 2.0) > 10.0, ARRAY(1, 2, 3, 4))) |
  16. +---------------------------------------------------------------------------------+
  17. | 4 |
  18. +---------------------------------------------------------------------------------+
  19. mysql> select col2, col3, array_first_index((x,y)->x>y, col2, col3) from array_test;
  20. +--------------+--------------+---------------------------------------------------------------------+
  21. | col2 | col3 | array_first_index(array_map([x, y] -> x(0) > y(1), `col2`, `col3`)) |
  22. +--------------+--------------+---------------------------------------------------------------------+
  23. | [1, 2, 3] | [3, 4, 5] | 0 |
  24. | [1, NULL, 2] | [NULL, 3, 1] | 3 |
  25. | [1, 2, 3] | [9, 8, 7] | 0 |
  26. | NULL | NULL | 0 |
  27. +--------------+--------------+---------------------------------------------------------------------+

keywords

ARRAY,FIRST_INDEX,ARRAY_FIRST_INDEX