Applies a lambda expression to elements in an array and returns a new array. The function applies the lambda expression to each element in the array and returns the corresponding result.
array_map(lambda, ARRAY<T> arr1, [ARRAY<T> arr2, ...])
lambda:lambda expression used to define transformation rulesarr1, arr2, ...:ARRAY type, arrays to be transformed. Supports one or more array parameters.Supported types for T:
Return type: ARRAY
Return value meaning:
Usage notes:
Query Examples:
Square each element in the array:
SELECT array_map(x -> x * x, [1, 2, 3, 4, 5]); +------------------------------------------+ | array_map(x -> x * x, [1, 2, 3, 4, 5]) | +------------------------------------------+ | [1, 4, 9, 16, 25] | +------------------------------------------+
Round each element in a floating-point array:
SELECT array_map(x -> round(x), [1.1, 2.7, 3.3, 4.9, 5.5]); +--------------------------------------------------+ | array_map(x -> round(x), [1.1, 2.7, 3.3, 4.9, 5.5]) | +--------------------------------------------------+ | [1, 3, 3, 5, 6] | +--------------------------------------------------+
Calculate the length of each element in a string array:
SELECT array_map(x -> length(x), ['a', 'bb', 'ccc', 'dddd', 'eeeee']); +--------------------------------------------------+ | array_map(x -> length(x), ['a', 'bb', 'ccc', 'dddd', 'eeeee']) | +--------------------------------------------------+ | [1, 2, 3, 4, 5] | +--------------------------------------------------+
Process an array containing null values:
SELECT array_map(x -> x is not null, [1, null, 3, null, 5]); +--------------------------------------------------+ | array_map(x -> x is not null, [1, null, 3, null, 5]) | +--------------------------------------------------+ | [1, 0, 1, 0, 1] | +--------------------------------------------------+
NULL can not apply to array_map, will meet error:
mysql> SELECT array_map(x->x>2, NULL); ERROR 1105 (HY000): errCode = 2, detailMessage = lambda argument must be array but is NULL
Multiple array parameters example, adding corresponding elements from two arrays:
SELECT array_map((x, y) -> x + y, [1, 2, 3, 4, 5], [10, 20, 30, 40, 50]); +--------------------------------------------------+ | array_map((x, y) -> x + y, [1, 2, 3, 4, 5], [10, 20, 30, 40, 50]) | +--------------------------------------------------+ | [11, 22, 33, 44, 55] | +--------------------------------------------------+
Nested array processing, calculating the length of each sub-array:
SELECT array_map(x -> size(x), [[1,2],[3,4,5],[6],[7,8,9,10]]); +--------------------------------------------------+ | array_map(x -> size(x), [[1,2],[3,4,5],[6],[7,8,9,10]]) | +--------------------------------------------------+ | [2, 3, 1, 4] | +--------------------------------------------------+
Map type processing, extracting the value with key ‘a’ from each map:
SELECT array_map(x -> x['a'], [{'a':1,'b':2}, {'a':3,'b':4}, {'a':5,'b':6}]); +--------------------------------------------------+ | array_map(x -> x['a'], [{'a':1,'b':2}, {'a':3,'b':4}, {'a':5,'b':6}]) | +--------------------------------------------------+ | [1, 3, 5] | +--------------------------------------------------+
Error when parameter count is wrong:
SELECT array_map(); ERROR 1105 (HY000): errCode = 2, detailMessage = Can not found function 'array_map' which has 0 arity. Candidate functions are: [array_map(Expression, Expression...)]
Error when the number of parameters in lambda expression doesn't match the number of array parameters:
SELECT array_map(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_map(x -> x * 2, 'not_an_array'); ERROR 1105 (HY000): errCode = 2, detailMessage = Can not find the compatibility function signature: array_map(Expression, VARCHAR(12))
ARRAY, MAP, ARRAY_MAP