Skip to main content

STRUCT_ELEMENT

Description​

Return a specific field within a data column of a struct.

Syntax​

STRUCT_ELEMENT( <struct>, `<filed_location>/<filed_name>`)

Parameters​

ParameterDescription
<struct>If the input struct column is null, return null
<filed_location>The position of the field starts from 1, and only constants are supported
<filed_name>The field name must be a constant

Return Value​

Return the specified field column, with the type being any type

Example​

select struct_element(named_struct('f1', 1, 'f2', 'a'), 'f2'),struct_element(named_struct('f1', 1, 'f2', 'a'), 1);
+--------------------------------------------------------+-----------------------------------------------------+
| struct_element(named_struct('f1', 1, 'f2', 'a'), 'f2') | struct_element(named_struct('f1', 1, 'f2', 'a'), 1) |
+--------------------------------------------------------+-----------------------------------------------------+
| a | 1 |
+--------------------------------------------------------+-----------------------------------------------------+
select struct_col, struct_element(struct_col, 'f1') from test_struct;
+-------------------------------------------------+-------------------------------------+
| struct_col | struct_element(`struct_col `, 'f1') |
+-------------------------------------------------+-------------------------------------+
| {1, 2, 3, 4, 5} | 1 |
| {1, 1000, 10000000, 100000000000, 100000000000} | 1 |
| {5, 4, 3, 2, 1} | 5 |
| NULL | NULL |
| {1, NULL, 3, NULL, 5} | 1 |
+-------------------------------------------------+-------------------------------------+