RETENTION
Description
The RETENTION function accepts 1 to 32 Boolean conditions that indicate whether events occurred.
The first array element indicates whether event_1 occurred. Each subsequent element indicates whether both event_1 and the corresponding event occurred. If event_1 did not occur, all array elements are false.
Syntax
RETENTION(<event_1> [, <event_2>, ... , <event_n>]);
Parameters
| Parameter | Description |
|---|---|
<event_n> | The nth event condition, type Bool. |
Returned value
Returns a Boolean array with one element per input condition, up to 32 elements:
true: The condition is met.false: The condition is not met.
If no data participates in the aggregation, the function returns NULL.
When multiple columns are involved in a 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 below.
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 how
NULLvalues affect retention.
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 |
+------+------+-------+
- Calculate retention with no matching rows. The
WHEREclause excludes all rows, so the aggregation returnsNULL.
SELECT RETENTION(flag) AS r FROM retention_test2 WHERE uid IS NULL;
+------+
| r |
+------+
| NULL |
+------+
- Calculate retention using only
flag. Becauseflagistrueforuid = 0, the result is[1].
select retention(flag) from retention_test2;
+-----------------+
| retention(flag) |
+-----------------+
| [1] |
+-----------------+
- Calculate retention using
flagandflag2. The row withuid = 0is excluded becauseflag2isNULL. Only the row withuid = 1participates, producing[0, 0].
select retention(flag,flag2) from retention_test2;
+-----------------------+
| retention(flag,flag2) |
+-----------------------+
| [0, 0] |
+-----------------------+
- Use
IFNULLto replaceNULLwithfalseso that both rows, withuid = 0anduid = 1, participate in the aggregation.
select retention(flag,IFNULL(flag2,false)) from retention_test2;
+-------------------------------------+
| retention(flag,IFNULL(flag2,false)) |
+-------------------------------------+
| [1, 0] |
+-------------------------------------+