Skip to content

Commit

Permalink
fix(persons): limit to maximum of 2500 distinct_ids for cross-db join
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsobrmlr committed Nov 6, 2023
1 parent 11b3745 commit 561f090
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion posthog/hogql_queries/events_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"created_at",
]

MAX_LIMIT_DISTINCT_IDS = 2500


class EventsQueryRunner(QueryRunner):
query: EventsQuery
Expand Down Expand Up @@ -116,7 +118,7 @@ def to_query(self) -> ast.SelectQuery:
if self.query.personId:
with self.timings.measure("person_id"):
person: Optional[Person] = get_pk_or_uuid(
Person.objects.filter(team=self.team), self.query.personId
Person.objects.filter(team=self.team)[:MAX_LIMIT_DISTINCT_IDS], self.query.personId
).first()
distinct_ids = person.distinct_ids if person is not None else []
ids_list = list(map(str, distinct_ids))
Expand Down
3 changes: 2 additions & 1 deletion posthog/models/event/query_event_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from posthog.clickhouse.client.connection import Workload
from posthog.hogql.constants import DEFAULT_RETURNED_ROWS
from posthog.hogql.context import HogQLContext
from posthog.hogql_queries.events_query_runner import MAX_LIMIT_DISTINCT_IDS
from posthog.models import Action, Filter, Person, Team
from posthog.models.action.util import format_action_filter
from posthog.models.event.sql import (
Expand Down Expand Up @@ -44,7 +45,7 @@ def determine_event_conditions(
params.update({"before": timestamp})
elif k == "person_id":
result += """AND distinct_id IN (%(distinct_ids)s) """
person = get_pk_or_uuid(Person.objects.filter(team=team), v).first()
person = get_pk_or_uuid(Person.objects.filter(team=team)[:MAX_LIMIT_DISTINCT_IDS], v).first()
distinct_ids = person.distinct_ids if person is not None else []
params.update({"distinct_ids": list(map(str, distinct_ids))})
elif k == "distinct_id":
Expand Down

0 comments on commit 561f090

Please sign in to comment.