struct_element

SinceVersion 2.0

struct_element

description

Function allows getting a field from a struct.

Syntax

  1. struct_element(struct, n/s)

Arguments

  1. struct - The input struct column. If null, null will be returned.
  2. n - The position of fieldstarting from 1only supports constants.
  3. s - The name of fieldonly supports constants.

Returned value

Returns the specified field column, of any type.

notice

Only supported in vectorized engine

example

  1. mysql> select struct_element(named_struct('f1', 1, 'f2', 'a'), 'f2');
  2. +--------------------------------------------------------+
  3. | struct_element(named_struct('f1', 1, 'f2', 'a'), 'f2') |
  4. +--------------------------------------------------------+
  5. | a |
  6. +--------------------------------------------------------+
  7. 1 row in set (0.03 sec)
  8. mysql> select struct_element(named_struct('f1', 1, 'f2', 'a'), 1);
  9. +-----------------------------------------------------+
  10. | struct_element(named_struct('f1', 1, 'f2', 'a'), 1) |
  11. +-----------------------------------------------------+
  12. | 1 |
  13. +-----------------------------------------------------+
  14. 1 row in set (0.02 sec)
  15. mysql> select struct_col, struct_element(struct_col, 'f1') from test_struct;
  16. +-------------------------------------------------+-------------------------------------+
  17. | struct_col | struct_element(`struct_col `, 'f1') |
  18. +-------------------------------------------------+-------------------------------------+
  19. | {1, 2, 3, 4, 5} | 1 |
  20. | {1, 1000, 10000000, 100000000000, 100000000000} | 1 |
  21. | {5, 4, 3, 2, 1} | 5 |
  22. | NULL | NULL |
  23. | {1, NULL, 3, NULL, 5} | 1 |
  24. +-------------------------------------------------+-------------------------------------+
  25. 9 rows in set (0.01 sec)

keywords

STRUCT, ELEMENT, STRUCT_ELEMENT