-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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(hogql): cohort filter #14600
Merged
Merged
feat(hogql): cohort filter #14600
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
058d3ce
feat(hogql): cohort filter
mariusandra 3b8a337
tidy up
mariusandra 7246673
add test for making the filter
mariusandra 153777e
cohort filter tests
mariusandra 7e20c07
Update query snapshots
github-actions[bot] 88cf8e1
Update query snapshots
github-actions[bot] 01c7863
recalculate people for test
mariusandra e8cf870
Merge remote-tracking branch 'origin/hogql-cohort-filters' into hogql…
mariusandra 17c6194
add person join
mariusandra 1f80499
Merge branch 'master' into hogql-cohort-filters
mariusandra 94074d7
add todo
mariusandra ceae5f7
use our own code
mariusandra 638f8f1
fix tests
mariusandra 41db09d
Update query snapshots
github-actions[bot] 581e234
Update query snapshots
github-actions[bot] 259fa21
Merge branch 'master' into hogql-cohort-filters
mariusandra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ | |
|
||
from posthog import datetime | ||
from posthog.hogql import ast | ||
from posthog.hogql.property import property_to_expr | ||
from posthog.hogql.query import execute_hogql_query | ||
from posthog.models import Cohort | ||
from posthog.models.cohort.util import recalculate_cohortpeople | ||
from posthog.models.utils import UUIDT | ||
from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, _create_person, flush_persons_and_events | ||
|
||
|
@@ -411,3 +414,63 @@ def test_select_person_on_events(self): | |
"SELECT poe.properties.email, count() FROM events AS s GROUP BY poe.properties.email LIMIT 10", | ||
) | ||
self.assertEqual(response.results[0][0], "[email protected]") | ||
|
||
def test_prop_cohort_basic(self): | ||
with freeze_time("2020-01-10"): | ||
_create_person(distinct_ids=["some_other_id"], team_id=self.team.pk, properties={"$some_prop": "something"}) | ||
_create_person( | ||
distinct_ids=["some_id"], | ||
team_id=self.team.pk, | ||
properties={"$some_prop": "something", "$another_prop": "something"}, | ||
) | ||
_create_person(distinct_ids=["no_match"], team_id=self.team.pk) | ||
_create_event(event="$pageview", team=self.team, distinct_id="some_id", properties={"attr": "some_val"}) | ||
_create_event( | ||
event="$pageview", team=self.team, distinct_id="some_other_id", properties={"attr": "some_val"} | ||
) | ||
cohort = Cohort.objects.create( | ||
team=self.team, | ||
groups=[{"properties": [{"key": "$some_prop", "value": "something", "type": "person"}]}], | ||
name="cohort", | ||
) | ||
recalculate_cohortpeople(cohort, pending_version=0) | ||
response = execute_hogql_query( | ||
"SELECT event, count() FROM events WHERE {cohort_filter} GROUP BY event", | ||
team=self.team, | ||
placeholders={ | ||
"cohort_filter": property_to_expr({"type": "cohort", "key": "id", "value": cohort.pk}, self.team) | ||
}, | ||
) | ||
self.assertEqual(response.results, [("$pageview", 2)]) | ||
self.assertEqual( | ||
response.clickhouse, | ||
f"SELECT event, count(*) FROM events INNER JOIN (SELECT argMax(person_distinct_id2.person_id, version) AS person_id, distinct_id FROM person_distinct_id2 WHERE equals(team_id, {self.team.pk}) GROUP BY distinct_id HAVING equals(argMax(is_deleted, version), 0)) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) WHERE and(equals(team_id, {self.team.pk}), in(events__pdi.person_id, (SELECT person_id FROM cohortpeople WHERE and(equals(team_id, {self.team.pk}), equals(cohort_id, {cohort.pk})) GROUP BY person_id, cohort_id, version HAVING greater(sum(sign), 0)))) GROUP BY event LIMIT 100", | ||
) | ||
|
||
def test_prop_cohort_static(self): | ||
with freeze_time("2020-01-10"): | ||
_create_person(distinct_ids=["some_other_id"], team_id=self.team.pk, properties={"$some_prop": "something"}) | ||
_create_person( | ||
distinct_ids=["some_id"], | ||
team_id=self.team.pk, | ||
properties={"$some_prop": "something", "$another_prop": "something"}, | ||
) | ||
_create_person(distinct_ids=["no_match"], team_id=self.team.pk) | ||
_create_event(event="$pageview", team=self.team, distinct_id="some_id", properties={"attr": "some_val"}) | ||
_create_event( | ||
event="$pageview", team=self.team, distinct_id="some_other_id", properties={"attr": "some_val"} | ||
) | ||
cohort = Cohort.objects.create(team=self.team, groups=[], is_static=True) | ||
cohort.insert_users_by_list(["some_id"]) | ||
response = execute_hogql_query( | ||
"SELECT event, count() FROM events WHERE {cohort_filter} GROUP BY event", | ||
team=self.team, | ||
placeholders={ | ||
"cohort_filter": property_to_expr({"type": "cohort", "key": "id", "value": cohort.pk}, self.team) | ||
}, | ||
) | ||
self.assertEqual(response.results, [("$pageview", 1)]) | ||
self.assertEqual( | ||
response.clickhouse, | ||
f"SELECT event, count(*) FROM events INNER JOIN (SELECT argMax(person_distinct_id2.person_id, version) AS person_id, distinct_id FROM person_distinct_id2 WHERE equals(team_id, {self.team.pk}) GROUP BY distinct_id HAVING equals(argMax(is_deleted, version), 0)) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) WHERE and(equals(team_id, {self.team.pk}), in(events__pdi.person_id, (SELECT person_id FROM person_static_cohort WHERE and(equals(team_id, {self.team.pk}), equals(cohort_id, {cohort.pk}))))) GROUP BY event LIMIT 100", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tangential to this PR but I wonder what stops these definitions and the actual table definitions from diverging other than people being careful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People are always careful :).
Good question. Noting really. I added
"Lock down" tables for HogQL. Make sure we don't change something users might query on. Expose only what we deem relevant.
as a point to the list of all lists.