DEV Community

Michael
Michael

Posted on

Using first_value in GBase 8a: Reading the First Value of a Window

first_value in the China-domestically developed GBase 8a MPP database from GBASE returns the first value of each window, similar to the row where row_number() equals 1. This post covers the syntax and a set of runnable examples.

Syntax

First_value(XXX) OVER([PARTITION BY col_name1,col_name2,…] ORDER BY col_name1 [asc/desc], col_name2 [asc/desc],…)
Enter fullscreen mode Exit fullscreen mode

Notes

  • Each window (or all rows as one window when PARTITION BY is omitted) is sorted on the given columns, then the first row up to the current row is returned. Without ORDER BY, the default order is used.
  • The argument can be a column or an expression.
  • Watch how NULL sorts. If you are not sure, pre-process the column first, for example with the nvl function.

Examples

Sample data

gbase> create table td(id int, type int,val decimal(20,3));
Query OK, 0 rows affected (Elapsed: 00:00:00.17)

gbase> insert into td values(1,1,111),(1,1,222),(2,1,333),(2,2,444),(3,2,555),(3,2,666),(4,2,777);
Query OK, 7 rows affected (Elapsed: 00:00:00.12)
Records: 7  Duplicates: 0  Warnings: 0

gbase> insert into td values(5,1,null),(50,2,null);
Query OK, 2 rows affected (Elapsed: 00:00:00.09)
Records: 2  Duplicates: 0  Warnings: 0

gbase> select * from td;
+------+------+---------+
| id   | type | val     |
+------+------+---------+
|    1 |    1 | 111.000 |
|    1 |    1 | 222.000 |
|    2 |    1 | 333.000 |
|    2 |    2 | 444.000 |
|    3 |    2 | 555.000 |
|    3 |    2 | 666.000 |
|    4 |    2 | 777.000 |
|    5 |    1 |    NULL |
|   50 |    2 |    NULL |
+------+------+---------+
9 rows in set (Elapsed: 00:00:00.03)
Enter fullscreen mode Exit fullscreen mode

Full window usage

gbase> select *,first_value(val)over(partition by type order by id) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    1 |    1 | 111.000 |   111.000 |
|    1 |    1 | 222.000 |   111.000 |
|    2 |    1 | 333.000 |   111.000 |
|    5 |    1 |    NULL |   111.000 |
|    2 |    2 | 444.000 |   444.000 |
|    3 |    2 | 555.000 |   444.000 |
|    3 |    2 | 666.000 |   444.000 |
|    4 |    2 | 777.000 |   444.000 |
|   50 |    2 |    NULL |   444.000 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.34)

gbase> select *,first_value(val)over(partition by type order by id desc) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    5 |    1 |    NULL |      NULL |
|    2 |    1 | 333.000 |      NULL |
|    1 |    1 | 111.000 |      NULL |
|    1 |    1 | 222.000 |      NULL |
|   50 |    2 |    NULL |      NULL |
|    4 |    2 | 777.000 |      NULL |
|    3 |    2 | 555.000 |      NULL |
|    3 |    2 | 666.000 |      NULL |
|    2 |    2 | 444.000 |      NULL |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.14)
Enter fullscreen mode Exit fullscreen mode

Without PARTITION BY

All rows are sorted, then the first value is taken.

gbase> select *,first_value(val)over(order by id) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    1 |    1 | 111.000 |   111.000 |
|    1 |    1 | 222.000 |   111.000 |
|    2 |    1 | 333.000 |   111.000 |
|    2 |    2 | 444.000 |   111.000 |
|    3 |    2 | 555.000 |   111.000 |
|    3 |    2 | 666.000 |   111.000 |
|    4 |    2 | 777.000 |   111.000 |
|    5 |    1 |    NULL |   111.000 |
|   50 |    2 |    NULL |   111.000 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.10)

gbase> select *,first_value(val)over(order by id desc) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|   50 |    2 |    NULL |      NULL |
|    5 |    1 |    NULL |      NULL |
|    4 |    2 | 777.000 |      NULL |
|    3 |    2 | 555.000 |      NULL |
|    3 |    2 | 666.000 |      NULL |
|    2 |    1 | 333.000 |      NULL |
|    2 |    2 | 444.000 |      NULL |
|    1 |    1 | 111.000 |      NULL |
|    1 |    1 | 222.000 |      NULL |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.05)
Enter fullscreen mode Exit fullscreen mode

PARTITION BY without ORDER BY

Each window returns the first value in natural order. Because the database runs in parallel, the result is not guaranteed to be reproducible.

gbase> select *,first_value(val)over(partition by type) maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    1 |    1 | 222.000 |   222.000 |
|    2 |    1 | 333.000 |   222.000 |
|    5 |    1 |    NULL |   222.000 |
|    1 |    1 | 111.000 |   222.000 |
|    3 |    2 | 666.000 |   666.000 |
|    4 |    2 | 777.000 |   666.000 |
|    3 |    2 | 555.000 |   666.000 |
|    2 |    2 | 444.000 |   666.000 |
|   50 |    2 |    NULL |   666.000 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.09)
Enter fullscreen mode Exit fullscreen mode

Neither PARTITION BY nor ORDER BY

All rows form one window and the first value in natural order is taken; this form has little practical use.

gbase> select *,first_value(val)over() maxOfType from td;
+------+------+---------+-----------+
| id   | type | val     | maxOfType |
+------+------+---------+-----------+
|    1 |    1 | 111.000 |   111.000 |
|    1 |    1 | 222.000 |   111.000 |
|    2 |    1 | 333.000 |   111.000 |
|    2 |    2 | 444.000 |   111.000 |
|    3 |    2 | 555.000 |   111.000 |
|    3 |    2 | 666.000 |   111.000 |
|    4 |    2 | 777.000 |   111.000 |
|    5 |    1 |    NULL |   111.000 |
|   50 |    2 |    NULL |   111.000 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.08)
Enter fullscreen mode Exit fullscreen mode

Going forward, first_value looks simple but its answer depends entirely on the sort order and on where NULLs land. Pin an ORDER BY, decide how to handle NULLs, and test the query on a gbase cluster before it feeds a dashboard; a deterministic window in gbase is the difference between a report you can trust and one that quietly changes between runs of the same database.

Top comments (0)