STRUCT_ELEMENT
更新时间:2025-10-16
描述
返回 struct 数据列内的某一 field
语法
SQL
1STRUCT_ELEMENT( <struct>, `<filed_location>/<filed_name>`)
参数
| 参数 | 说明 |
|---|---|
<struct> |
输入的 struct 列,如果是 null,则返回 null |
<filed_location> |
field 的位置,起始位置从 1 开始,仅支持常量 |
<filed_name> |
field 的名字,仅支持常量 |
返回值
返回指定的 field 列,类型为任意类型
举例
SQL
1select struct_element(named_struct('f1', 1, 'f2', 'a'), 'f2'),struct_element(named_struct('f1', 1, 'f2', 'a'), 1);
Text
1+--------------------------------------------------------+-----------------------------------------------------+
2| struct_element(named_struct('f1', 1, 'f2', 'a'), 'f2') | struct_element(named_struct('f1', 1, 'f2', 'a'), 1) |
3+--------------------------------------------------------+-----------------------------------------------------+
4| a | 1 |
5+--------------------------------------------------------+-----------------------------------------------------+
SQL
1select struct_col, struct_element(struct_col, 'f1') from test_struct;
Text
1+-------------------------------------------------+-------------------------------------+
2| struct_col | struct_element(`struct_col `, 'f1') |
3+-------------------------------------------------+-------------------------------------+
4| {1, 2, 3, 4, 5} | 1 |
5| {1, 1000, 10000000, 100000000000, 100000000000} | 1 |
6| {5, 4, 3, 2, 1} | 5 |
7| NULL | NULL |
8| {1, NULL, 3, NULL, 5} | 1 |
9+-------------------------------------------------+-------------------------------------+
