DEV Community

Michael
Michael

Posted on

unnest in GBase 8a: The Inverse of group_concat

group_concat collapses multiple rows into a single field. unnest does the opposite: it splits one field into multiple rows. In the China-domestically developed GBase 8a MPP database from GBASE, that is handy whenever a column stores a comma-separated list. On the build tested here, unnest in the gbase client accepts exactly one argument.

Environment

Version 9.5.3.28.14.patch1. The test table tt has a single column v holding two rows:

gbase> select * from tt;
+---------+
| v       |
+---------+
| 1,2,3,4 |
| a,b,c   |
+---------+
2 rows in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

The unnest function

The input text must be comma-separated. If your data uses another delimiter, run replace first to convert it to commas. Passing a column expands every row; passing a literal expands that one list:

gbase> select unnest(v) f1 from tt;
+------+
| f1   |
+------+
| 1    |
| 2    |
| 3    |
| 4    |
| a    |
| b    |
| c    |
+------+
7 rows in set (Elapsed: 00:00:00.00)

gbase> select unnest('1,2,3,4,5,6,7,8');
+---------------------------+
| unnest('1,2,3,4,5,6,7,8') |
+---------------------------+
| 1                         |
| 2                         |
| 3                         |
| 4                         |
| 5                         |
| 6                         |
| 7                         |
| 8                         |
+---------------------------+
8 rows in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

The first query turns 2 rows into 7, the second turns one string into 8 — the inverse of what group_concat would do.

Going forward, the practical constraint is the single-argument form: normalise your delimiter before calling unnest, and remember the expansion happens per row, so a gbase cluster produces as many output rows as there are elements across all input rows. For a database that stores tags, IDs or lists in one column, that makes unnest the quickest way back to a relational shape without an external script.

Top comments (0)