Skip to main content

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​

ParameterDescription
<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_1 was met. For each subsequent element, both event_1 and 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​

  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 NULL handling.
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. 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 |
+------+
  1. Aggregate only flag. Because flag is true when uid = 0, the result is [1].
select retention(flag) from retention_test2;
+-----------------+
| retention(flag) |
+-----------------+
| [1] |
+-----------------+
  1. Aggregate flag and flag2. The row with uid = 0 is excluded because flag2 is NULL. Only the row with uid = 1 participates, resulting in [0, 0].
select retention(flag,flag2) from retention_test2;
+-----------------------+
| retention(flag,flag2) |
+-----------------------+
| [0, 0] |
+-----------------------+
  1. Use IFNULL() to replace NULL with false so both rows participate in the aggregation.
select retention(flag,IFNULL(flag2,false)) from retention_test2;
+-------------------------------------+
| retention(flag,IFNULL(flag2,false)) |
+-------------------------------------+
| [1, 0] |
+-------------------------------------+

Enter to send · Shift+Enter for a new line