aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/tests/sql/suites/aggregate/group_by_session_extended.sql
blob: 1b9af1d0d82ac1dc10e7fc84c9ebf2c40b0483e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* syntax version 1 */
/* postgres can not */

$init = ($row) -> (AsStruct($row.ts ?? 0 as value, 1 as count));

$calculate = ($_row, $state) -> ($state.value);
-- split partition into two-element grooups, make session key to be cumulative sum of ts from partition start
$update = ($row, $state) -> {
  $state = AsStruct($state.count + 1 as count, $state.value as value);
  $state = AsStruct($state.count as count, $state.value + ($row.ts ?? 0) as value);
  return  AsTuple(Unwrap($state.count % 2) == 1, $state);
};

SELECT
    user,
    SessionStart() as session_start,
    ListSort(AGGREGATE_LIST(ts ?? 100500)) as session,
    COUNT(1) as session_len
FROM plato.Input
GROUP BY SessionWindow(ts, $init, $update, $calculate), user
ORDER BY user, session_start;