Skip to content

Commit

Permalink
feat(web-analytics): Default onto sessions v2 (#24615)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
robbie-c and github-actions[bot] authored Aug 27, 2024
1 parent 1fb4e7f commit b434bb8
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 129 deletions.
6 changes: 3 additions & 3 deletions posthog/hogql/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
PersonsOnEventsMode,
SessionTableVersion,
)
from posthog.utils import get_instance_region
from posthog.warehouse.models.external_data_job import ExternalDataJob
from posthog.warehouse.models.external_data_schema import ExternalDataSchema
from posthog.warehouse.models.external_data_source import ExternalDataSource
Expand Down Expand Up @@ -249,8 +248,9 @@ def create_hogql_database(
join_function=join_with_persons_table,
)

if modifiers.sessionTableVersion == SessionTableVersion.V2 or (
get_instance_region() == "EU" and modifiers.sessionTableVersion == SessionTableVersion.AUTO
if (
modifiers.sessionTableVersion == SessionTableVersion.V2
or modifiers.sessionTableVersion == SessionTableVersion.AUTO
):
raw_sessions = RawSessionsTableV2()
database.raw_sessions = raw_sessions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@
SELECT DISTINCT session_replay_events.session_id AS session_id
FROM session_replay_events
LEFT JOIN
(SELECT dateDiff('second', min(toTimeZone(sessions.min_timestamp, 'UTC')), max(toTimeZone(sessions.max_timestamp, 'UTC'))) AS duration,
sessions.session_id AS session_id
FROM sessions
WHERE equals(sessions.team_id, 2)
GROUP BY sessions.session_id,
sessions.session_id) AS raw_session_replay_events__session ON equals(session_replay_events.session_id, raw_session_replay_events__session.session_id)
(SELECT dateDiff('second', min(toTimeZone(raw_sessions.min_timestamp, 'UTC')), max(toTimeZone(raw_sessions.max_timestamp, 'UTC'))) AS duration,
raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
WHERE equals(raw_sessions.team_id, 2)
GROUP BY raw_sessions.session_id_v7,
raw_sessions.session_id_v7) AS raw_session_replay_events__session ON equals(toUInt128(accurateCastOrNull(session_replay_events.session_id, 'UUID')), raw_session_replay_events__session.session_id_v7)
WHERE and(equals(session_replay_events.team_id, 2), ifNull(greater(raw_session_replay_events__session.duration, 3600), 0))
GROUP BY session_replay_events.session_id
HAVING ifNull(equals(dateDiff('second', min(toTimeZone(session_replay_events.min_first_timestamp, 'UTC')), max(toTimeZone(session_replay_events.max_last_timestamp, 'UTC'))), 3600), 0)
Expand Down
122 changes: 94 additions & 28 deletions posthog/hogql/database/test/__snapshots__/test_database.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@
"fields": [
"id",
"session_id",
"session_id_v7",
"team_id",
"distinct_id",
"$start_timestamp",
Expand All @@ -354,8 +355,8 @@
"$num_uniq_urls",
"$entry_current_url",
"$entry_pathname",
"$exit_current_url",
"$exit_pathname",
"$end_current_url",
"$end_pathname",
"$entry_utm_source",
"$entry_utm_campaign",
"$entry_utm_medium",
Expand All @@ -364,13 +365,15 @@
"$entry_referring_domain",
"$entry_gclid",
"$entry_gad_source",
"$event_count_map",
"$pageview_count",
"$autocapture_count",
"$screen_count",
"$channel_type",
"$session_duration",
"duration",
"$is_bounce"
"$is_bounce",
"$last_external_click_url",
"$page_screen_autocapture_count_up_to"
],
"hogql_value": "session",
"name": "session",
Expand Down Expand Up @@ -832,6 +835,7 @@
"fields": [
"id",
"session_id",
"session_id_v7",
"team_id",
"distinct_id",
"$start_timestamp",
Expand All @@ -840,8 +844,8 @@
"$num_uniq_urls",
"$entry_current_url",
"$entry_pathname",
"$exit_current_url",
"$exit_pathname",
"$end_current_url",
"$end_pathname",
"$entry_utm_source",
"$entry_utm_campaign",
"$entry_utm_medium",
Expand All @@ -850,13 +854,15 @@
"$entry_referring_domain",
"$entry_gclid",
"$entry_gad_source",
"$event_count_map",
"$pageview_count",
"$autocapture_count",
"$screen_count",
"$channel_type",
"$session_duration",
"duration",
"$is_bounce"
"$is_bounce",
"$last_external_click_url",
"$page_screen_autocapture_count_up_to"
],
"hogql_value": "session",
"name": "session",
Expand Down Expand Up @@ -1054,6 +1060,15 @@
"table": null,
"type": "string"
},
"session_id_v7": {
"chain": null,
"fields": null,
"hogql_value": "session_id_v7",
"name": "session_id_v7",
"schema_valid": true,
"table": null,
"type": "integer"
},
"distinct_id": {
"chain": null,
"fields": null,
Expand Down Expand Up @@ -1117,20 +1132,20 @@
"table": null,
"type": "string"
},
"$exit_current_url": {
"$end_current_url": {
"chain": null,
"fields": null,
"hogql_value": "`$exit_current_url`",
"name": "$exit_current_url",
"hogql_value": "`$end_current_url`",
"name": "$end_current_url",
"schema_valid": true,
"table": null,
"type": "string"
},
"$exit_pathname": {
"$end_pathname": {
"chain": null,
"fields": null,
"hogql_value": "`$exit_pathname`",
"name": "$exit_pathname",
"hogql_value": "`$end_pathname`",
"name": "$end_pathname",
"schema_valid": true,
"table": null,
"type": "string"
Expand Down Expand Up @@ -1225,6 +1240,15 @@
"table": null,
"type": "integer"
},
"$screen_count": {
"chain": null,
"fields": null,
"hogql_value": "`$screen_count`",
"name": "$screen_count",
"schema_valid": true,
"table": null,
"type": "integer"
},
"$channel_type": {
"chain": null,
"fields": null,
Expand Down Expand Up @@ -1260,6 +1284,15 @@
"schema_valid": true,
"table": null,
"type": "boolean"
},
"$last_external_click_url": {
"chain": null,
"fields": null,
"hogql_value": "`$last_external_click_url`",
"name": "$last_external_click_url",
"schema_valid": true,
"table": null,
"type": "string"
}
},
"id": "sessions",
Expand Down Expand Up @@ -1703,6 +1736,7 @@
"fields": [
"id",
"session_id",
"session_id_v7",
"team_id",
"distinct_id",
"$start_timestamp",
Expand All @@ -1711,8 +1745,8 @@
"$num_uniq_urls",
"$entry_current_url",
"$entry_pathname",
"$exit_current_url",
"$exit_pathname",
"$end_current_url",
"$end_pathname",
"$entry_utm_source",
"$entry_utm_campaign",
"$entry_utm_medium",
Expand All @@ -1721,13 +1755,15 @@
"$entry_referring_domain",
"$entry_gclid",
"$entry_gad_source",
"$event_count_map",
"$pageview_count",
"$autocapture_count",
"$screen_count",
"$channel_type",
"$session_duration",
"duration",
"$is_bounce"
"$is_bounce",
"$last_external_click_url",
"$page_screen_autocapture_count_up_to"
],
"hogql_value": "session",
"name": "session",
Expand Down Expand Up @@ -2189,6 +2225,7 @@
"fields": [
"id",
"session_id",
"session_id_v7",
"team_id",
"distinct_id",
"$start_timestamp",
Expand All @@ -2197,8 +2234,8 @@
"$num_uniq_urls",
"$entry_current_url",
"$entry_pathname",
"$exit_current_url",
"$exit_pathname",
"$end_current_url",
"$end_pathname",
"$entry_utm_source",
"$entry_utm_campaign",
"$entry_utm_medium",
Expand All @@ -2207,13 +2244,15 @@
"$entry_referring_domain",
"$entry_gclid",
"$entry_gad_source",
"$event_count_map",
"$pageview_count",
"$autocapture_count",
"$screen_count",
"$channel_type",
"$session_duration",
"duration",
"$is_bounce"
"$is_bounce",
"$last_external_click_url",
"$page_screen_autocapture_count_up_to"
],
"hogql_value": "session",
"name": "session",
Expand Down Expand Up @@ -2411,6 +2450,15 @@
"table": null,
"type": "string"
},
"session_id_v7": {
"chain": null,
"fields": null,
"hogql_value": "session_id_v7",
"name": "session_id_v7",
"schema_valid": true,
"table": null,
"type": "integer"
},
"distinct_id": {
"chain": null,
"fields": null,
Expand Down Expand Up @@ -2474,20 +2522,20 @@
"table": null,
"type": "string"
},
"$exit_current_url": {
"$end_current_url": {
"chain": null,
"fields": null,
"hogql_value": "`$exit_current_url`",
"name": "$exit_current_url",
"hogql_value": "`$end_current_url`",
"name": "$end_current_url",
"schema_valid": true,
"table": null,
"type": "string"
},
"$exit_pathname": {
"$end_pathname": {
"chain": null,
"fields": null,
"hogql_value": "`$exit_pathname`",
"name": "$exit_pathname",
"hogql_value": "`$end_pathname`",
"name": "$end_pathname",
"schema_valid": true,
"table": null,
"type": "string"
Expand Down Expand Up @@ -2582,6 +2630,15 @@
"table": null,
"type": "integer"
},
"$screen_count": {
"chain": null,
"fields": null,
"hogql_value": "`$screen_count`",
"name": "$screen_count",
"schema_valid": true,
"table": null,
"type": "integer"
},
"$channel_type": {
"chain": null,
"fields": null,
Expand Down Expand Up @@ -2617,6 +2674,15 @@
"schema_valid": true,
"table": null,
"type": "boolean"
},
"$last_external_click_url": {
"chain": null,
"fields": null,
"hogql_value": "`$last_external_click_url`",
"name": "$last_external_click_url",
"schema_valid": true,
"table": null,
"type": "string"
}
},
"id": "sessions",
Expand Down
16 changes: 8 additions & 8 deletions posthog/hogql/test/__snapshots__/test_query.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@

SELECT sessions.session_id AS session_id, sessions.`$entry_current_url` AS `$entry_current_url`
FROM (
SELECT sessions.session_id AS session_id, nullIf(nullIf(argMinMerge(sessions.entry_url), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_current_url`, min(toTimeZone(sessions.min_timestamp, %(hogql_val_2)s)) AS `$start_timestamp`
FROM sessions
WHERE and(equals(sessions.team_id, 420), ifNull(lessOrEquals(minus(toTimeZone(sessions.min_timestamp, %(hogql_val_3)s), toIntervalDay(3)), toDateTime64('2024-07-06 00:00:00.000000', 6, 'UTC')), 0), ifNull(greaterOrEquals(plus(toTimeZone(sessions.min_timestamp, %(hogql_val_4)s), toIntervalDay(3)), toDateTime64('2024-07-04 00:00:00.000000', 6, 'UTC')), 0))
GROUP BY sessions.session_id, sessions.session_id) AS sessions
SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, nullIf(nullIf(argMinMerge(raw_sessions.entry_url), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_current_url`, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_2)s)) AS `$start_timestamp`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
WHERE equals(raw_sessions.team_id, 420)
GROUP BY raw_sessions.session_id_v7, raw_sessions.session_id_v7) AS sessions
WHERE and(ifNull(less(sessions.`$start_timestamp`, toDateTime64('2024-07-06 00:00:00.000000', 6, 'UTC')), 0), ifNull(greaterOrEquals(sessions.`$start_timestamp`, toDateTime64('2024-07-04 00:00:00.000000', 6, 'UTC')), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, format_csv_allow_double_quotes=0, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0
Expand Down Expand Up @@ -136,10 +136,10 @@

SELECT sessions.session_id AS session_id, sessions.`$entry_current_url` AS `$entry_current_url`
FROM (
SELECT sessions.session_id AS session_id, nullIf(nullIf(argMinMerge(sessions.entry_url), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_current_url`
FROM sessions
WHERE equals(sessions.team_id, 420)
GROUP BY sessions.session_id, sessions.session_id) AS sessions
SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, nullIf(nullIf(argMinMerge(raw_sessions.entry_url), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_current_url`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
WHERE equals(raw_sessions.team_id, 420)
GROUP BY raw_sessions.session_id_v7, raw_sessions.session_id_v7) AS sessions
WHERE and(ifNull(equals(sessions.`$entry_current_url`, %(hogql_val_2)s), 0), 1)
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, format_csv_allow_double_quotes=0, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0
Expand Down
Loading

0 comments on commit b434bb8

Please sign in to comment.