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

feat: cohort tests #26559

Open
wants to merge 290 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
290 commits
Select commit Hold shift + click to select a range
e6f943e
new stickiness
aspicer Nov 25, 2024
684dc1f
changes
aspicer Nov 25, 2024
a212b3d
front end
aspicer Nov 25, 2024
0893f51
stickiness
aspicer Nov 25, 2024
68c06c8
fix mypy
aspicer Nov 25, 2024
3efbe58
merge and rebuild schema
aspicer Nov 25, 2024
ea0d388
Update UI snapshots for `webkit` (2)
github-actions[bot] Nov 25, 2024
88cafe8
Update query snapshots
github-actions[bot] Nov 25, 2024
f1741ef
Update UI snapshots for `chromium` (2)
github-actions[bot] Nov 25, 2024
e994f37
hm
aspicer Nov 26, 2024
bef27a2
Merge branch 'aspicer/stickiness' of github.com:PostHog/posthog into …
aspicer Nov 26, 2024
07c8d2f
Merge remote-tracking branch 'origin/master' into aspicer/stickiness
aspicer Nov 26, 2024
fbcb6b2
hm
aspicer Nov 26, 2024
d6d1775
Update UI snapshots for `webkit` (2)
github-actions[bot] Nov 26, 2024
4febf88
Update UI snapshots for `chromium` (2)
github-actions[bot] Nov 26, 2024
5c53e0c
hm
aspicer Nov 26, 2024
8f9ba80
Merge branch 'aspicer/stickiness' of github.com:PostHog/posthog into …
aspicer Nov 26, 2024
28fbccf
merge
aspicer Nov 26, 2024
67405e0
hm
aspicer Nov 26, 2024
210b444
Update query snapshots
github-actions[bot] Nov 26, 2024
39bc9bd
event regularly
aspicer Nov 26, 2024
93f362b
ahhhh
aspicer Nov 26, 2024
0949181
Merge branch 'aspicer/stickiness' of github.com:PostHog/posthog into …
aspicer Nov 26, 2024
f076b47
Merge remote-tracking branch 'origin/master' into aspicer/stickiness
aspicer Nov 26, 2024
07208fa
Merge remote-tracking branch 'origin/master' into aspicer/cohort-hogql
aspicer Nov 26, 2024
3d74d8f
Merge branch 'aspicer/stickiness' into aspicer/cohort-hogql
aspicer Nov 26, 2024
d12b4fe
count
aspicer Nov 26, 2024
394034a
17 fails
aspicer Nov 26, 2024
35f03e0
merge
aspicer Nov 26, 2024
b063527
12 failing
aspicer Nov 26, 2024
ee12d1c
need distinct
aspicer Nov 27, 2024
bffea7b
distinct
aspicer Nov 27, 2024
378ab67
Merge branch 'aspicer/distinct' into aspicer/cohort-hogql
aspicer Nov 27, 2024
ade0d0a
remove duplicates
aspicer Nov 27, 2024
ded003c
12 fails
aspicer Nov 27, 2024
57ba402
8 failing
aspicer Nov 27, 2024
6f3280e
6 failing
aspicer Nov 27, 2024
972e7c4
bababababa
aspicer Nov 27, 2024
c6fd22f
two failing
aspicer Nov 27, 2024
fbfe478
negation ors
aspicer Nov 27, 2024
c38b4da
tests
aspicer Nov 30, 2024
8ae8293
merge
aspicer Nov 30, 2024
3c8ab2f
Update query snapshots
github-actions[bot] Nov 30, 2024
1c8a997
Update query snapshots
github-actions[bot] Nov 30, 2024
4413320
Update query snapshots
github-actions[bot] Nov 30, 2024
900fcd7
Update query snapshots
github-actions[bot] Nov 30, 2024
17cb8a7
null
aspicer Nov 30, 2024
7f17d99
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Nov 30, 2024
62f25d4
sort
aspicer Nov 30, 2024
7a59266
ruff
aspicer Nov 30, 2024
595d6e5
scheam
aspicer Nov 30, 2024
bd575c2
ruff
aspicer Nov 30, 2024
f8aadff
clean up
aspicer Nov 30, 2024
ada0f71
trends revert
aspicer Nov 30, 2024
e4144fd
date check wrapper
aspicer Dec 2, 2024
cfe1a84
merge
aspicer Dec 10, 2024
81a8df2
hm
aspicer Dec 10, 2024
52162eb
execute
aspicer Dec 10, 2024
1955422
Update query snapshots
github-actions[bot] Dec 10, 2024
6a063c5
utils
aspicer Dec 13, 2024
ffc6d8f
Update UI snapshots for `chromium` (2)
github-actions[bot] Dec 13, 2024
642b734
Update UI snapshots for `chromium` (2)
github-actions[bot] Dec 13, 2024
1fcd939
Merge remote-tracking branch 'origin/master' into aspicer/cohort-hogql
aspicer Dec 18, 2024
6bfad9a
compare
aspicer Dec 18, 2024
fa555ec
date ranges, need to figure out settinsg
aspicer Dec 18, 2024
f2f743b
Update query snapshots
github-actions[bot] Dec 18, 2024
889e98b
Update query snapshots
github-actions[bot] Dec 18, 2024
9365832
Update query snapshots
github-actions[bot] Dec 18, 2024
2f6166d
Update query snapshots
github-actions[bot] Dec 18, 2024
c39a8d6
Update query snapshots
github-actions[bot] Dec 18, 2024
06f8d42
query
aspicer Dec 19, 2024
e918a85
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Dec 19, 2024
711f8d7
fix bug
aspicer Dec 19, 2024
74e8a45
working
aspicer Dec 19, 2024
264e135
Merge remote-tracking branch 'origin/master' into aspicer/cohort-hogql
aspicer Dec 19, 2024
1f7275a
Update UI snapshots for `chromium` (2)
github-actions[bot] Dec 19, 2024
0b59911
Update UI snapshots for `chromium` (2)
github-actions[bot] Dec 19, 2024
7ba2f4c
mypy
aspicer Dec 19, 2024
244f40c
mypy garbage
aspicer Dec 19, 2024
5202539
type disaster
aspicer Dec 19, 2024
5c4a167
Update UI snapshots for `chromium` (2)
github-actions[bot] Dec 19, 2024
b4686b1
Update UI snapshots for `chromium` (2)
github-actions[bot] Dec 20, 2024
d713865
mypy
aspicer Dec 20, 2024
244ec10
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Dec 20, 2024
d18a0e4
push
aspicer Dec 20, 2024
a548e88
removed
aspicer Dec 20, 2024
9017ed3
requirements
aspicer Dec 20, 2024
5635442
don't allow ints
aspicer Dec 23, 2024
86a26a0
Update query snapshots
github-actions[bot] Dec 23, 2024
03f1b90
Update query snapshots
github-actions[bot] Dec 23, 2024
a0b0a8a
hogql
aspicer Dec 23, 2024
80ab02b
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Dec 23, 2024
8b40bba
Update query snapshots
github-actions[bot] Dec 24, 2024
45a5029
Update query snapshots
github-actions[bot] Dec 24, 2024
fff4414
Update query snapshots
github-actions[bot] Dec 24, 2024
64010b3
Update query snapshots
github-actions[bot] Dec 24, 2024
66837c1
Update query snapshots
github-actions[bot] Dec 24, 2024
1b08aed
Update query snapshots
github-actions[bot] Dec 24, 2024
471f77d
Update query snapshots
github-actions[bot] Dec 24, 2024
a5a940f
Update query snapshots
github-actions[bot] Dec 24, 2024
7917cd3
Update query snapshots
github-actions[bot] Dec 24, 2024
cb4c637
Update query snapshots
github-actions[bot] Dec 24, 2024
f1ff8e4
Update query snapshots
github-actions[bot] Dec 24, 2024
0edc8cb
Update query snapshots
github-actions[bot] Dec 24, 2024
3dd1cb0
Update query snapshots
github-actions[bot] Dec 24, 2024
944a96e
Update query snapshots
github-actions[bot] Dec 24, 2024
cc7d2c9
Update query snapshots
github-actions[bot] Dec 24, 2024
a9413cf
Update query snapshots
github-actions[bot] Dec 24, 2024
edad3a8
Merge remote-tracking branch 'origin/master' into aspicer/cohort-hogql
aspicer Dec 24, 2024
c613730
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Dec 24, 2024
d4c9760
freeze time for snapshot
aspicer Dec 24, 2024
13ff230
Update query snapshots
github-actions[bot] Dec 24, 2024
eb31756
Update UI snapshots for `chromium` (2)
github-actions[bot] Dec 24, 2024
b807068
Update query snapshots
github-actions[bot] Dec 24, 2024
bfa187c
sign -1
aspicer Dec 24, 2024
4a0d6be
hm
aspicer Dec 24, 2024
97c2de6
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Dec 24, 2024
2e36667
mypy baseline
aspicer Dec 24, 2024
3a018b2
time logging
aspicer Dec 24, 2024
70a1aa5
Update query snapshots
github-actions[bot] Dec 24, 2024
db48657
Update query snapshots
github-actions[bot] Dec 24, 2024
23de222
simplify
aspicer Dec 24, 2024
2b5ebe5
clean
aspicer Dec 24, 2024
f02e365
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Dec 24, 2024
7c62b68
remove comment
aspicer Dec 24, 2024
caae8dc
Update query snapshots
github-actions[bot] Dec 24, 2024
54dc46b
Update UI snapshots for `chromium` (1)
github-actions[bot] Dec 24, 2024
8220512
Update query snapshots
github-actions[bot] Dec 24, 2024
aa4b3ba
Update UI snapshots for `chromium` (1)
github-actions[bot] Dec 24, 2024
182e24f
Update query snapshots
github-actions[bot] Dec 24, 2024
803d0f8
wip checkpoint
aspicer Dec 24, 2024
9023e20
add tests
aspicer Dec 24, 2024
f148b4c
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Dec 24, 2024
7e229f9
Update query snapshots
github-actions[bot] Dec 24, 2024
d46a4df
hm
aspicer Dec 24, 2024
e99a104
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Dec 24, 2024
74c2dc6
Merge remote-tracking branch 'origin/master' into aspicer/cohort-hogql
aspicer Jan 6, 2025
042aa22
mypy
aspicer Jan 6, 2025
caf179d
test
aspicer Jan 6, 2025
b70f60d
fix type error
aspicer Jan 6, 2025
11d368f
1
aspicer Jan 6, 2025
e3de993
change
aspicer Jan 7, 2025
d645991
revert change
aspicer Jan 7, 2025
77bce1b
Update query snapshots
github-actions[bot] Jan 7, 2025
b66c647
actors query
aspicer Jan 7, 2025
2dcc284
Update query snapshots
github-actions[bot] Jan 7, 2025
cb19cbd
Update query snapshots
github-actions[bot] Jan 7, 2025
55ad6a7
Update UI snapshots for `chromium` (2)
github-actions[bot] Jan 7, 2025
28f2371
py
aspicer Jan 7, 2025
aba4d4c
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 7, 2025
4bdeee6
add request
aspicer Jan 7, 2025
433c631
Merge branch 'aspicer/test_change' of github.com:PostHog/posthog into…
aspicer Jan 7, 2025
6092c45
Update UI snapshots for `chromium` (2)
github-actions[bot] Jan 7, 2025
785b2b9
Update query snapshots
github-actions[bot] Jan 7, 2025
d4f1bf7
actors query
aspicer Jan 7, 2025
8c3276a
source
aspicer Jan 7, 2025
42cb44e
Merge branch 'aspicer/test_change' of github.com:PostHog/posthog into…
aspicer Jan 7, 2025
e364202
merge
aspicer Jan 7, 2025
87083af
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 7, 2025
2d7df7b
Update query snapshots
github-actions[bot] Jan 7, 2025
33dc728
remove calculate
aspicer Jan 7, 2025
1c91fd7
Merge branch 'aspicer/test_change' of github.com:PostHog/posthog into…
aspicer Jan 7, 2025
742b6b4
Update query snapshots
github-actions[bot] Jan 7, 2025
883670e
m
aspicer Jan 7, 2025
2e2f3ca
undo freeze
aspicer Jan 7, 2025
3239ec2
Update query snapshots
github-actions[bot] Jan 7, 2025
665142c
Update query snapshots
github-actions[bot] Jan 7, 2025
097496a
type
aspicer Jan 7, 2025
22b22b7
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 7, 2025
69f7e3e
Update query snapshots
github-actions[bot] Jan 7, 2025
880745a
Update query snapshots
github-actions[bot] Jan 7, 2025
63933ac
tests
aspicer Jan 7, 2025
b6dda40
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 7, 2025
619729f
Update UI snapshots for `chromium` (1)
github-actions[bot] Jan 7, 2025
b563b99
tests
aspicer Jan 7, 2025
3e4986e
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 7, 2025
95dc161
Update query snapshots
github-actions[bot] Jan 7, 2025
67752a3
Update UI snapshots for `chromium` (1)
github-actions[bot] Jan 7, 2025
8da6233
Update query snapshots
github-actions[bot] Jan 7, 2025
80c347f
Update query snapshots
github-actions[bot] Jan 7, 2025
4eb1843
Update query snapshots
github-actions[bot] Jan 7, 2025
403e82c
Update query snapshots
github-actions[bot] Jan 7, 2025
cbb8aff
Update query snapshots
github-actions[bot] Jan 7, 2025
40d9dee
Update query snapshots
github-actions[bot] Jan 7, 2025
b45227b
Update query snapshots
github-actions[bot] Jan 7, 2025
c382ed5
Update query snapshots
github-actions[bot] Jan 7, 2025
640fc43
Update query snapshots
github-actions[bot] Jan 7, 2025
a185f17
Update query snapshots
github-actions[bot] Jan 7, 2025
8ca0e49
Update query snapshots
github-actions[bot] Jan 7, 2025
8deb486
Update query snapshots
github-actions[bot] Jan 7, 2025
3f50dcb
Update query snapshots
github-actions[bot] Jan 7, 2025
ad7f1bd
Update query snapshots
github-actions[bot] Jan 7, 2025
07f94e2
Update query snapshots
github-actions[bot] Jan 7, 2025
f1b6375
Update query snapshots
github-actions[bot] Jan 7, 2025
95e12dc
Update query snapshots
github-actions[bot] Jan 7, 2025
77a464d
Update query snapshots
github-actions[bot] Jan 7, 2025
d55a0b4
Update query snapshots
github-actions[bot] Jan 7, 2025
71dd022
Update query snapshots
github-actions[bot] Jan 7, 2025
a88f6c1
fix
aspicer Jan 7, 2025
5f50e4e
Update query snapshots
github-actions[bot] Jan 7, 2025
ac4c08b
Update query snapshots
github-actions[bot] Jan 7, 2025
05f8551
Merge branch 'aspicer/test_change' of github.com:PostHog/posthog into…
aspicer Jan 7, 2025
477524e
merge
aspicer Jan 7, 2025
5650c8b
Update query snapshots
github-actions[bot] Jan 7, 2025
daca52c
Update query snapshots
github-actions[bot] Jan 7, 2025
0036bdc
remove snapshot
aspicer Jan 7, 2025
e6bcf2e
Update query snapshots
github-actions[bot] Jan 7, 2025
f6f2479
Update query snapshots
github-actions[bot] Jan 7, 2025
806f84d
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 7, 2025
b374c5b
Update query snapshots
github-actions[bot] Jan 7, 2025
a0f5d33
test
aspicer Jan 7, 2025
bdf87e2
Update query snapshots
github-actions[bot] Jan 7, 2025
06ccf60
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 7, 2025
ea8efae
Update query snapshots
github-actions[bot] Jan 7, 2025
3aed6a8
wip
aspicer Jan 8, 2025
f870f31
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 8, 2025
070140c
Update query snapshots
github-actions[bot] Jan 8, 2025
378542b
stickiness
aspicer Jan 8, 2025
75cd8a0
stickiness actors
aspicer Jan 8, 2025
5430f48
merge
aspicer Jan 8, 2025
8776f51
Update query snapshots
github-actions[bot] Jan 8, 2025
2c63907
Update UI snapshots for `chromium` (2)
github-actions[bot] Jan 8, 2025
a6a6164
Update UI snapshots for `chromium` (2)
github-actions[bot] Jan 8, 2025
5323c3f
fix regularly
aspicer Jan 8, 2025
1d4ef2b
hm
aspicer Jan 8, 2025
f3b941b
Merge branch 'aspicer/stickiness' of github.com:PostHog/posthog into …
aspicer Jan 8, 2025
69b392a
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 8, 2025
7a7952f
Merge branch 'aspicer/stickiness' into aspicer/cohort-hogql
aspicer Jan 8, 2025
886151d
Update query snapshots
github-actions[bot] Jan 8, 2025
4ffeb51
Update query snapshots
github-actions[bot] Jan 8, 2025
0d55dca
hm
aspicer Jan 8, 2025
31bea57
Merge branch 'aspicer/stickiness' of github.com:PostHog/posthog into …
aspicer Jan 8, 2025
7f996a9
Merge branch 'aspicer/stickiness' into aspicer/cohort-hogql
aspicer Jan 8, 2025
8723b12
qqq
aspicer Jan 8, 2025
263f494
Merge remote-tracking branch 'origin/master' into aspicer/stickiness
aspicer Jan 8, 2025
306dbd0
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 8, 2025
5c8ba15
Merge branch 'aspicer/stickiness' into aspicer/cohort-hogql
aspicer Jan 8, 2025
0052418
Update UI snapshots for `chromium` (2)
github-actions[bot] Jan 8, 2025
7fc0048
Update UI snapshots for `chromium` (2)
github-actions[bot] Jan 8, 2025
86a5fb3
day
aspicer Jan 9, 2025
9b57034
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 9, 2025
534d89b
Update query snapshots
github-actions[bot] Jan 9, 2025
928cc14
Update query snapshots
github-actions[bot] Jan 9, 2025
dd64548
increment to even
aspicer Jan 16, 2025
8a18e88
merge and regenerate
aspicer Jan 16, 2025
b0fc024
Update query snapshots
github-actions[bot] Jan 16, 2025
a7035ad
Update query snapshots
github-actions[bot] Jan 16, 2025
60083fb
Update query snapshots
github-actions[bot] Jan 16, 2025
d45c8a0
4
aspicer Jan 17, 2025
0f93541
Merge branch 'aspicer/cohort-hogql' of github.com:PostHog/posthog int…
aspicer Jan 17, 2025
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
4 changes: 2 additions & 2 deletions docker/clickhouse/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
<!-- Interval of flushing data. -->
<flush_interval_milliseconds>7500</flush_interval_milliseconds>
<!-- Maximal size in lines for the logs. When non-flushed logs amount reaches max_size, logs dumped to the disk. -->
<max_size_rows>1048576</max_size_rows>
<max_size_rows>8388608</max_size_rows>
<!-- Pre-allocated size in lines for the logs. -->
<reserved_size_rows>8192</reserved_size_rows>
<!-- Lines amount threshold, reaching it launches flushing logs to the disk in background. -->
Expand All @@ -239,7 +239,7 @@
<table>query_thread_log</table>
<partition_by>toYYYYMM(event_date)</partition_by>
<flush_interval_milliseconds>7500</flush_interval_milliseconds>
<max_size_rows>1048576</max_size_rows>
<max_size_rows>8388608</max_size_rows>
<reserved_size_rows>8192</reserved_size_rows>
<buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
<flush_on_crash>false</flush_on_crash>
Expand Down
251 changes: 241 additions & 10 deletions ee/clickhouse/models/test/__snapshots__/test_cohort.ambr

