Skip to main content

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​

ParameterDescription
<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​

  1. 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');
  1. 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] |
+------+------+--------+-----------+
  1. Create a second table and insert data to demonstrate how NULL values 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 |
+------+------+-------+
  1. Calculate retention with no matching rows. The WHERE clause excludes all rows, so the aggregation returns NULL.
SELECT RETENTION(flag) AS r FROM retention_test2 WHERE uid IS NULL;
+------+
| r |
+------+
| NULL |
+------+
  1. Calculate retention using only flag. Because flag is true for uid = 0, the result is [1].
select retention(flag) from retention_test2;
+-----------------+
| retention(flag) |
+-----------------+
| [1] |
+-----------------+
  1. Calculate retention using flag and flag2. The row with uid = 0 is excluded because flag2 is NULL. Only the row with uid = 1 participates, producing [0, 0].
select retention(flag,flag2) from retention_test2;
+-----------------------+
| retention(flag,flag2) |
+-----------------------+
| [0, 0] |
+-----------------------+
  1. Use IFNULL to replace NULL with false so that both rows, with uid = 0 and uid = 1, participate in the aggregation.
select retention(flag,IFNULL(flag2,false)) from retention_test2;
+-------------------------------------+
| retention(flag,IFNULL(flag2,false)) |
+-------------------------------------+
| [1, 0] |
+-------------------------------------+