array_filter

SinceVersion 2.0

array_filter(lambda,array)

SinceVersion 2.0.2

array array_filter(array arr, array_bool filter_column)

description

Syntax

  1. ARRAY<T> array_filter(lambda, ARRAY<T> arr)
  2. ARRAY<T> array_filter(ARRAY<T> arr, ARRAY<Bool> filter_column)

使用lambda表达式作为输入参数,计算筛选另外的输入参数ARRAY列的数据。 并过滤掉在结果中0和NULL的值。

  1. array_filter(x->x>0, array1);
  2. array_filter(x->(x+2)=10, array1);
  3. array_filter(x->(abs(x)-2)>0, array1);
  4. array_filter(c_array,[0,1,0]);

example

  1. mysql [test]>select c_array,array_filter(c_array,[0,1,0]) from array_test;
  2. +-----------------+----------------------------------------------------+
  3. | c_array | array_filter(`c_array`, ARRAY(FALSE, TRUE, FALSE)) |
  4. +-----------------+----------------------------------------------------+
  5. | [1, 2, 3, 4, 5] | [2] |
  6. | [6, 7, 8] | [7] |
  7. | [] | [] |
  8. | NULL | NULL |
  9. +-----------------+----------------------------------------------------+
  10. mysql [test]>select array_filter(x->(x > 1),[1,2,3,0,null]);
  11. +----------------------------------------------------------------------------------------------+
  12. | array_filter(ARRAY(1, 2, 3, 0, NULL), array_map([x] -> (x(0) > 1), ARRAY(1, 2, 3, 0, NULL))) |
  13. +----------------------------------------------------------------------------------------------+
  14. | [2, 3] |
  15. +----------------------------------------------------------------------------------------------+
  16. mysql [test]>select *, array_filter(x->x>0,c_array2) from array_test2;
  17. +------+-----------------+-------------------------+------------------------------------------------------------------+
  18. | id | c_array1 | c_array2 | array_filter(`c_array2`, array_map([x] -> x(0) > 0, `c_array2`)) |
  19. +------+-----------------+-------------------------+------------------------------------------------------------------+
  20. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [10, 20, 80] |
  21. | 2 | [6, 7, 8] | [10, 12, 13] | [10, 12, 13] |
  22. | 3 | [1] | [-100] | [] |
  23. | 4 | NULL | NULL | NULL |
  24. +------+-----------------+-------------------------+------------------------------------------------------------------+
  25. 4 rows in set (0.01 sec)
  26. mysql [test]>select *, array_filter(x->x%2=0,c_array2) from array_test2;
  27. +------+-----------------+-------------------------+----------------------------------------------------------------------+
  28. | id | c_array1 | c_array2 | array_filter(`c_array2`, array_map([x] -> x(0) % 2 = 0, `c_array2`)) |
  29. +------+-----------------+-------------------------+----------------------------------------------------------------------+
  30. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [10, 20, -40, 80, -100] |
  31. | 2 | [6, 7, 8] | [10, 12, 13] | [10, 12] |
  32. | 3 | [1] | [-100] | [-100] |
  33. | 4 | NULL | NULL | NULL |
  34. +------+-----------------+-------------------------+----------------------------------------------------------------------+
  35. mysql [test]>select *, array_filter(x->(x*(-10)>0),c_array2) from array_test2;
  36. +------+-----------------+-------------------------+----------------------------------------------------------------------------+
  37. | id | c_array1 | c_array2 | array_filter(`c_array2`, array_map([x] -> (x(0) * (-10) > 0), `c_array2`)) |
  38. +------+-----------------+-------------------------+----------------------------------------------------------------------------+
  39. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [-40, -100] |
  40. | 2 | [6, 7, 8] | [10, 12, 13] | [] |
  41. | 3 | [1] | [-100] | [-100] |
  42. | 4 | NULL | NULL | NULL |
  43. +------+-----------------+-------------------------+----------------------------------------------------------------------------+
  44. mysql [test]>select *, array_filter(x->x>0, array_map((x,y)->(x>y), c_array1,c_array2)) as res from array_test2;
  45. +------+-----------------+-------------------------+--------+
  46. | id | c_array1 | c_array2 | res |
  47. +------+-----------------+-------------------------+--------+
  48. | 1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [1, 1] |
  49. | 2 | [6, 7, 8] | [10, 12, 13] | [] |
  50. | 3 | [1] | [-100] | [1] |
  51. | 4 | NULL | NULL | NULL |
  52. +------+-----------------+-------------------------+--------+

keywords

ARRAY,FILTER,ARRAY_FILTER