Large diffs are not rendered by default.

91 changes: 59 additions & 32 deletions ee/clickhouse/models/test/test_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from posthog.client import sync_execute
from posthog.hogql.hogql import HogQLContext
from posthog.models.action import Action
from posthog.models.cohort import Cohort
from posthog.models.cohort import Cohort, get_and_update_pending_version
from posthog.models.cohort.sql import GET_COHORTPEOPLE_BY_COHORT_ID
from posthog.models.cohort.util import format_filter_query
from posthog.models.filters import Filter
Expand Down Expand Up @@ -68,6 +68,21 @@ def get_person_ids_by_cohort_id(
return [str(row[0]) for row in results]


def calculate_cohort_hogql_test_harness(cohort: Cohort, pending_version: int):
version = pending_version * 2 + 2
cohort.calculate_people_ch(version)
query = f"""
SELECT count() FROM
(SELECT person_id FROM cohortpeople as cp WHERE cp.version = {version} and cp.cohort_id = {cohort.pk}) as cp1
FULL OUTER JOIN (SELECT person_id FROM cohortpeople as cp WHERE cp.version = {version-1} and cp.cohort_id = {cohort.pk}) as cp2
ON cp1.person_id = cp2.person_id
WHERE empty(cp1.person_id) or empty(cp2.person_id)
"""
result = sync_execute(query)
assert 0 == result[0][0]
return version


class TestCohort(ClickhouseTestMixin, BaseTest):
def _get_cohortpeople(self, cohort: Cohort, *, team_id: Optional[int] = None):
team_id = team_id or cohort.team_id
Expand Down Expand Up @@ -517,7 +532,7 @@ def test_insert_by_distinct_id_or_email(self):
self.assertEqual(results, 3)

#  If we accidentally call calculate_people it shouldn't erase people
cohort.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort, 0)
results = get_person_ids_by_cohort_id(self.team.pk, cohort.id)
self.assertEqual(len(results), 3)

