array_last_index

SinceVersion 2.0

array_last_index

description

Syntax

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

Use an lambda expression as an input parameter to perform corresponding expression calculations on the internal data of other input ARRAY parameters. Returns the last index such that the return value of lambda(array1[i], ...) is not 0. Return 0 if such index is not found.

There are one or more parameters input in the lambda expression, and the number of elements of all input arrays must be the same. Legal scalar functions can be executed in lambda, aggregate functions, etc. are not supported.

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

example

  1. mysql> select array_last_index(x -> x is null, [null, null, 1, 2]);
  2. +------------------------------------------------------------------------+
  3. | array_last_index(array_map([x] -> x IS NULL, ARRAY(NULL, NULL, 1, 2))) |
  4. +------------------------------------------------------------------------+
  5. | 2 |
  6. +------------------------------------------------------------------------+
  7. mysql> select array_last_index(x->x='s', ['a', 's', 's', 's', 'b']);
  8. +-----------------------------------------------------------------------------+
  9. | array_last_index(array_map([x] -> x = 's', ARRAY('a', 's', 's', 's', 'b'))) |
  10. +-----------------------------------------------------------------------------+
  11. | 4 |
  12. +-----------------------------------------------------------------------------+
  13. mysql> select array_last_index(x->power(x,2)>10, [1, 4, 3, 4]);
  14. +-----------------------------------------------------------------------------+
  15. | array_last_index(array_map([x] -> power(x, 2.0) > 10.0, ARRAY(1, 4, 3, 4))) |
  16. +-----------------------------------------------------------------------------+
  17. | 4 |
  18. +-----------------------------------------------------------------------------+
  19. mysql> select col2, col3, array_last_index((x,y)->x>y, col2, col3) from array_test;
  20. +--------------+--------------+---------------------------------------------------------------------+
  21. | col2 | col3 | array_last_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_last_index