Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix column order difference between creation and migration in raw_sessions #24505

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions posthog/clickhouse/test/__snapshots__/test_schema.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1758,11 +1758,12 @@
autocapture_uniq AggregateFunction(uniq, Nullable(UUID)),
screen_count SimpleAggregateFunction(sum, Int64),
screen_uniq AggregateFunction(uniq, Nullable(UUID)),
-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID)),

-- replay
maybe_has_session_replay SimpleAggregateFunction(max, Bool) -- will be written False to by the events table mv and True to by the replay table mv
maybe_has_session_replay SimpleAggregateFunction(max, Bool), -- will be written False to by the events table mv and True to by the replay table mv

-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID))
) ENGINE = Distributed('posthog', 'posthog_test', 'sharded_raw_sessions', cityHash64(session_id_v7))

'''
Expand Down Expand Up @@ -1833,10 +1834,12 @@
uniqState(if(event='$autocapture', uuid, NULL)) as autocapture_uniq,
sumIf(1, event='$screen') as screen_count,
uniqState(if(event='$screen', uuid, NULL)) as screen_uniq,
uniqUpToState(1)(if(event='$pageview' OR event='$screen' OR event='$autocapture', uuid, NULL)) as page_screen_autocapture_uniq_up_to,

-- replay
false as maybe_has_session_replay
false as maybe_has_session_replay,

-- perf
uniqUpToState(1)(if(event='$pageview' OR event='$screen' OR event='$autocapture', uuid, NULL)) as page_screen_autocapture_uniq_up_to
FROM posthog_test.sharded_events
WHERE bitAnd(bitShiftRight(toUInt128(accurateCastOrNull(`$session_id`, 'UUID')), 76), 0xF) == 7 -- has a session id and is valid uuidv7)
GROUP BY
Expand Down Expand Up @@ -2422,11 +2425,12 @@
autocapture_uniq AggregateFunction(uniq, Nullable(UUID)),
screen_count SimpleAggregateFunction(sum, Int64),
screen_uniq AggregateFunction(uniq, Nullable(UUID)),
-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID)),

-- replay
maybe_has_session_replay SimpleAggregateFunction(max, Bool) -- will be written False to by the events table mv and True to by the replay table mv
maybe_has_session_replay SimpleAggregateFunction(max, Bool), -- will be written False to by the events table mv and True to by the replay table mv

-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID))
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_{shard}/posthog.raw_sessions', '{replica}')

PARTITION BY toYYYYMM(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(session_id_v7, 80)), 1000)))
Expand Down Expand Up @@ -2723,11 +2727,12 @@
autocapture_uniq AggregateFunction(uniq, Nullable(UUID)),
screen_count SimpleAggregateFunction(sum, Int64),
screen_uniq AggregateFunction(uniq, Nullable(UUID)),
-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID)),

-- replay
maybe_has_session_replay SimpleAggregateFunction(max, Bool) -- will be written False to by the events table mv and True to by the replay table mv
maybe_has_session_replay SimpleAggregateFunction(max, Bool), -- will be written False to by the events table mv and True to by the replay table mv

-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID))
) ENGINE = Distributed('posthog', 'posthog_test', 'sharded_raw_sessions', cityHash64(session_id_v7))

'''
Expand Down Expand Up @@ -3533,11 +3538,12 @@
autocapture_uniq AggregateFunction(uniq, Nullable(UUID)),
screen_count SimpleAggregateFunction(sum, Int64),
screen_uniq AggregateFunction(uniq, Nullable(UUID)),
-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID)),

-- replay
maybe_has_session_replay SimpleAggregateFunction(max, Bool) -- will be written False to by the events table mv and True to by the replay table mv
maybe_has_session_replay SimpleAggregateFunction(max, Bool), -- will be written False to by the events table mv and True to by the replay table mv

-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID))
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_{shard}/posthog.raw_sessions', '{replica}')

PARTITION BY toYYYYMM(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(session_id_v7, 80)), 1000)))
Expand Down
27 changes: 27 additions & 0 deletions posthog/clickhouse/test/test_raw_sessions_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from posthog.clickhouse.client import sync_execute
from posthog.models.raw_sessions.sql import RAW_SESSION_TABLE_BACKFILL_SELECT_SQL
from posthog.models.utils import uuid7
from posthog.test.base import (
_create_event,
ClickhouseTestMixin,
BaseTest,
)


class TestRawSessionsModel(ClickhouseTestMixin, BaseTest):
def test_backfill_sql(self):
distinct_id = str(uuid7())
session_id = str(uuid7())
_create_event(
team=self.team,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/", "$session_id": session_id},
timestamp="2024-03-08",
)