Expand Down Expand Up @@ -556,7 +571,7 @@ def test_cohortpeople_basic(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

results = self._get_cohortpeople(cohort1)
self.assertEqual(len(results), 2)
Expand Down Expand Up @@ -592,13 +607,13 @@ def test_cohortpeople_action_basic(self):
)

cohort1 = Cohort.objects.create(team=self.team, groups=[{"action_id": action.pk, "days": 1}], name="cohort1")
cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

results = self._get_cohortpeople(cohort1)
self.assertEqual(len(results), 2)

cohort2 = Cohort.objects.create(team=self.team, groups=[{"action_id": action.pk, "days": 1}], name="cohort2")
cohort2.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort2, 0)

results = self._get_cohortpeople(cohort2)
self.assertEqual(len(results), 2)
Expand Down Expand Up @@ -684,7 +699,7 @@ def test_cohortpeople_action_count(self):
groups=[{"action_id": action.pk, "days": 3, "count": 2, "count_operator": "gte"}],
name="cohort1",
)
cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

results = self._get_cohortpeople(cohort1)
self.assertEqual(len(results), 2)
Expand All @@ -694,7 +709,7 @@ def test_cohortpeople_action_count(self):
groups=[{"action_id": action.pk, "days": 3, "count": 1, "count_operator": "lte"}],
name="cohort2",
)
cohort2.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort2, 0)

