Finds the position index (starting from 1) of the last element in the array that satisfies the lambda expression. Finds the last element that satisfies the condition and returns its position index.
array_last_index(lambda, ARRAY<T> arr1, [ARRAY<T> arr2, ...])
lambda:lambda expression used to define search conditionsarr1, arr2, ...:ARRAY type, arrays to search. Supports one or more array parameters.Supported types for T:
Return type: BIGINT
Return value meaning:
Usage notes:
Query Examples:
Find the position index of the last element greater than or equal to 3 in a floating-point array:
SELECT array_last_index(x -> x >= 3, [1.1, 2.2, 3.3, 4.4, 5.5]); +----------------------------------------------------------+ | array_last_index(x -> x >= 3, [1.1, 2.2, 3.3, 4.4, 5.5]) | +----------------------------------------------------------+ | 5 | +----------------------------------------------------------+
Find the position index of the last element with length greater than 2 in a string array:
SELECT array_last_index(x -> length(x) > 2, ['a', 'bb', 'ccc', 'dddd', 'eeeee']); +---------------------------------------------------------------------------+ | array_last_index(x -> length(x) > 2, ['a', 'bb', 'ccc', 'dddd', 'eeeee']) | +---------------------------------------------------------------------------+ | 5 | +---------------------------------------------------------------------------+
Empty array returns 0:
SELECT array_last_index(x -> x > 0, []); +----------------------------------------+ | array_last_index(x -> x > 0, []) | +----------------------------------------+ | 0 | +----------------------------------------+
NULL array and lambda expression combination. When there is a lambda expression with NULL, it will error. When there is no lambda expression, it returns 0:
SELECT array_last_index(NULL); +-------------------------+ | array_last_index(NULL) | +-------------------------+ | 0 | +-------------------------+ SELECT array_last_index(x -> x > 2, NULL); ERROR 1105 (HY000): errCode = 2, detailMessage = lambda argument must be array but is NULL
Array containing null values, lambda can check for null:
SELECT array_last_index(x -> x is not null, [null, 1, null, 3, null, 5]); +-------------------------------------------------------------------+ | array_last_index(x -> x is not null, [null, 1, null, 3, null, 5]) | +-------------------------------------------------------------------+ | 6 | +-------------------------------------------------------------------+
Multi-array search, find the position index of the last element where the first array is greater than the second array:
SELECT array_last_index((x, y) -> x > y, [1, 2, 3, 4, 5], [1.1, 2.2, 3.3, 4.4, 5.5]); +-------------------------------------------------------------------------------+ | array_last_index((x, y) -> x > y, [1, 2, 3, 4, 5], [1.1, 2.2, 3.3, 4.4, 5.5]) | +-------------------------------------------------------------------------------+ | 0 | +-------------------------------------------------------------------------------+
Nested array search, find the position index of the last element where each sub-array length is greater than 2:
SELECT array_last_index(x -> size(x) > 2, [[1,2],[3,4,5],[6],[7,8,9,10]]); +--------------------------------------------------------------------+ | array_last_index(x -> size(x) > 2, [[1,2],[3,4,5],[6],[7,8,9,10]]) | +--------------------------------------------------------------------+ | 4 | +--------------------------------------------------------------------+
Map type search, find the position index of the last element where the value with key ‘a’ is greater than 10:
SELECT array_last_index(x -> x['a'] > 10, [{'a':5}, {'a':15}, {'a':20}]); +-------------------------------------------------------------------+ | array_last_index(x -> x['a'] > 10, [{'a':5}, {'a':15}, {'a':20}]) | +-------------------------------------------------------------------+ | 3 | +-------------------------------------------------------------------+
Error when parameter count is wrong:
SELECT array_last_index(); ERROR 1105 (HY000): errCode = 2, detailMessage = Can not found function 'array_last_index' which has 0 arity. Candidate functions are: [array_last_index(Expression, Expression...)]
Error when the number of parameters in lambda expression doesn't match the number of array parameters:
SELECT array_last_index(x -> x > 0, [1,2,3], [4,5,6], [7,8,9]); ERROR 1105 (HY000): errCode = 2, detailMessage = lambda x -> (x > 0) arguments' size is not equal parameters' size
Error when passing non-array type:
SELECT array_last_index(x -> x > 0, 'not_an_array'); ERROR 1105 (HY000): errCode = 2, detailMessage = Can not find the compatibility function signature: array_last_index(Expression, VARCHAR(12))
ARRAY, LAST, INDEX, ARRAY_LAST_INDEX