Skip to content

Commit

Permalink
fix: person querying via TedGPT (#20098)
Browse files Browse the repository at this point in the history
* fix: person querying via TedGPT

* fix query matching replacing

* fix query matching replacing

* fix query matching replacing

* Update query snapshots

* Update query snapshots

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
pauldambra and github-actions[bot] authored Feb 1, 2024
1 parent ca0cf39 commit 53a078e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
person_id
FROM events
WHERE (team_id = 2
AND person_id = '6ba7b810-9dad-11d1-80b4-00c04fd430c8')
AND person_id = '00000000-0000-0000-0000-000000000000')
'''
# ---
# name: TestAsyncDeletion.test_mark_deletions_done_person_when_not_done
Expand All @@ -48,7 +48,7 @@
person_id
FROM events
WHERE (team_id = 2
AND person_id = '6ba7b810-9dad-11d1-80b4-00c04fd430c8')
AND person_id = '00000000-0000-0000-0000-000000000000')
'''
# ---
# name: TestAsyncDeletion.test_mark_deletions_done_team_when_not_done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,13 @@ def _data_to_return(self, results: List[Any]) -> List[Dict[str, Any]]:
pass

_raw_persons_query = """
SELECT distinct_id, argMax(person_id, version) as person_id
SELECT distinct_id, argMax(person_id, version) as current_person_id
{select_person_props}
FROM person_distinct_id2 as pdi
{filter_persons_clause}
WHERE team_id = %(team_id)s
{prop_filter_clause}
{prop_filter_clause}
{all_distinct_ids_that_might_match_a_person}
GROUP BY distinct_id
HAVING
argMax(is_deleted, version) = 0
Expand All @@ -195,7 +196,7 @@ def get_query(self) -> Tuple[str, Dict[str, Any]]:
g for g in self._filter.property_groups.flat if g.type == "hogql" and "person.properties" in g.key
],
),
person_id_joined_alias=f"{self.DISTINCT_ID_TABLE_ALIAS}.person_id",
person_id_joined_alias=f"{self.DISTINCT_ID_TABLE_ALIAS}.current_person_id",
)

person_query, person_query_params = self._get_person_query()
Expand All @@ -205,7 +206,21 @@ def get_query(self) -> Tuple[str, Dict[str, Any]]:
return "", {}
else:
filter_persons_clause = person_query or ""
filter_by_person_uuid_condition = "and person_id = %(person_uuid)s" if self._filter.person_uuid else ""
filter_by_person_uuid_condition = (
"and current_person_id = %(person_uuid)s" if self._filter.person_uuid else ""
)
all_distinct_ids_that_might_match_a_person = (
"""
AND distinct_id IN (
SELECT distinct_id
FROM person_distinct_id2
WHERE team_id = %(team_id)s
AND person_id = %(person_uuid)s) as all_distinct_ids_that_might_match_a_person
"""
if self._filter.person_uuid
else ""
)

return self._raw_persons_query.format(
filter_persons_clause=filter_persons_clause,
select_person_props=", argMax(person_props, version) as person_props"
Expand All @@ -214,6 +229,7 @@ def get_query(self) -> Tuple[str, Dict[str, Any]]:
prop_filter_clause=prop_query,
prop_having_clause=having_prop_query,
filter_by_person_uuid_condition=filter_by_person_uuid_condition,
all_distinct_ids_that_might_match_a_person=all_distinct_ids_that_might_match_a_person,
), {
"team_id": self._team_id,
**person_query_params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,17 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
WHERE team_id = 2
AND distinct_id IN
(SELECT distinct_id
FROM person_distinct_id2
WHERE team_id = 2
AND person_id = '00000000-0000-0000-0000-000000000000') as all_distinct_ids_that_might_match_a_person
GROUP BY distinct_id
HAVING argMax(is_deleted, version) = 0
and person_id = '00000000-0000-0000-0000-000000000000') as session_persons_sub_query)
AND current_person_id = '00000000-0000-0000-0000-000000000000') as session_persons_sub_query)
AND s.session_id in
(select `$session_id` as session_id
from
Expand All @@ -240,12 +245,17 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
WHERE team_id = 2
AND distinct_id IN
(SELECT distinct_id
FROM person_distinct_id2
WHERE team_id = 2
AND person_id = '00000000-0000-0000-0000-000000000000') as all_distinct_ids_that_might_match_a_person
GROUP BY distinct_id
HAVING argMax(is_deleted, version) = 0
and person_id = '00000000-0000-0000-0000-000000000000') as events_persons_sub_query)
AND current_person_id = '00000000-0000-0000-0000-000000000000') as events_persons_sub_query)
GROUP BY `$session_id`
HAVING 1=1
AND hasAll(event_names, ['$pageview', 'custom-event'])) as session_events_sub_query)
Expand Down Expand Up @@ -1386,7 +1396,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand Down Expand Up @@ -1416,7 +1426,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand Down Expand Up @@ -1467,7 +1477,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand Down Expand Up @@ -1496,7 +1506,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand Down Expand Up @@ -1591,7 +1601,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id,
Expand Down Expand Up @@ -1620,7 +1630,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id,
Expand Down Expand Up @@ -1670,7 +1680,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id,
Expand Down Expand Up @@ -1698,7 +1708,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id,
Expand Down Expand Up @@ -1747,7 +1757,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand All @@ -1768,7 +1778,7 @@
FROM events e
JOIN
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand Down Expand Up @@ -1825,7 +1835,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand All @@ -1846,7 +1856,7 @@
FROM events e
JOIN
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand Down Expand Up @@ -2177,7 +2187,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -2412,7 +2422,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -2447,7 +2457,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -2546,7 +2556,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -2581,7 +2591,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -3327,7 +3337,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -3398,7 +3408,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -3431,7 +3441,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -3485,7 +3495,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -3518,7 +3528,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -3664,12 +3674,17 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
WHERE team_id = 2
AND distinct_id IN
(SELECT distinct_id
FROM person_distinct_id2
WHERE team_id = 2
AND person_id = '00000000-0000-0000-0000-000000000000') as all_distinct_ids_that_might_match_a_person
GROUP BY distinct_id
HAVING argMax(is_deleted, version) = 0
and person_id = '00000000-0000-0000-0000-000000000000') as session_persons_sub_query)
AND current_person_id = '00000000-0000-0000-0000-000000000000') as session_persons_sub_query)
GROUP BY session_id
HAVING 1=1
ORDER BY start_time DESC
Expand Down Expand Up @@ -4277,7 +4292,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id ,
argMax(person_id, version) as current_person_id ,
argMax(person_props, version) as person_props
FROM person_distinct_id2 as pdi
INNER JOIN
Expand Down Expand Up @@ -4370,7 +4385,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id,
Expand Down Expand Up @@ -4462,7 +4477,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down Expand Up @@ -4558,7 +4573,7 @@
(select distinct_id
from
(SELECT distinct_id,
argMax(person_id, version) as person_id
argMax(person_id, version) as current_person_id
FROM person_distinct_id2 as pdi
INNER JOIN
(SELECT id
Expand Down
10 changes: 9 additions & 1 deletion posthog/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,16 @@ def assertQueryMatchesSnapshot(self, query, params=None, replace_all_numbers=Fal
# Replace person id (when querying session recording replay events)
query = re.sub(
"and person_id = '[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}'",
r"and person_id = '00000000-0000-0000-0000-000000000000'",
r"AND person_id = '00000000-0000-0000-0000-000000000000'",
query,
flags=re.IGNORECASE,
)

query = re.sub(
"and current_person_id = '[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}'",
r"AND current_person_id = '00000000-0000-0000-0000-000000000000'",
query,
flags=re.IGNORECASE,
)

# Replace tag id lookups for postgres
Expand Down

0 comments on commit 53a078e

Please sign in to comment.