array_map

SinceVersion dev

array_map(lambda,array1,array2….)

description

Syntax

ARRAY<T> array_map(lambda, ARRAY<T> array1, ARRAY<T> array2)

使用一个lambda表达式作为输入参数,对其他的输入ARRAY参数的内部数据做对应表达式计算。 在lambda表达式中输入的参数为1个或多个,必须和后面的输入array列数量一致。 在lambda中可以执行合法的标量函数,不支持聚合函数等。

  1. array_map(x->x, array1);
  2. array_map(x->(x+2), array1);
  3. array_map(x->(abs(x)-2), array1);
  4. array_map((x,y)->(x = y), array1, array2);
  5. array_map((x,y)->(power(x,2)+y), array1, array2);
  6. array_map((x,y,z)->(abs(x)+y*z), array1, array2, array3);

example

  1. mysql [test]>select *, array_map(x->x,[1,2,3]) from array_test2 order by id;
  2. +------+-----------------+-------------------------+----------------------------------------+
  3. | id | c_array1 | c_array2 | array_map([x] -> x(0), ARRAY(1, 2, 3)) |
  4. +------+-----------------+-------------------------+----------------------------------------+
  5. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [1, 2, 3] |
  6. | 2 | [6, 7, 8] | [10, 12, 13] | [1, 2, 3] |
  7. | 3 | [1] | [-100] | [1, 2, 3] |
  8. | 4 | NULL | NULL | [1, 2, 3] |
  9. +------+-----------------+-------------------------+----------------------------------------+
  10. 4 rows in set (0.02 sec)
  11. mysql [test]>select *, array_map(x->x+2,[1,2,3]) from array_test2 order by id;
  12. +------+-----------------+-------------------------+--------------------------------------------+
  13. | id | c_array1 | c_array2 | array_map([x] -> x(0) + 2, ARRAY(1, 2, 3)) |
  14. +------+-----------------+-------------------------+--------------------------------------------+
  15. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [3, 4, 5] |
  16. | 2 | [6, 7, 8] | [10, 12, 13] | [3, 4, 5] |
  17. | 3 | [1] | [-100] | [3, 4, 5] |
  18. | 4 | NULL | NULL | [3, 4, 5] |
  19. +------+-----------------+-------------------------+--------------------------------------------+
  20. 4 rows in set (0.02 sec)
  21. mysql [test]>select c_array1, c_array2, array_map(x->x,[1,2,3]) from array_test2 order by id;
  22. +-----------------+-------------------------+----------------------------------------+
  23. | c_array1 | c_array2 | array_map([x] -> x(0), ARRAY(1, 2, 3)) |
  24. +-----------------+-------------------------+----------------------------------------+
  25. | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [1, 2, 3] |
  26. | [6, 7, 8] | [10, 12, 13] | [1, 2, 3] |
  27. | [1] | [-100] | [1, 2, 3] |
  28. | NULL | NULL | [1, 2, 3] |
  29. +-----------------+-------------------------+----------------------------------------+
  30. 4 rows in set (0.01 sec)
  31. mysql [test]>select c_array1, c_array2, array_map(x->power(x,2),[1,2,3]) from array_test2 order by id;
  32. +-----------------+-------------------------+----------------------------------------------------+
  33. | c_array1 | c_array2 | array_map([x] -> power(x(0), 2.0), ARRAY(1, 2, 3)) |
  34. +-----------------+-------------------------+----------------------------------------------------+
  35. | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [1, 4, 9] |
  36. | [6, 7, 8] | [10, 12, 13] | [1, 4, 9] |
  37. | [1] | [-100] | [1, 4, 9] |
  38. | NULL | NULL | [1, 4, 9] |
  39. +-----------------+-------------------------+----------------------------------------------------+
  40. mysql [test]>select c_array1, c_array2, array_map((x,y)->x+y,c_array1,c_array2) from array_test2 order by id;
  41. +-----------------+-------------------------+----------------------------------------------------------+
  42. | c_array1 | c_array2 | array_map([x, y] -> x(0) + y(1), `c_array1`, `c_array2`) |
  43. +-----------------+-------------------------+----------------------------------------------------------+
  44. | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [11, 22, -37, 84, -95] |
  45. | [6, 7, 8] | [10, 12, 13] | [16, 19, 21] |
  46. | [1] | [-100] | [-99] |
  47. | NULL | NULL | NULL |
  48. +-----------------+-------------------------+----------------------------------------------------------+
  49. 4 rows in set (0.02 sec)
  50. mysql [test]>select c_array1, c_array2, array_map((x,y)->power(x,2)+y,c_array1, c_array2) from array_test2 order by id;
  51. +-----------------+-------------------------+----------------------------------------------------------------------+
  52. | c_array1 | c_array2 | array_map([x, y] -> power(x(0), 2.0) + y(1), `c_array1`, `c_array2`) |
  53. +-----------------+-------------------------+----------------------------------------------------------------------+
  54. | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [11, 24, -31, 96, -75] |
  55. | [6, 7, 8] | [10, 12, 13] | [46, 61, 77] |
  56. | [1] | [-100] | [-99] |
  57. | NULL | NULL | NULL |
  58. +-----------------+-------------------------+----------------------------------------------------------------------+
  59. 4 rows in set (0.03 sec)
  60. mysql [test]>select *,array_map(x->x=3,c_array1) from array_test2 order by id;
  61. +------+-----------------+-------------------------+----------------------------------------+
  62. | id | c_array1 | c_array2 | array_map([x] -> x(0) = 3, `c_array1`) |
  63. +------+-----------------+-------------------------+----------------------------------------+
  64. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [0, 0, 1, 0, 0] |
  65. | 2 | [6, 7, 8] | [10, 12, 13] | [0, 0, 0] |
  66. | 3 | [1] | [-100] | [0] |
  67. | 4 | NULL | NULL | NULL |
  68. +------+-----------------+-------------------------+----------------------------------------+
  69. 4 rows in set (0.02 sec)
  70. mysql [test]>select *,array_map(x->x>3,c_array1) from array_test2 order by id;
  71. +------+-----------------+-------------------------+----------------------------------------+
  72. | id | c_array1 | c_array2 | array_map([x] -> x(0) > 3, `c_array1`) |
  73. +------+-----------------+-------------------------+----------------------------------------+
  74. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [0, 0, 0, 1, 1] |
  75. | 2 | [6, 7, 8] | [10, 12, 13] | [1, 1, 1] |
  76. | 3 | [1] | [-100] | [0] |
  77. | 4 | NULL | NULL | NULL |
  78. +------+-----------------+-------------------------+----------------------------------------+
  79. 4 rows in set (0.02 sec)
  80. mysql [test]>select *,array_map((x,y)->x>y,c_array1,c_array2) from array_test2 order by id;
  81. +------+-----------------+-------------------------+----------------------------------------------------------+
  82. | id | c_array1 | c_array2 | array_map([x, y] -> x(0) > y(1), `c_array1`, `c_array2`) |
  83. +------+-----------------+-------------------------+----------------------------------------------------------+
  84. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [0, 0, 1, 0, 1] |
  85. | 2 | [6, 7, 8] | [10, 12, 13] | [0, 0, 0] |
  86. | 3 | [1] | [-100] | [1] |
  87. | 4 | NULL | NULL | NULL |
  88. +------+-----------------+-------------------------+----------------------------------------------------------+
  89. 4 rows in set (0.02 sec)
  90. mysql [test]>select array_map(x->cast(x as string), c_array1) from test_array_map_function;
  91. +-----------------+-------------------------------------------------------+
  92. | c_array1 | array_map([x] -> CAST(x(0) AS CHARACTER), `c_array1`) |
  93. +-----------------+-------------------------------------------------------+
  94. | [1, 2, 3, 4, 5] | ['1', '2', '3', '4', '5'] |
  95. | [6, 7, 8] | ['6', '7', '8'] |
  96. | [] | [] |
  97. | NULL | NULL |
  98. +-----------------+-------------------------------------------------------+
  99. 4 rows in set (0.01 sec)

keywords

ARRAY,MAP,ARRAY_MAP