SQL 参考

特殊函数

师成师成· 更新于 2026-09-28· 阅读 5 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

特殊函数

展开函数

unnest

将数组或映射展开为多行。

参数

  • array:要展开的数组表达式。可以是常量、列或函数,以及数组运算符的任意组合。

示例

> select unnest(make_array(1, 2, 3, 4, 5)) as unnested;
+----------+
| unnested |
+----------+
| 1        |
| 2        |
| 3        |
| 4        |
| 5        |
+----------+
> select unnest(range(0, 10)) as unnested_range;
+----------------+
| unnested_range |
+----------------+
| 0              |
| 1              |
| 2              |
| 3              |
| 4              |
| 5              |
| 6              |
| 7              |
| 8              |
| 9              |
+----------------+

unnest (struct)

将结构体的字段展开为独立的列。结构体的每个字段可以通过 "<table>.<struct>.<field>" 的方式访问。

参数

  • struct:要展开的对象表达式。可以是常量、列或函数,也可以是对象运算符的任意组合。

示例

> create table foo as values ({a: 5, b: 'a string'}), ({a:6, b: 'another string'});

> create view foov as select column1 as struct_column from foo;

> select * from foov;
+---------------------------+
| struct_column             |
+---------------------------+
| {a: 5, b: a string}       |
| {a: 6, b: another string} |
+---------------------------+

> select unnest(struct_column) from foov;
+--------------------------------------------+--------------------------------------------+
| foov.struct_column.a                       | foov.struct_column.b                       |
+--------------------------------------------+--------------------------------------------+
| 5                                          | a string                                   |
| 6                                          | another string                             |
+--------------------------------------------+--------------------------------------------+

评论

登录后参与评论

正在加载评论…