# just test that the backfill SQL can be run without error
sync_execute(
"INSERT INTO raw_sessions" + RAW_SESSION_TABLE_BACKFILL_SELECT_SQL() + "AND team_id = %(team_id)s",
{"team_id": self.team.id},
)
1 change: 1 addition & 0 deletions posthog/models/raw_sessions/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ADD COLUMN IF NOT EXISTS
page_screen_autocapture_uniq_up_to
AggregateFunction(uniqUpTo(1), Nullable(UUID))
AFTER maybe_has_session_replay
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should have been included before

"""

BASE_RAW_SESSIONS_ADD_PAGEVIEW_AUTOCAPTURE_SCREEN_UP_TO_2_COLUMN_SQL = (
Expand Down
25 changes: 16 additions & 9 deletions posthog/models/raw_sessions/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@
autocapture_uniq AggregateFunction(uniq, Nullable(UUID)),
screen_count SimpleAggregateFunction(sum, Int64),
screen_uniq AggregateFunction(uniq, Nullable(UUID)),
-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID)),

-- replay
maybe_has_session_replay SimpleAggregateFunction(max, Bool) -- will be written False to by the events table mv and True to by the replay table mv
maybe_has_session_replay SimpleAggregateFunction(max, Bool), -- will be written False to by the events table mv and True to by the replay table mv

-- as a performance optimisation, also keep track of the uniq events for all of these combined, a bounce is a session with <2 of these
page_screen_autocapture_uniq_up_to AggregateFunction(uniqUpTo(1), Nullable(UUID))
) ENGINE = {engine}
"""

Expand Down Expand Up @@ -214,10 +215,12 @@ def source_int_column(column_name: str) -> str:
initializeAggregation('uniqState', if(event='autocapture', uuid, NULL)) as autocapture_uniq,
if(event='$screen', 1, 0) as screen_count,
initializeAggregation('uniqState', if(event='screen', uuid, NULL)) as screen_uniq,
initializeAggregation('uniqUpToState(1)', if(event='$pageview' OR event='$screen' OR event='$autocapture', uuid, NULL)) as page_screen_autocapture_uniq_up_to,

-- replay
false as maybe_has_session_replay
false as maybe_has_session_replay,

-- perf
initializeAggregation('uniqUpToState(1)', if(event='$pageview' OR event='$screen' OR event='$autocapture', uuid, NULL)) as page_screen_autocapture_uniq_up_to
FROM {database}.events
WHERE bitAnd(bitShiftRight(toUInt128(accurateCastOrNull(`$session_id`, 'UUID')), 76), 0xF) == 7 -- has a session id and is valid uuidv7
""".format(
Expand Down Expand Up @@ -321,10 +324,12 @@ def source_int_column(column_name: str) -> str:
uniqState(if(event='$autocapture', uuid, NULL)) as autocapture_uniq,
sumIf(1, event='$screen') as screen_count,
uniqState(if(event='$screen', uuid, NULL)) as screen_uniq,
uniqUpToState(1)(if(event='$pageview' OR event='$screen' OR event='$autocapture', uuid, NULL)) as page_screen_autocapture_uniq_up_to,

-- replay
false as maybe_has_session_replay
false as maybe_has_session_replay,

-- perf
uniqUpToState(1)(if(event='$pageview' OR event='$screen' OR event='$autocapture', uuid, NULL)) as page_screen_autocapture_uniq_up_to
FROM {database}.sharded_events
WHERE bitAnd(bitShiftRight(toUInt128(accurateCastOrNull(`$session_id`, 'UUID')), 76), 0xF) == 7 -- has a session id and is valid uuidv7)
GROUP BY
Expand Down Expand Up @@ -429,7 +434,7 @@ def source_int_column(column_name: str) -> str:
session_id_v7,
fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(session_id_v7, 80)), 1000)) as session_timestamp,
team_id,
any(distinct_id) as distinct_id,
argMaxMerge(distinct_id) as distinct_id,
min(min_timestamp) as min_timestamp,
max(max_timestamp) as max_timestamp,

Expand Down Expand Up @@ -483,7 +488,9 @@ def source_int_column(column_name: str) -> str:
sum(screen_count) as screen_count,
uniqMerge(screen_uniq) as screen_uniq,

max(maybe_has_session_replay) as maybe_has_session_replay
max(maybe_has_session_replay) as maybe_has_session_replay,

uniqUpToMerge(1)(page_screen_autocapture_uniq_up_to) as page_screen_autocapture_uniq_up_to
FROM {TABLE_BASE_NAME}
GROUP BY session_id_v7, team_id
"""
Expand Down
Loading