Skip to content

Commit

Permalink
feat(hogql): session.id and session.duration rename (#18215)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Oct 26, 2023
1 parent 428542a commit 395ff40
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
12 changes: 6 additions & 6 deletions posthog/hogql/database/schema/event_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
"""
)

Expand All @@ -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=""),
),
Expand All @@ -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"]),
)
)

Expand Down
8 changes: 4 additions & 4 deletions posthog/hogql/database/test/__snapshots__/test_database.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@
"type": "lazy_table",
"table": "events",
"fields": [
"$session_id",
"session_duration"
"id",
"duration"
]
}
],
Expand Down Expand Up @@ -1022,8 +1022,8 @@
"type": "lazy_table",
"table": "events",
"fields": [
"$session_id",
"session_duration"
"id",
"duration"
]
}
],
Expand Down
28 changes: 28 additions & 0 deletions posthog/hogql/test/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]

0 comments on commit 395ff40

Please sign in to comment.