results = self._get_cohortpeople(cohort2)
self.assertEqual(len(results), 1)
Expand All @@ -704,7 +719,7 @@ def test_cohortpeople_action_count(self):
groups=[{"action_id": action.pk, "days": 3, "count": 1, "count_operator": "eq"}],
name="cohort3",
)
cohort3.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort3, 0)

results = self._get_cohortpeople(cohort3)
self.assertEqual(len(results), 1)
Expand Down Expand Up @@ -738,9 +753,9 @@ def test_cohortpeople_deleted_person(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)
p2.delete()
cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

def test_cohortpeople_prop_changed(self):
with freeze_time((datetime.now() - timedelta(days=3)).strftime("%Y-%m-%d")):
Expand Down Expand Up @@ -776,14 +791,14 @@ def test_cohortpeople_prop_changed(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

with freeze_time((datetime.now() - timedelta(days=2)).strftime("%Y-%m-%d")):
p2.version = 1
p2.properties = ({"$some_prop": "another", "$another_prop": "another"},)
p2.properties = {"$some_prop": "another", "$another_prop": "another"}
p2.save()

cohort1.calculate_people_ch(pending_version=1)
calculate_cohort_hogql_test_harness(cohort1, 1)

results = self._get_cohortpeople(cohort1)

Expand Down Expand Up @@ -818,7 +833,7 @@ def test_cohort_change(self):
],
name="cohort1",
)
cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)
results = self._get_cohortpeople(cohort1)

