RETENTION
Description
The RETENTION() aggregate function accepts 1-32 event conditions of type UInt8 and returns an array indicating which conditions were met.
The first array element indicates whether event_1 was met. Each subsequent element indicates whether both event_1 and the corresponding event condition were met. For example, the second element represents event_1 and event_2, and the third represents event_1 and event_3. If event_1 was not met, all array elements are zero.
Syntax
RETENTION(<event_1> [, <event_2>, ... , <event_n>]);
Parameters
| Parameter | Description |
|---|---|
<event_n> | The nth event condition, of type UInt8, with a value of 1 or 0. |
Returned value
Returns an array of 1 and 0 values with one element per input condition, up to 32 elements:
1: For the first element,event_1was met. For each subsequent element, bothevent_1and the corresponding event condition were met.0: The requirement for that element was not met.
If no rows participate in the aggregation, the function returns NULL.
When multiple columns are involved in the calculation, a row containing NULL in any of those columns is excluded from the aggregation.
Use IFNULL() on an input column to handle NULL values, as shown in the examples.
Examples
- Create a sample table and insert data.
CREATE TABLE retention_test(
`uid` int COMMENT 'user id',
`date` datetime COMMENT 'date time'
) DUPLICATE KEY(uid)
DISTRIBUTED BY HASH(uid) BUCKETS AUTO
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
INSERT into retention_test values
(0, '2022-10-12'),
(0, '2022-10-13'),
(0, '2022-10-14'),
(1, '2022-10-12'),
(1, '2022-10-13'),
(2, '2022-10-12');
- Calculate user retention.
SELECT
uid,
RETENTION(date = '2022-10-12') AS r,
RETENTION(date = '2022-10-12', date = '2022-10-13') AS r2,
RETENTION(date = '2022-10-12', date = '2022-10-13', date = '2022-10-14') AS r3
FROM retention_test
GROUP BY uid
ORDER BY uid ASC;
+------+------+--------+-----------+
| uid | r | r2 | r3 |
+------+------+--------+-----------+
| 0 | [1] | [1, 1] | [1, 1, 1] |
| 1 | [1] | [1, 1] | [1, 1, 0] |
| 2 | [1] | [1, 0] | [1, 0, 0] |
+------+------+--------+-----------+
- Create a second table and insert data to demonstrate
NULLhandling.
CREATE TABLE retention_test2(
`uid` int,
`flag` boolean,
`flag2` boolean
) DUPLICATE KEY(uid)
DISTRIBUTED BY HASH(uid) BUCKETS AUTO
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
INSERT into retention_test2 values (0, true, NULL), (1, false, false);
SELECT * FROM retention_test2 ORDER BY uid;
+------+------+-------+
| uid | flag | flag2 |
+------+------+-------+
| 0 | 1 | NULL |
| 1 | 0 | 0 |
+------+------+-------+
- Filter out all rows to demonstrate aggregation with no input rows.
SELECT RETENTION(flag) AS r FROM retention_test2 WHERE uid IS NULL;
+------+
| r |
+------+
| NULL |
+------+
- Aggregate only
flag. Becauseflagistruewhenuid = 0, the result is[1].
select retention(flag) from retention_test2;
+-----------------+
| retention(flag) |
+-----------------+
| [1] |
+-----------------+
- Aggregate
flagandflag2. The row withuid = 0is excluded becauseflag2isNULL. Only the row withuid = 1participates, resulting in[0, 0].
select retention(flag,flag2) from retention_test2;
+-----------------------+
| retention(flag,flag2) |
+-----------------------+
| [0, 0] |
+-----------------------+
- Use
IFNULL()to replaceNULLwithfalseso both rows participate in the aggregation.
select retention(flag,IFNULL(flag2,false)) from retention_test2;
+-------------------------------------+
| retention(flag,IFNULL(flag2,false)) |
+-------------------------------------+
| [1, 0] |
+-------------------------------------+