Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M27-001): arrayCompact: turn chart.sankey and overview.userJourney from FAILS into ordinary queries

ralphcd2acc7f0ac3
diff --git a/packages/core/src/modules/chart/src/sankey.sql.proof.md b/packages/core/src/modules/chart/src/sankey.sql.proof.md
index b1bb06c71..4f11b8439 100644
--- a/packages/core/src/modules/chart/src/sankey.sql.proof.md
+++ b/packages/core/src/modules/chart/src/sankey.sql.proof.md
@@ -7,6 +7,25 @@ Both statements of every sankey mode were executed twice against the same data
- **Cluster note.** The `HAVING session_id IN (SELECT session_id FROM start_event_sessions)` clauses are V1's and are ported **verbatim**: no `IN` was converted to or from `GLOBAL IN`. Whether they should be `GLOBAL IN` on the 2x2 cluster is a pre-existing question this conversion neither answers nor changes (`docs/ENVIRONMENT.md`), and it is recorded as debt in the task report.
- **One case reads IDENTICAL AS SET.** The transitions statement orders by `step ASC, value DESC`, which leaves rows sharing a `(step, value)` pair in arbitrary order. Measured on 2026-09-04: **V1's own statement, executed 4 times unchanged, returned 4 different row orders and one identical row set.** Row order is therefore not part of the contract — the service groups transitions by step and source before using them — so that case is compared as a set, exactly as M7-003 compared the chart statements.
+- **Amended by M27-001 (2026-09-15): the dedup expression is no longer V1's.** V1's
+ `arrayFilter((x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw))`
+ is Theta(n^2) in the longest single session's event count and could not answer at a 7-day window on
+ a busy project (`docs/ANALYTICS_PERFORMANCE.md` section 6.1). Every `-- V2` statement below now reads
+ `arrayCompact(groupArray(event_name))`; the `-- V1` statements are left exactly as V1 wrote them, so the
+ two columns differ in that one expression by design. Re-proved against the local prod copy on
+ **2026-09-15**, `use_query_condition_cache=0`, `max_memory_usage=6 GiB`, per-query `memory_usage` and
+ `elapsed_ns` read off `X-ClickHouse-Summary`:
+ - **Result sets:** `cityHash64(arraySort(groupArray(tuple(*))))` compared old vs new on every
+ (project, window) pair where the old spelling still survives — `earlysalary-production` 30 d and
+ `chatpaper` 30 d, both statements: **identical, 4/4**. One of the four (`earlysalary-production`'s
+ transitions statement) also differs in row ORDER between the two spellings — and equally between two
+ runs of either spelling, because of the `(step, value)` ties the bullet above already records. It is
+ compared as a set for that reason.
+ - **Cost, both statements, `mode: 'after'`, 3 steps, 30-day window:** `verdict` FAIL -> 2,931 ms /
+ 660 MiB · `bayse` FAIL -> 4,606 ms / 1,327 MiB · `earlysalary-production` 10,129 ms -> 5,927 ms /
+ 1,504 MiB · `website-8103` FAIL (`would use 2.03 PiB`) -> 2,607 ms / 1,035 MiB · `chatpaper`
+ 13,453 ms -> 12,639 ms / 2,132 MiB. `website-8103` at 1 day: FAIL (`768.88 GiB`) -> 147 ms / 20 MiB.
+
- **Verdict**: 23 cases, 0 not identical.
### sankey topEntries — after, screen_view, 3 steps (golden insights-user-flow-after)
@@ -17,7 +36,7 @@ Both statements of every sankey mode were executed twice against the same data
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'screen_view', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"screen_view","p9":3,"p10":2,"p11":3}
```
@@ -29,7 +48,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'screen_view', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('screen_view')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p11:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p11:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"screen_view","p9":3,"p10":2,"p11":["screen_view"]}
```
@@ -41,7 +60,7 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_end' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = 'session_end', events_deduped) - 3 + 1), arrayFirstIndex(x -> x = 'session_end', events_deduped) - greatest(1, arrayFirstIndex(x -> x = 'session_end', events_deduped) - 3 + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = {p8:String}, events_deduped) - {p9:UInt64} + 1), arrayFirstIndex(x -> x = {p10:String}, events_deduped) - greatest(1, arrayFirstIndex(x -> x = {p11:String}, events_deduped) - {p12:UInt64} + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p14:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = {p8:String}, events_deduped) - {p9:UInt64} + 1), arrayFirstIndex(x -> x = {p10:String}, events_deduped) - greatest(1, arrayFirstIndex(x -> x = {p11:String}, events_deduped) - {p12:UInt64} + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p14:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"session_end","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"session_end","p9":3,"p10":"session_end","p11":"session_end","p12":3,"p13":2,"p14":3}
```
@@ -53,7 +72,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_end' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = 'session_end', events_deduped) - 3 + 1), arrayFirstIndex(x -> x = 'session_end', events_deduped) - greatest(1, arrayFirstIndex(x -> x = 'session_end', events_deduped) - 3 + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('session_start', 'link_out', 'screen_view')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = {p8:String}, events_deduped) - {p9:UInt64} + 1), arrayFirstIndex(x -> x = {p10:String}, events_deduped) - greatest(1, arrayFirstIndex(x -> x = {p11:String}, events_deduped) - {p12:UInt64} + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p14:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = {p8:String}, events_deduped) - {p9:UInt64} + 1), arrayFirstIndex(x -> x = {p10:String}, events_deduped) - greatest(1, arrayFirstIndex(x -> x = {p11:String}, events_deduped) - {p12:UInt64} + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p14:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"session_end","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"session_end","p9":3,"p10":"session_end","p11":"session_end","p12":3,"p13":2,"p14":["session_start","link_out","screen_view"]}
```
@@ -65,7 +84,7 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_start' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_end' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, 4) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT session_id, events, arrayFirstIndex(x -> x = 'session_start', events) as start_index, arrayFirstIndex(x -> x = 'session_end', events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index) SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events, events[start_index] as entry_event FROM between_sessions) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p5:String} AND name = {p6:String} AND created_at BETWEEN toDateTime({p7:String}) AND toDateTime({p8:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p9:String} AND created_at BETWEEN toDateTime({p10:String}) AND toDateTime({p11:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p12:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT session_id, events, arrayFirstIndex(x -> x = {p14:String}, events) as start_index, arrayFirstIndex(x -> x = {p15:String}, events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index) SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events, events[start_index] as entry_event FROM between_sessions) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p16:UInt64}
+WITH session_paths AS (WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p5:String} AND name = {p6:String} AND created_at BETWEEN toDateTime({p7:String}) AND toDateTime({p8:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p9:String} AND created_at BETWEEN toDateTime({p10:String}) AND toDateTime({p11:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p12:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT session_id, events, arrayFirstIndex(x -> x = {p14:String}, events) as start_index, arrayFirstIndex(x -> x = {p15:String}, events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index) SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events, events[start_index] as entry_event FROM between_sessions) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p16:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"session_start","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"session_end","p7":"2026-07-06 00:00:00","p8":"2026-07-12 23:59:59","p9":"secure-privacy","p10":"2026-07-06 00:00:00","p11":"2026-07-12 23:59:59","p12":4,"p13":2,"p14":"session_start","p15":"session_end","p16":3}
```
@@ -77,7 +96,7 @@ WITH session_paths AS (WITH between_sessions AS (WITH session_paths AS (WITH sta
-- V1
WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_start' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_end' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, 4) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT session_id, events, arrayFirstIndex(x -> x = 'session_start', events) as start_index, arrayFirstIndex(x -> x = 'session_end', events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index), session_paths AS (SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events FROM between_sessions HAVING events[1] IN ('session_start')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p5:String} AND name = {p6:String} AND created_at BETWEEN toDateTime({p7:String}) AND toDateTime({p8:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p9:String} AND created_at BETWEEN toDateTime({p10:String}) AND toDateTime({p11:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p12:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT session_id, events, arrayFirstIndex(x -> x = {p14:String}, events) as start_index, arrayFirstIndex(x -> x = {p15:String}, events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index), session_paths AS (SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events FROM between_sessions HAVING events[1] IN {p16:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p5:String} AND name = {p6:String} AND created_at BETWEEN toDateTime({p7:String}) AND toDateTime({p8:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p9:String} AND created_at BETWEEN toDateTime({p10:String}) AND toDateTime({p11:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p12:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT session_id, events, arrayFirstIndex(x -> x = {p14:String}, events) as start_index, arrayFirstIndex(x -> x = {p15:String}, events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index), session_paths AS (SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events FROM between_sessions HAVING events[1] IN {p16:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"session_start","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"session_end","p7":"2026-07-06 00:00:00","p8":"2026-07-12 23:59:59","p9":"secure-privacy","p10":"2026-07-06 00:00:00","p11":"2026-07-12 23:59:59","p12":4,"p13":2,"p14":"session_start","p15":"session_end","p16":["session_start"]}
```
@@ -89,7 +108,7 @@ WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (S
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND name NOT IN ('session_end') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'screen_view', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) AND name NOT IN {p8:Array(String)} ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p9:String}, events_deduped), {p10:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p11:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p12:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) AND name NOT IN {p8:Array(String)} ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p9:String}, events_deduped), {p10:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p11:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p12:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":["session_end"],"p9":"screen_view","p10":3,"p11":2,"p12":3}
```
@@ -101,7 +120,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND name NOT IN ('session_end') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'screen_view', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('screen_view')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) AND name NOT IN {p8:Array(String)} ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p9:String}, events_deduped), {p10:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p11:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p12:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) AND name NOT IN {p8:Array(String)} ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p9:String}, events_deduped), {p10:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p11:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p12:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":["session_end"],"p9":"screen_view","p10":3,"p11":2,"p12":["screen_view"]}
```
@@ -113,7 +132,7 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND name IN ('session_start', 'session_end', 'screen_view') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'screen_view', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) AND name IN {p8:Array(String)} ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p9:String}, events_deduped), {p10:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p11:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p12:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) AND name IN {p8:Array(String)} ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p9:String}, events_deduped), {p10:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p11:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p12:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":["session_start","session_end","screen_view"],"p9":"screen_view","p10":3,"p11":2,"p12":3}
```
@@ -125,7 +144,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND name IN ('session_start', 'session_end', 'screen_view') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'screen_view', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('screen_view')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) AND name IN {p8:Array(String)} ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p9:String}, events_deduped), {p10:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p11:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p12:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) AND name IN {p8:Array(String)} ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p9:String}, events_deduped), {p10:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p11:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p12:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":["session_start","session_end","screen_view"],"p9":"screen_view","p10":3,"p11":2,"p12":["screen_view"]}
```
@@ -137,7 +156,7 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND country = 'US' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'screen_view', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) AND country = 'US' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) AND country = 'US' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"screen_view","p9":3,"p10":2,"p11":3}
```
@@ -149,7 +168,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND country = 'US' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'screen_view', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('screen_view')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) AND country = 'US' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p11:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) AND country = 'US' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p11:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"screen_view","p9":3,"p10":2,"p11":["screen_view"]}
```
@@ -161,7 +180,7 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_start' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'session_start', events_deduped), 10) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"session_start","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"session_start","p9":10,"p10":2,"p11":3}
```
@@ -173,7 +192,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_start' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'session_start', events_deduped), 10) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('session_start')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p11:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p11:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"session_start","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"session_start","p9":10,"p10":2,"p11":["session_start"]}
```
@@ -185,7 +204,7 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_end' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = 'session_end', events_deduped) - 2 + 1), arrayFirstIndex(x -> x = 'session_end', events_deduped) - greatest(1, arrayFirstIndex(x -> x = 'session_end', events_deduped) - 2 + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = {p8:String}, events_deduped) - {p9:UInt64} + 1), arrayFirstIndex(x -> x = {p10:String}, events_deduped) - greatest(1, arrayFirstIndex(x -> x = {p11:String}, events_deduped) - {p12:UInt64} + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p14:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = {p8:String}, events_deduped) - {p9:UInt64} + 1), arrayFirstIndex(x -> x = {p10:String}, events_deduped) - greatest(1, arrayFirstIndex(x -> x = {p11:String}, events_deduped) - {p12:UInt64} + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p14:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"session_end","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"session_end","p9":2,"p10":"session_end","p11":"session_end","p12":2,"p13":2,"p14":3}
```
@@ -197,7 +216,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_end' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = 'session_end', events_deduped) - 2 + 1), arrayFirstIndex(x -> x = 'session_end', events_deduped) - greatest(1, arrayFirstIndex(x -> x = 'session_end', events_deduped) - 2 + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('screen_view', 'link_out', 'web_vitals_lcp')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = {p8:String}, events_deduped) - {p9:UInt64} + 1), arrayFirstIndex(x -> x = {p10:String}, events_deduped) - greatest(1, arrayFirstIndex(x -> x = {p11:String}, events_deduped) - {p12:UInt64} + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p14:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice( events_deduped, greatest(1, arrayFirstIndex(x -> x = {p8:String}, events_deduped) - {p9:UInt64} + 1), arrayFirstIndex(x -> x = {p10:String}, events_deduped) - greatest(1, arrayFirstIndex(x -> x = {p11:String}, events_deduped) - {p12:UInt64} + 1) + 1 ) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, events_sliced as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p14:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"session_end","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"session_end","p9":2,"p10":"session_end","p11":"session_end","p12":2,"p13":2,"p14":["screen_view","link_out","web_vitals_lcp"]}
```
@@ -209,7 +228,7 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_start' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND country = 'US' GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_end' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND os = 'iOS' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, 4) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT session_id, events, arrayFirstIndex(x -> x = 'session_start', events) as start_index, arrayFirstIndex(x -> x = 'session_end', events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index) SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events, events[start_index] as entry_event FROM between_sessions) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) AND country = 'US' GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p5:String} AND name = {p6:String} AND created_at BETWEEN toDateTime({p7:String}) AND toDateTime({p8:String}) AND os = 'iOS' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p9:String} AND created_at BETWEEN toDateTime({p10:String}) AND toDateTime({p11:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p12:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT session_id, events, arrayFirstIndex(x -> x = {p14:String}, events) as start_index, arrayFirstIndex(x -> x = {p15:String}, events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index) SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events, events[start_index] as entry_event FROM between_sessions) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p16:UInt64}
+WITH session_paths AS (WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) AND country = 'US' GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p5:String} AND name = {p6:String} AND created_at BETWEEN toDateTime({p7:String}) AND toDateTime({p8:String}) AND os = 'iOS' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p9:String} AND created_at BETWEEN toDateTime({p10:String}) AND toDateTime({p11:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p12:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT session_id, events, arrayFirstIndex(x -> x = {p14:String}, events) as start_index, arrayFirstIndex(x -> x = {p15:String}, events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index) SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events, events[start_index] as entry_event FROM between_sessions) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p16:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"session_start","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"session_end","p7":"2026-07-06 00:00:00","p8":"2026-07-12 23:59:59","p9":"secure-privacy","p10":"2026-07-06 00:00:00","p11":"2026-07-12 23:59:59","p12":4,"p13":2,"p14":"session_start","p15":"session_end","p16":3}
```
@@ -221,7 +240,7 @@ WITH session_paths AS (WITH between_sessions AS (WITH session_paths AS (WITH sta
-- V1
WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_start' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND country = 'US' GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'session_end' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') AND os = 'iOS' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, 4) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT session_id, events, arrayFirstIndex(x -> x = 'session_start', events) as start_index, arrayFirstIndex(x -> x = 'session_end', events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index), session_paths AS (SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events FROM between_sessions HAVING events[1] IN ('session_start')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) AND country = 'US' GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p5:String} AND name = {p6:String} AND created_at BETWEEN toDateTime({p7:String}) AND toDateTime({p8:String}) AND os = 'iOS' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p9:String} AND created_at BETWEEN toDateTime({p10:String}) AND toDateTime({p11:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p12:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT session_id, events, arrayFirstIndex(x -> x = {p14:String}, events) as start_index, arrayFirstIndex(x -> x = {p15:String}, events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index), session_paths AS (SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events FROM between_sessions HAVING events[1] IN {p16:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) AND country = 'US' GROUP BY session_id), end_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p5:String} AND name = {p6:String} AND created_at BETWEEN toDateTime({p7:String}) AND toDateTime({p8:String}) AND os = 'iOS' GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p9:String} AND created_at BETWEEN toDateTime({p10:String}) AND toDateTime({p11:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p12:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions) AND session_id IN (SELECT session_id FROM end_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p13:UInt64}) SELECT session_id, events, arrayFirstIndex(x -> x = {p14:String}, events) as start_index, arrayFirstIndex(x -> x = {p15:String}, events) as end_index FROM session_paths HAVING start_index > 0 AND end_index > 0 AND start_index < end_index), session_paths AS (SELECT session_id, arraySlice(events, start_index, end_index - start_index + 1) as events FROM between_sessions HAVING events[1] IN {p16:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"session_start","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"session_end","p7":"2026-07-06 00:00:00","p8":"2026-07-12 23:59:59","p9":"secure-privacy","p10":"2026-07-06 00:00:00","p11":"2026-07-12 23:59:59","p12":4,"p13":2,"p14":"session_start","p15":"session_end","p16":["session_start"]}
```
@@ -233,7 +252,7 @@ WITH between_sessions AS (WITH session_paths AS (WITH start_event_sessions AS (S
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, 3) as events_sliced FROM events_deduped_cte HAVING 1 = 1) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p8:UInt64}) as events_sliced FROM events_deduped_cte HAVING 1 = 1) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p9:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p10:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p8:UInt64}) as events_sliced FROM events_deduped_cte HAVING 1 = 1) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p9:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p10:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":3,"p9":2,"p10":3}
```
@@ -245,7 +264,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'screen_view' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, 3) as events_sliced FROM events_deduped_cte HAVING 1 = 1) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('session_start', 'screen_view')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p8:UInt64}) as events_sliced FROM events_deduped_cte HAVING 1 = 1) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p9:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p10:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, 1, {p8:UInt64}) as events_sliced FROM events_deduped_cte HAVING 1 = 1) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p9:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p10:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":3,"p9":2,"p10":["session_start","screen_view"]}
```
@@ -257,7 +276,7 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'bayse' AND name = 'session_start' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-08 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'bayse' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-08 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'session_start', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
-- V2 params: {"p1":"bayse","p2":"session_start","p3":"2026-07-06 00:00:00","p4":"2026-07-08 23:59:59","p5":"bayse","p6":"2026-07-06 00:00:00","p7":"2026-07-08 23:59:59","p8":"session_start","p9":3,"p10":2,"p11":3}
```
@@ -269,7 +288,7 @@ WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM even
-- V1
WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'bayse' AND name = 'session_start' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-08 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'bayse' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-08 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'session_start', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN ('session_start')) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2
-WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p11:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
+WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}), session_paths AS (SELECT session_id, events FROM session_paths_base HAVING events[1] IN {p11:Array(String)}) SELECT pair.1 as source, pair.2 as target, pair.3 as step, count() as value FROM ((SELECT arrayJoin(arrayMap(i -> (events[i], events[i + 1], i), range(1, length(events)))) as pair FROM session_paths WHERE length(events) >= 2)) GROUP BY source, target, step ORDER BY step ASC, value DESC
-- V2 params: {"p1":"bayse","p2":"session_start","p3":"2026-07-06 00:00:00","p4":"2026-07-08 23:59:59","p5":"bayse","p6":"2026-07-06 00:00:00","p7":"2026-07-08 23:59:59","p8":"session_start","p9":3,"p10":2,"p11":["session_start"]}
```
@@ -281,6 +300,6 @@ WITH session_paths_base AS (WITH start_event_sessions AS (SELECT session_id FROM
-- V1
WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = 'secure-privacy' AND name = 'no_such_event_xyz' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = 'secure-privacy' AND created_at BETWEEN toDateTime('2026-07-06 00:00:00') AND toDateTime('2026-07-12 23:59:59') ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = 'no_such_event_xyz', events_deduped), 3) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= 2) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT 3
-- V2
-WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayFilter( (x, i) -> i = 1 OR x != events_raw[i - 1], groupArray(event_name) as events_raw, arrayEnumerate(events_raw) ) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
+WITH session_paths AS (WITH start_event_sessions AS (SELECT session_id FROM events WHERE project_id = {p1:String} AND name = {p2:String} AND created_at BETWEEN toDateTime({p3:String}) AND toDateTime({p4:String}) GROUP BY session_id), events_deduped_cte AS (WITH ordered_events AS (SELECT session_id, name as event_name, created_at FROM events WHERE project_id = {p5:String} AND created_at BETWEEN toDateTime({p6:String}) AND toDateTime({p7:String}) ORDER BY session_id ASC, created_at ASC) SELECT session_id, arrayCompact(groupArray(event_name)) as events_deduped FROM ordered_events GROUP BY session_id), events_sliced_cte AS (SELECT session_id, arraySlice(events_deduped, arrayFirstIndex(x -> x = {p8:String}, events_deduped), {p9:UInt64}) as events_sliced FROM events_deduped_cte HAVING session_id IN (SELECT session_id FROM start_event_sessions)) SELECT session_id, if( arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) = 0, events_sliced, arraySlice( events_sliced, 1, arrayFirstIndex(x -> x > 1, arrayEnumerateUniq(events_sliced)) - 1 ) ) as events, events[1] as entry_event FROM events_sliced_cte HAVING length(events) >= {p10:UInt64}) SELECT entry_event, count() as count FROM session_paths GROUP BY entry_event ORDER BY count DESC LIMIT {p11:UInt64}
-- V2 params: {"p1":"secure-privacy","p2":"no_such_event_xyz","p3":"2026-07-06 00:00:00","p4":"2026-07-12 23:59:59","p5":"secure-privacy","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":"no_such_event_xyz","p9":3,"p10":2,"p11":3}
```
diff --git a/packages/core/src/modules/chart/src/sankey.sql.ts b/packages/core/src/modules/chart/src/sankey.sql.ts
index a40b48b71..c66f71b32 100644
--- a/packages/core/src/modules/chart/src/sankey.sql.ts
+++ b/packages/core/src/modules/chart/src/sankey.sql.ts
@@ -24,12 +24,17 @@ export type SankeyMode = 'after' | 'before' | 'between';
/** How many entry events the flow starts from. */
export const TOP_ENTRY_EVENTS = 3;
-/** The dedup + transition pieces every mode shares, verbatim from V1. */
-const DEDUPE_CONSECUTIVE = compiledText(`arrayFilter(
- (x, i) -> i = 1 OR x != events_raw[i - 1],
- groupArray(event_name) as events_raw,
- arrayEnumerate(events_raw)
- ) as events_deduped`);
+/**
+ * Consecutive-repeat dedup, shared by every mode. V1 spelled it as an
+ * `arrayFilter` whose lambda indexed its own captured `groupArray`, which
+ * ClickHouse re-materialises once per element: Theta(n^2) in the longest
+ * single session's event count, fatal on a tenant with one very long session
+ * (docs/ANALYTICS_PERFORMANCE.md section 6.1). `arrayCompact` is the built-in
+ * for exactly this and is linear; the result is byte-identical.
+ */
+const DEDUPE_CONSECUTIVE = compiledText(
+ 'arrayCompact(groupArray(event_name)) as events_deduped'
+);
/**
* 'after' mode stops the path at the first repeated event, so a loop back to
diff --git a/packages/core/src/modules/overview/src/overview.sql.proof.md b/packages/core/src/modules/overview/src/overview.sql.proof.md
index 64d7a36c5..67e071c41 100644
--- a/packages/core/src/modules/overview/src/overview.sql.proof.md
+++ b/packages/core/src/modules/overview/src/overview.sql.proof.md
@@ -14,6 +14,28 @@
execution (no query is committed unexecuted). Row counts and timings below are from that
run.
+- **Amended by M27-001 (2026-09-15): the user-journey dedup expression is no longer V1's.** V1's
+ `arrayFilter((x, i) -> i = 1 OR x != paths_raw[i - 1], groupArray(path) as paths_raw, arrayEnumerate(paths_raw))`
+ is Theta(n^2) in the longest single session's pageview count, so `overview.userJourney` — which the
+ dashboard calls on every load — could not answer at all for a tenant with one very long session
+ (`docs/ANALYTICS_PERFORMANCE.md` section 6.1). It now reads `arrayCompact(groupArray(path))`, which is the
+ ClickHouse built-in for the same operation and is linear. The render below is the amended text.
+ Re-proved against the local prod copy on **2026-09-15**, `use_query_condition_cache=0`,
+ `max_memory_usage=6 GiB`, `memory_usage` and `elapsed_ns` read off `X-ClickHouse-Summary`:
+ - **Result sets:** `cityHash64(arraySort(groupArray(tuple(*))))` old vs new on every (project, window)
+ pair where the old spelling survives — `verdict`/`bayse`/`earlysalary-production`/`chatpaper` at 30 d
+ and `bayse` at 7 d, both statements: **identical, 10/10**.
+ - **Cost, both statements, 5 steps, 30-day window:** `verdict` 100 ms -> 71 ms · `bayse` 1,583 ms ->
+ 980 ms / 182 MiB · `earlysalary-production` 1,341 ms -> 1,283 ms · `website-8103` FAIL
+ (`would use 917.36 TiB`) -> 2,304 ms / 909 MiB · `chatpaper` 7,125 ms -> 6,434 ms / 1,392 MiB.
+ `website-8103` at 1 day: FAIL (`852.36 GiB`) -> 119 ms / 27 MiB.
+ - **Row order is not part of the contract for `transitionsQuery`**, exactly as `sankey.sql.proof.md`
+ already records: it ends `ORDER BY step ASC, value DESC` and most rows share a `(step, value)` pair,
+ so ClickHouse returns them in whatever order the parallel merge produced. Measured 2026-09-15 on
+ `bayse` 30 d: 5,192 of 5,301 result rows sit in a tied group, and **the unchanged shipped statement,
+ run 10 times, returned 10 different row orders and 1 identical row set**. Set-level hashes are the
+ comparison above for that reason.
+
- **Date**: 2026-09-04. **Data**: local prod-copy `openpanel` (319,499,000 events, static).
Project: `skills-directory` (293,597 events, 75,727 sessions, 2026-07-01..2026-08-25).
Window used throughout: `2026-08-01 00:00:00`..`2026-08-08 00:00:00`, UTC.
@@ -148,7 +170,8 @@ WITH FILL FROM toStartOfDay({p4:DateTime}) TO {p5:DateTime} STEP toIntervalDay(1
```
```sql
--- topEntriesQuery (user-journey step 1) — same array-function shape sankey.sql.ts established (M7-004)
+-- topEntriesQuery (user-journey step 1) — same array-function shape sankey.sql.ts established (M7-004),
+-- with M27-001's arrayCompact amendment (see the note above the render)
WITH session_paths AS (
WITH paths_deduped_cte AS (
WITH ordered_events AS (
@@ -159,7 +182,7 @@ WITH session_paths AS (
ORDER BY session_id ASC, created_at ASC
)
SELECT session_id,
- arraySlice(arrayFilter((x, i) -> i = 1 OR x != paths_raw[i - 1], groupArray(path) as paths_raw, arrayEnumerate(paths_raw)), 1, {p4:UInt64}) as paths_deduped
+ arraySlice(arrayCompact(groupArray(path)), 1, {p4:UInt64}) as paths_deduped
FROM ordered_events GROUP BY session_id
)
SELECT session_id,
diff --git a/packages/core/src/modules/overview/src/overview.sql.ts b/packages/core/src/modules/overview/src/overview.sql.ts
index f4f909c0b..eea3ae369 100644
--- a/packages/core/src/modules/overview/src/overview.sql.ts
+++ b/packages/core/src/modules/overview/src/overview.sql.ts
@@ -543,6 +543,12 @@ function orderedEventsQuery(input: OrderedEventsQueryInput): SqlFragment {
`;
}
+/**
+ * `arrayCompact` drops consecutive repeats in linear time. V1's `arrayFilter`
+ * spelling was Theta(n^2) in the longest session's pageview count and could not
+ * answer for a tenant with one very long session — the same defect, and the same
+ * fix, as sankey.sql.ts's DEDUPE_CONSECUTIVE.
+ */
function pathsDedupedCte(
input: OrderedEventsQueryInput,
steps: number
@@ -552,11 +558,7 @@ function pathsDedupedCte(
SELECT
session_id,
arraySlice(
- arrayFilter(
- (x, i) -> i = 1 OR x != paths_raw[i - 1],
- groupArray(path) as paths_raw,
- arrayEnumerate(paths_raw)
- ),
+ arrayCompact(groupArray(path)),
1, ${sql.uint64(steps)}
) as paths_deduped
FROM ordered_events