self.assertEqual(len(results), 1)
Expand All @@ -834,7 +849,7 @@ def test_cohort_change(self):
]
cohort1.save()

cohort1.calculate_people_ch(pending_version=1)
calculate_cohort_hogql_test_harness(cohort1, 1)

results = self._get_cohortpeople(cohort1)
self.assertEqual(len(results), 1)
Expand All @@ -851,7 +866,7 @@ def test_static_cohort_precalculated(self):
cohort = Cohort.objects.create(team=self.team, groups=[], is_static=True, last_calculation=timezone.now())
cohort.insert_users_by_list(["1", "123"])

cohort.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort, 0)

with self.settings(USE_PRECALCULATED_CH_COHORT_PEOPLE=True):
sql, _ = format_filter_query(cohort, 0, HogQLContext(team_id=self.team.pk))
Expand All @@ -866,15 +881,15 @@ def test_cohortpeople_with_valid_other_cohort_filter(self):
groups=[{"properties": [{"key": "foo", "value": "bar", "type": "person"}]}],
name="cohort0",
)
cohort0.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort0, 0)

cohort1: Cohort = Cohort.objects.create(
team=self.team,
groups=[{"properties": [{"key": "id", "type": "cohort", "value": cohort0.id}]}],
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

res = self._get_cohortpeople(cohort1)
self.assertEqual(len(res), 1)
Expand Down Expand Up @@ -914,7 +929,7 @@ def test_cohortpeople_with_not_in_cohort_operator(self):
groups=[{"properties": [{"key": "$some_prop", "value": "something1", "type": "person"}]}],
name="cohort0",
)
cohort0.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort0, 0)

