diff --git a/posthog/hogql/database/schema/event_sessions.py b/posthog/hogql/database/schema/event_sessions.py index a6951478ada4a..e049b9dcaf103 100644 --- a/posthog/hogql/database/schema/event_sessions.py +++ b/posthog/hogql/database/schema/event_sessions.py @@ -15,8 +15,8 @@ class EventsSessionSubTable(VirtualTable): fields: Dict[str, FieldOrTable] = { - "$session_id": StringDatabaseField(name="$session_id"), - "session_duration": IntegerDatabaseField(name="session_duration"), + "id": StringDatabaseField(name="$session_id"), + "duration": IntegerDatabaseField(name="session_duration"), } def to_printed_clickhouse(self, context): @@ -141,9 +141,9 @@ def join_with_events_table_session_duration( ): select_query = parse_select( """ - select "$session_id", dateDiff('second', min(timestamp), max(timestamp)) as session_duration + select "$session_id" as id, dateDiff('second', min(timestamp), max(timestamp)) as duration from events - group by "$session_id" + group by id """ ) @@ -157,7 +157,7 @@ def join_with_events_table_session_duration( exprs=[ *compare_operators, ast.CompareOperation( - left=ast.Field(chain=["$session_id"]), + left=ast.Field(chain=["id"]), op=ast.CompareOperationOp.NotEq, right=ast.Constant(value=""), ), @@ -171,7 +171,7 @@ def join_with_events_table_session_duration( expr=ast.CompareOperation( op=ast.CompareOperationOp.Eq, left=ast.Field(chain=[from_table, "$session_id"]), - right=ast.Field(chain=[to_table, "$session_id"]), + right=ast.Field(chain=[to_table, "id"]), ) ) diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambr index 90bc08c457891..98a17e3b3821f 100644 --- a/posthog/hogql/database/test/__snapshots__/test_database.ambr +++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr @@ -232,8 +232,8 @@ "type": "lazy_table", "table": "events", "fields": [ - "$session_id", - "session_duration" + "id", + "duration" ] } ], @@ -1022,8 +1022,8 @@ "type": "lazy_table", "table": "events", "fields": [ - "$session_id", - "session_duration" + "id", + "duration" ] } ], diff --git a/posthog/hogql/test/test_query.py b/posthog/hogql/test/test_query.py index 0d6cf4342dead..235ef2d251ab1 100644 --- a/posthog/hogql/test/test_query.py +++ b/posthog/hogql/test/test_query.py @@ -1492,3 +1492,31 @@ def test_hogql_union_all_limits(self): f"SELECT event FROM events LIMIT 100 UNION ALL SELECT event FROM events LIMIT 100", ) assert pretty_print_in_tests(response.clickhouse, self.team.pk) == self.snapshot + + def test_events_sessions_table(self): + with freeze_time("2020-01-10 12:00:00"): + random_uuid = self._create_random_events() + + with freeze_time("2020-01-10 12:10:00"): + _create_event( + distinct_id=random_uuid, + event="random event", + team=self.team, + properties={"$session_id": random_uuid}, + ) + with freeze_time("2020-01-10 12:20:00"): + _create_event( + distinct_id=random_uuid, + event="random event", + team=self.team, + properties={"$session_id": random_uuid}, + ) + + query = "SELECT session.id, session.duration from events WHERE distinct_id={distinct_id} order by timestamp" + response = execute_hogql_query( + query, team=self.team, placeholders={"distinct_id": ast.Constant(value=random_uuid)} + ) + assert response.results == [ + (random_uuid, 600), + (random_uuid, 600), + ]