array_slice

array_slice

description

Syntax

ARRAY<T> array_slice(ARRAY<T> arr, BIGINT off, BIGINT len)

返回一个子数组,包含所有从指定位置开始的指定长度的元素,如果输入参数为NULL,则返回NULL

  1. 如果off是正数,则表示从左侧开始的偏移量
  2. 如果off是负数,则表示从右侧开始的偏移量
  3. 当指定的off不在数组的实际范围内,返回空数组
  4. 如果len是负数,则表示长度为0

example

  1. mysql> select k2, k2[2:2] from array_type_table_nullable;
  2. +-----------------+-------------------------+
  3. | k2 | array_slice(`k2`, 2, 2) |
  4. +-----------------+-------------------------+
  5. | [1, 2, 3] | [2, 3] |
  6. | [1, NULL, 3] | [NULL, 3] |
  7. | [2, 3] | [3] |
  8. | NULL | NULL |
  9. +-----------------+-------------------------+
  10. mysql> select k2, array_slice(k2, 2, 2) from array_type_table_nullable;
  11. +-----------------+-------------------------+
  12. | k2 | array_slice(`k2`, 2, 2) |
  13. +-----------------+-------------------------+
  14. | [1, 2, 3] | [2, 3] |
  15. | [1, NULL, 3] | [NULL, 3] |
  16. | [2, 3] | [3] |
  17. | NULL | NULL |
  18. +-----------------+-------------------------+
  19. mysql> select k2, k2[2:2] from array_type_table_nullable_varchar;
  20. +----------------------------+-------------------------+
  21. | k2 | array_slice(`k2`, 2, 2) |
  22. +----------------------------+-------------------------+
  23. | ['hello', 'world', 'c++'] | ['world', 'c++'] |
  24. | ['a1', 'equals', 'b1'] | ['equals', 'b1'] |
  25. | ['hasnull', NULL, 'value'] | [NULL, 'value'] |
  26. | ['hasnull', NULL, 'value'] | [NULL, 'value'] |
  27. +----------------------------+-------------------------+
  28. mysql> select k2, array_slice(k2, 2, 2) from array_type_table_nullable_varchar;
  29. +----------------------------+-------------------------+
  30. | k2 | array_slice(`k2`, 2, 2) |
  31. +----------------------------+-------------------------+
  32. | ['hello', 'world', 'c++'] | ['world', 'c++'] |
  33. | ['a1', 'equals', 'b1'] | ['equals', 'b1'] |
  34. | ['hasnull', NULL, 'value'] | [NULL, 'value'] |
  35. | ['hasnull', NULL, 'value'] | [NULL, 'value'] |
  36. +----------------------------+-------------------------+

当指定off为负数:

  1. mysql> select k2, k2[-2:1] from array_type_table_nullable;
  2. +-----------+--------------------------+
  3. | k2 | array_slice(`k2`, -2, 1) |
  4. +-----------+--------------------------+
  5. | [1, 2, 3] | [2] |
  6. | [1, 2, 3] | [2] |
  7. | [2, 3] | [2] |
  8. | [2, 3] | [2] |
  9. +-----------+--------------------------+
  10. mysql> select k2, array_slice(k2, -2, 1) from array_type_table_nullable;
  11. +-----------+--------------------------+
  12. | k2 | array_slice(`k2`, -2, 1) |
  13. +-----------+--------------------------+
  14. | [1, 2, 3] | [2] |
  15. | [1, 2, 3] | [2] |
  16. | [2, 3] | [2] |
  17. | [2, 3] | [2] |
  18. +-----------+--------------------------+
  19. mysql> select k2, k2[-2:2] from array_type_table_nullable_varchar;
  20. +----------------------------+--------------------------+
  21. | k2 | array_slice(`k2`, -2, 2) |
  22. +----------------------------+--------------------------+
  23. | ['hello', 'world', 'c++'] | ['world', 'c++'] |
  24. | ['a1', 'equals', 'b1'] | ['equals', 'b1'] |
  25. | ['hasnull', NULL, 'value'] | [NULL, 'value'] |
  26. | ['hasnull', NULL, 'value'] | [NULL, 'value'] |
  27. +----------------------------+--------------------------+
  28. mysql> select k2, array_slice(k2, -2, 2) from array_type_table_nullable_varchar;
  29. +----------------------------+--------------------------+
  30. | k2 | array_slice(`k2`, -2, 2) |
  31. +----------------------------+--------------------------+
  32. | ['hello', 'world', 'c++'] | ['world', 'c++'] |
  33. | ['a1', 'equals', 'b1'] | ['equals', 'b1'] |
  34. | ['hasnull', NULL, 'value'] | [NULL, 'value'] |
  35. | ['hasnull', NULL, 'value'] | [NULL, 'value'] |
  36. +----------------------------+--------------------------+
  1. mysql> select k2, array_slice(k2, 0) from array_type_table;
  2. +-----------+-------------------------+
  3. | k2 | array_slice(`k2`, 0) |
  4. +-----------+-------------------------+
  5. | [1, 2, 3] | [] |
  6. +-----------+-------------------------+
  7. mysql> select k2, array_slice(k2, -5) from array_type_table;
  8. +-----------+----------------------+
  9. | k2 | array_slice(`k2`, -5) |
  10. +-----------+----------------------+
  11. | [1, 2, 3] | [] |
  12. +-----------+----------------------+

keywords

ARRAY,SLICE,ARRAY_SLICE