cohort1 = Cohort.objects.create(
team=self.team,
Expand Down Expand Up @@ -943,7 +958,7 @@ def test_cohortpeople_with_not_in_cohort_operator(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

with self.settings(USE_PRECALCULATED_CH_COHORT_PEOPLE=True):
filter = Filter(
Expand Down Expand Up @@ -1110,7 +1125,7 @@ def test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts(self):
],
name="cohort0",
)
cohort0.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort0, 0)

cohort1 = Cohort.objects.create(
team=self.team,
Expand Down Expand Up @@ -1139,7 +1154,7 @@ def test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

with self.settings(USE_PRECALCULATED_CH_COHORT_PEOPLE=True):
filter = Filter(
Expand Down Expand Up @@ -1171,7 +1186,7 @@ def test_cohortpeople_with_nonexistent_other_cohort_filter(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

res = self._get_cohortpeople(cohort1)
self.assertEqual(len(res), 0)
Expand All @@ -1183,7 +1198,7 @@ def test_clickhouse_empty_query(self):
name="cohort1",
)

cohort2.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort2, 0)
self.assertFalse(Cohort.objects.get().is_calculating)

def test_query_with_multiple_new_style_cohorts(self):
Expand Down Expand Up @@ -1322,7 +1337,7 @@ def test_query_with_multiple_new_style_cohorts(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

result = self._get_cohortpeople(cohort1)
self.assertCountEqual([p1.uuid, p3.uuid], [r[0] for r in result])
Expand Down Expand Up @@ -1350,23 +1365,23 @@ def test_update_cohort(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(cohort1, 0)

# Should only have p1 in this cohort
results = self._get_cohortpeople(cohort1)
self.assertEqual(len(results), 1)

cohort1.groups = [{"properties": [{"key": "$another_prop", "value": "something", "type": "person"}]}]
cohort1.save()
cohort1.calculate_people_ch(pending_version=1)
calculate_cohort_hogql_test_harness(cohort1, 1)

# Should only have p2, p3 in this cohort
results = self._get_cohortpeople(cohort1)
self.assertEqual(len(results), 2)

cohort1.groups = [{"properties": [{"key": "$some_prop", "value": "something", "type": "person"}]}]
cohort1.save()
cohort1.calculate_people_ch(pending_version=2)
calculate_cohort_hogql_test_harness(cohort1, 2)

# Should only have p1 again in this cohort
results = self._get_cohortpeople(cohort1)
Expand Down Expand Up @@ -1396,10 +1411,10 @@ def test_cohort_versioning(self):
name="cohort1",
)

cohort1.calculate_people_ch(pending_version=5)
version = calculate_cohort_hogql_test_harness(cohort1, 5)

cohort1.pending_version = 5
cohort1.version = 5
cohort1.pending_version = version
cohort1.version = version
cohort1.save()

# Should have p1 in this cohort even if version is different
Expand Down Expand Up @@ -1439,11 +1454,23 @@ def test_calculate_people_ch_in_multiteam_project(self):
name="shared cohort",
)
# Calculate cohort
shared_cohort.calculate_people_ch(pending_version=0)
calculate_cohort_hogql_test_harness(shared_cohort, 0)

# Verify shared_cohort is now calculated for both teams
results_team1 = self._get_cohortpeople(shared_cohort, team_id=self.team.pk)
results_team2 = self._get_cohortpeople(shared_cohort, team_id=team2.pk)

self.assertCountEqual([r[0] for r in results_team1], [person2_team1.uuid])
self.assertCountEqual([r[0] for r in results_team2], [person1_team2.uuid])

def test_increment_cohort(self):
cohort1 = Cohort.objects.create(
team=self.team,
groups=[{"properties": [{"key": "$some_prop", "value": "something", "type": "person"}]}],
name="cohort1",
pending_version=3,
)
new_version = get_and_update_pending_version(cohort1)
assert new_version == 6
new_version = get_and_update_pending_version(cohort1)
assert new_version == 8
66 changes: 66 additions & 0 deletions ee/clickhouse/queries/test/__snapshots__/test_cohort_query.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,28 @@
AND (has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', ''))))) SETTINGS optimize_aggregation_in_order = 1
'''
# ---
# name: TestCohortQuery.test_person_props_only.1
'''

SELECT id
FROM person
WHERE team_id = 99999
AND id IN
(SELECT id
FROM person
WHERE team_id = 99999
AND (((has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', '')))
OR (has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', ''))))
OR ((has(['test3'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', '')))
AND (has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', ''))))) )
GROUP BY id
HAVING max(is_deleted) = 0
AND (((has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', '')))
OR (has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', ''))))
OR ((has(['test3'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', '')))
AND (has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', ''))))) SETTINGS optimize_aggregation_in_order = 1
'''
# ---
# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters
'''

Expand Down Expand Up @@ -580,6 +602,50 @@
OR (has(['test2'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', '')))) SETTINGS optimize_aggregation_in_order = 1
'''
# ---
# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters.3
'''
SELECT persons.id AS id
FROM
(SELECT argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', ''), person.version) AS properties___name,
person.id AS id
FROM person
WHERE and(equals(person.team_id, 99999), in(id,
(SELECT where_optimization.id AS id
FROM person AS where_optimization
WHERE and(equals(where_optimization.team_id, 99999), or(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', ''), 'test'), 0), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', ''), 'test2'), 0))))))
GROUP BY person.id
HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))), 0))) AS persons
WHERE or(ifNull(equals(persons.properties___name, 'test'), 0), ifNull(equals(persons.properties___name, 'test2'), 0))
ORDER BY persons.id ASC
LIMIT 100 SETTINGS optimize_aggregation_in_order=1,
join_algorithm='auto',
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
'''
# ---
# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters.4
'''

SELECT id
FROM person
WHERE team_id = 99999
AND id IN
(SELECT id
FROM person
WHERE team_id = 99999
AND ((((has(['test'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', '')))))
OR (has(['test2'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', '')))) )
GROUP BY id
HAVING max(is_deleted) = 0
AND ((((has(['test'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', '')))))
OR (has(['test2'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', '')))) SETTINGS optimize_aggregation_in_order = 1
'''
# ---
# name: TestCohortQuery.test_static_cohort_filter
'''

Expand Down
Loading
Loading