Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(persons): search properties.Email only when the project defines it (#121959)

Vasco de Krijgere7e8b33ed793
diff --git a/posthog/api/person.py b/posthog/api/person.py
index 7a9e39ac2e4e..f7ca7ac49bdb 100644
--- a/posthog/api/person.py
+++ b/posthog/api/person.py
@@ -50,6 +50,7 @@ from posthog.errors import ExposedCHQueryError, QueryErrorCategory, classify_que
from posthog.event_usage import get_request_analytics_properties
from posthog.exceptions_capture import capture_exception
from posthog.helpers.impersonation import is_impersonated
+from posthog.hogql_queries.actor_strategies import PERSON_EMAIL_PROPERTY_KEYS, person_email_property_keys
from posthog.hogql_queries.properties_timeline import PropertiesTimeline
from posthog.hogql_queries.serialized_actors import SerializedPerson, get_serialized_people
from posthog.metrics import LABEL_TEAM_ID
@@ -638,9 +639,6 @@ _GET_OBJECT_DISTINCT_ID_LIMITS: dict[str, int] = {
# hide the other matches.
_COMPLETE_EMAIL_TERM = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
-# Keep in sync with the email arms of `PersonStrategy.filter_conditions`.
-_EMAIL_PROPERTY_KEYS = ("email", "Email")
-
def _is_canonical_uuid(value: str) -> bool:
# Only the dashed form counts, so the fast path answers the same terms the fuzzy search
@@ -683,7 +681,7 @@ def _search_match_fields(search: str, person: SerializedPerson, known_fields: Co
(PersonSearchMatchField.DISTINCT_ID, any(needle in did.lower() for did in person["distinct_ids"])),
(
PersonSearchMatchField.EMAIL,
- any(needle in str(properties.get(key) or "").lower() for key in _EMAIL_PROPERTY_KEYS),
+ any(needle in str(properties.get(key) or "").lower() for key in PERSON_EMAIL_PROPERTY_KEYS),
),
(PersonSearchMatchField.NAME, needle in str(properties.get("name") or "").lower()),
(PersonSearchMatchField.ID, needle in str(person["id"]).lower()),
@@ -929,7 +927,7 @@ class PersonViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
{"type": "hogql", "key": identifier_hit},
*(
{"type": "person", "key": key, "value": search, "operator": "icontains"}
- for key in (*_EMAIL_PROPERTY_KEYS, "name")
+ for key in (*person_email_property_keys(team), "name")
),
],
}
diff --git a/posthog/api/test/__snapshots__/test_person.ambr b/posthog/api/test/__snapshots__/test_person.ambr
index a57195071a1f..5580148dd6b7 100644
--- a/posthog/api/test/__snapshots__/test_person.ambr
+++ b/posthog/api/test/__snapshots__/test_person.ambr
@@ -275,7 +275,6 @@
SELECT persons.id AS id
FROM
(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email,
- tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'Email'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___Email,
tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___name,
argMax(toTimeZone(person.created_at, 'UTC'), person.version) AS created_at,
person.id AS id
@@ -283,26 +282,26 @@
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(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'Email'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ilike(toString(where_optimization.id), '%another@gm%'), in(where_optimization.id,
- (SELECT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE ilike(person_distinct_ids.distinct_id, '%another@gm%'))))))))
+ WHERE and(equals(where_optimization.team_id, 99999), or(ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ilike(toString(where_optimization.id), '%another@gm%'), in(where_optimization.id,
+ (SELECT person_distinct_ids.person_id AS person_id
+ FROM
+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ WHERE ilike(person_distinct_ids.distinct_id, '%another@gm%'))))))))
GROUP BY person.id
HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))) AS persons
- WHERE or(ifNull(ilike(toString(persons.properties___email), '%another@gm%'), 0), ifNull(ilike(toString(persons.properties___Email), '%another@gm%'), 0), ifNull(ilike(toString(persons.properties___name), '%another@gm%'), 0), ilike(toString(persons.id), '%another@gm%'), in(persons.id,
- (SELECT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE ilike(person_distinct_ids.distinct_id, '%another@gm%'))))
+ WHERE or(ifNull(ilike(toString(persons.properties___email), '%another@gm%'), 0), ifNull(ilike(toString(persons.properties___name), '%another@gm%'), 0), ilike(toString(persons.id), '%another@gm%'), in(persons.id,
+ (SELECT person_distinct_ids.person_id AS person_id
+ FROM
+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ WHERE ilike(person_distinct_ids.distinct_id, '%another@gm%'))))
ORDER BY persons.created_at DESC,
persons.id DESC
LIMIT 101
@@ -328,7 +327,6 @@
SELECT persons.id AS id
FROM
(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email,
- tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'Email'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___Email,
tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___name,
argMax(toTimeZone(person.created_at, 'UTC'), person.version) AS created_at,
person.id AS id
@@ -336,26 +334,26 @@
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(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'Email'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ilike(toString(where_optimization.id), '%distinct_id_3%'), in(where_optimization.id,
- (SELECT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE ilike(person_distinct_ids.distinct_id, '%distinct_id_3%'))))))))
+ WHERE and(equals(where_optimization.team_id, 99999), or(ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ilike(toString(where_optimization.id), '%distinct_id_3%'), in(where_optimization.id,
+ (SELECT person_distinct_ids.person_id AS person_id
+ FROM
+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ WHERE ilike(person_distinct_ids.distinct_id, '%distinct_id_3%'))))))))
GROUP BY person.id
HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))) AS persons
- WHERE or(ifNull(ilike(toString(persons.properties___email), '%distinct_id_3%'), 0), ifNull(ilike(toString(persons.properties___Email), '%distinct_id_3%'), 0), ifNull(ilike(toString(persons.properties___name), '%distinct_id_3%'), 0), ilike(toString(persons.id), '%distinct_id_3%'), in(persons.id,
- (SELECT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE ilike(person_distinct_ids.distinct_id, '%distinct_id_3%'))))
+ WHERE or(ifNull(ilike(toString(persons.properties___email), '%distinct_id_3%'), 0), ifNull(ilike(toString(persons.properties___name), '%distinct_id_3%'), 0), ilike(toString(persons.id), '%distinct_id_3%'), in(persons.id,
+ (SELECT person_distinct_ids.person_id AS person_id
+ FROM
+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ WHERE ilike(person_distinct_ids.distinct_id, '%distinct_id_3%'))))
ORDER BY persons.created_at DESC,
persons.id DESC
LIMIT 101
@@ -651,7 +649,6 @@
SELECT persons.id AS id
FROM
(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email,
- tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'Email'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___Email,
tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___name,
argMax(toTimeZone(person.created_at, 'UTC'), person.version) AS created_at,
person.id AS id
@@ -659,26 +656,26 @@
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(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'Email'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ilike(toString(where_optimization.id), '%another@gm%'), in(where_optimization.id,
- (SELECT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE ilike(person_distinct_ids.distinct_id, '%another@gm%'))))))))
+ WHERE and(equals(where_optimization.team_id, 99999), or(ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', '')), '%another@gm%'), 0), ilike(toString(where_optimization.id), '%another@gm%'), in(where_optimization.id,
+ (SELECT person_distinct_ids.person_id AS person_id
+ FROM
+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ WHERE ilike(person_distinct_ids.distinct_id, '%another@gm%'))))))))
GROUP BY person.id
HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))) AS persons
- WHERE or(ifNull(ilike(toString(persons.properties___email), '%another@gm%'), 0), ifNull(ilike(toString(persons.properties___Email), '%another@gm%'), 0), ifNull(ilike(toString(persons.properties___name), '%another@gm%'), 0), ilike(toString(persons.id), '%another@gm%'), in(persons.id,
- (SELECT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE ilike(person_distinct_ids.distinct_id, '%another@gm%'))))
+ WHERE or(ifNull(ilike(toString(persons.properties___email), '%another@gm%'), 0), ifNull(ilike(toString(persons.properties___name), '%another@gm%'), 0), ilike(toString(persons.id), '%another@gm%'), in(persons.id,
+ (SELECT person_distinct_ids.person_id AS person_id
+ FROM
+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ WHERE ilike(person_distinct_ids.distinct_id, '%another@gm%'))))
ORDER BY persons.created_at DESC,
persons.id DESC
LIMIT 101
@@ -704,7 +701,6 @@
SELECT persons.id AS id
FROM
(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email,
- tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'Email'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___Email,
tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___name,
argMax(toTimeZone(person.created_at, 'UTC'), person.version) AS created_at,
person.id AS id
@@ -712,26 +708,26 @@
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(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'Email'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ilike(toString(where_optimization.id), '%distinct_id_3%'), in(where_optimization.id,
- (SELECT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE ilike(person_distinct_ids.distinct_id, '%distinct_id_3%'))))))))
+ WHERE and(equals(where_optimization.team_id, 99999), or(ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ifNull(ilike(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', '')), '%distinct_id_3%'), 0), ilike(toString(where_optimization.id), '%distinct_id_3%'), in(where_optimization.id,
+ (SELECT person_distinct_ids.person_id AS person_id
+ FROM
+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ WHERE ilike(person_distinct_ids.distinct_id, '%distinct_id_3%'))))))))
GROUP BY person.id
HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))) AS persons
- WHERE or(ifNull(ilike(toString(persons.properties___email), '%distinct_id_3%'), 0), ifNull(ilike(toString(persons.properties___Email), '%distinct_id_3%'), 0), ifNull(ilike(toString(persons.properties___name), '%distinct_id_3%'), 0), ilike(toString(persons.id), '%distinct_id_3%'), in(persons.id,
- (SELECT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE ilike(person_distinct_ids.distinct_id, '%distinct_id_3%'))))
+ WHERE or(ifNull(ilike(toString(persons.properties___email), '%distinct_id_3%'), 0), ifNull(ilike(toString(persons.properties___name), '%distinct_id_3%'), 0), ilike(toString(persons.id), '%distinct_id_3%'), in(persons.id,
+ (SELECT person_distinct_ids.person_id AS person_id
+ FROM
+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ WHERE ilike(person_distinct_ids.distinct_id, '%distinct_id_3%'))))
ORDER BY persons.created_at DESC,
persons.id DESC
LIMIT 101
diff --git a/posthog/api/test/test_person.py b/posthog/api/test/test_person.py
index ffe82ca2c92b..96b6941202bd 100644
--- a/posthog/api/test/test_person.py
+++ b/posthog/api/test/test_person.py
@@ -319,6 +319,7 @@ class TestPerson(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
created_at=timezone.now() - timedelta(days=1),
immediate=True,
)
+ PropertyDefinition.objects.create(team=self.team, name="Email", type=PropertyDefinition.Type.PERSON)
by_name = _create_person(
team=self.team,
distinct_ids=["0198f3c1-6c2a-7a5b-9d41-9a1b2c3d4e61"],
diff --git a/posthog/hogql_queries/actor_strategies.py b/posthog/hogql_queries/actor_strategies.py
index 972ea90b37fb..85b1550a8804 100644
--- a/posthog/hogql_queries/actor_strategies.py
+++ b/posthog/hogql_queries/actor_strategies.py
@@ -14,14 +14,33 @@ from posthog.hogql.query import execute_hogql_query
from posthog.hogql_queries.paginators import HogQLHasMorePaginator
from posthog.hogql_queries.utils.recordings_helper import RecordingsHelper
-from posthog.models import Team
+from posthog.models import PropertyDefinition, Team
from posthog.models.person.util import _batched_get_distinct_ids_for_persons, _batched_get_persons_by_uuids
from posthog.models.user import User
from posthog.personhog_client.caller_tag import personhog_caller_tag
from posthog.utils import is_anonymous_id
+from products.event_definitions.backend.models.property_definition import effective_project_id_expr
+
logger = structlog.get_logger(__name__)
+PERSON_EMAIL_PROPERTY_KEY = "email"
+PERSON_TITLE_CASED_EMAIL_PROPERTY_KEY = "Email"
+PERSON_EMAIL_PROPERTY_KEYS = (PERSON_EMAIL_PROPERTY_KEY, PERSON_TITLE_CASED_EMAIL_PROPERTY_KEY)
+
+
+def person_email_property_keys(team: Team) -> tuple[str, ...]:
+ has_title_cased_email = (
+ PropertyDefinition.objects.alias(effective_project_id=effective_project_id_expr())
+ .filter(
+ effective_project_id=team.project_id,
+ type=PropertyDefinition.Type.PERSON,
+ name=PERSON_TITLE_CASED_EMAIL_PROPERTY_KEY,
+ )
+ .exists()
+ )
+ return PERSON_EMAIL_PROPERTY_KEYS if has_title_cased_email else (PERSON_EMAIL_PROPERTY_KEY,)
+
class ActorStrategy:
field: str
@@ -146,23 +165,17 @@ class PersonStrategy(ActorStrategy):
search = self.query.search.strip() if self.query.search else None
if search:
+ searched_properties = [*person_email_property_keys(self.team), "name"]
where_exprs.append(
ast.Or(
exprs=[
- ast.CompareOperation(
- op=ast.CompareOperationOp.ILike,
- left=ast.Call(name="toString", args=[ast.Field(chain=["properties", "email"])]),
- right=ast.Constant(value=f"%{search}%"),
- ),
- ast.CompareOperation(
- op=ast.CompareOperationOp.ILike,
- left=ast.Call(name="toString", args=[ast.Field(chain=["properties", "Email"])]),
- right=ast.Constant(value=f"%{search}%"),
- ),
- ast.CompareOperation(
- op=ast.CompareOperationOp.ILike,
- left=ast.Call(name="toString", args=[ast.Field(chain=["properties", "name"])]),
- right=ast.Constant(value=f"%{search}%"),
+ *(
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.ILike,
+ left=ast.Call(name="toString", args=[ast.Field(chain=["properties", name])]),
+ right=ast.Constant(value=f"%{search}%"),
+ )
+ for name in searched_properties
),
ast.CompareOperation(
op=ast.CompareOperationOp.ILike,
diff --git a/posthog/hogql_queries/test/__snapshots__/test_actors_query_runner.ambr b/posthog/hogql_queries/test/__snapshots__/test_actors_query_runner.ambr
index 0e225938ac3d..b654eb22b1c0 100644
--- a/posthog/hogql_queries/test/__snapshots__/test_actors_query_runner.ambr
+++ b/posthog/hogql_queries/test/__snapshots__/test_actors_query_runner.ambr
@@ -2,6 +2,19 @@
# name: TestActorsQueryRunner.test_persons_query_search_snapshot
'''
+ SELECT id, id, 1
+ FROM persons
+ WHERE or(ilike(toString(properties.email), '%SEARCHSTRING%'), ilike(toString(properties.name), '%SEARCHSTRING%'), ilike(toString(id), '%SEARCHSTRING%'), in(id, (
+ SELECT person_id
+ FROM person_distinct_ids
+ WHERE ilike(distinct_id, '%SEARCHSTRING%'))))
+ ORDER BY id ASC
+ LIMIT 50000
+ '''
+# ---
+# name: TestActorsQueryRunner.test_persons_query_search_snapshot_with_title_cased_email
+ '''
+
SELECT id, id, 1
FROM persons
WHERE or(ilike(toString(properties.email), '%SEARCHSTRING%'), ilike(toString(properties.Email), '%SEARCHSTRING%'), ilike(toString(properties.name), '%SEARCHSTRING%'), ilike(toString(id), '%SEARCHSTRING%'), in(id, (
diff --git a/posthog/hogql_queries/test/test_actors_query_runner.py b/posthog/hogql_queries/test/test_actors_query_runner.py
index c39c2ce4cb8e..e0c94f88b12b 100644
--- a/posthog/hogql_queries/test/test_actors_query_runner.py
+++ b/posthog/hogql_queries/test/test_actors_query_runner.py
@@ -201,19 +201,22 @@ class TestActorsQueryRunner(ClickhouseTestMixin, APIBaseTest):
runner = self._create_runner(ActorsQuery(filterTestAccounts=filter_test_accounts))
self.assertEqual(len(runner.calculate().results), expected_count)
- def test_persons_query_search_email(self):
+ @parameterized.expand([("email_defined", True, 1), ("email_not_defined", False, 0)])
+ def test_persons_query_search_email(self, _name: str, title_cased_email_defined: bool, capitalized_matches: int):
self.random_uuid = self._create_random_persons()
self._create_random_persons()
capitalized_email = f"capitalized-{self.random_uuid}@example.com"
_create_person(team=self.team, distinct_ids=["capitalized"], properties={"Email": capitalized_email})
flush_persons_and_events()
+ if title_cased_email_defined:
+ PropertyDefinition.objects.create(team=self.team, name="Email", type=PropertyDefinition.Type.PERSON)
runner = self._create_runner(ActorsQuery(search=f"jacob4@{self.random_uuid}.posthog"))
self.assertEqual(len(runner.calculate().results), 1)
runner = self._create_runner(ActorsQuery(search=f"JACOB4@{self.random_uuid}.posthog"))
self.assertEqual(len(runner.calculate().results), 1)
runner = self._create_runner(ActorsQuery(search=f"capitalized-{self.random_uuid}@example"))
- self.assertEqual(len(runner.calculate().results), 1)
+ self.assertEqual(len(runner.calculate().results), capitalized_matches)
def test_persons_query_search_name(self):
self.random_uuid = self._create_random_persons()
@@ -241,6 +244,11 @@ class TestActorsQueryRunner(ClickhouseTestMixin, APIBaseTest):
runner = self._create_runner(ActorsQuery(search="SEARCHSTRING"))
assert pretty_print_in_tests(runner.to_hogql(), self.team.pk) == self.snapshot
+ def test_persons_query_search_snapshot_with_title_cased_email(self):
+ PropertyDefinition.objects.create(team=self.team, name="Email", type=PropertyDefinition.Type.PERSON)
+ runner = self._create_runner(ActorsQuery(search="SEARCHSTRING"))
+ assert pretty_print_in_tests(runner.to_hogql(), self.team.pk) == self.snapshot
+
def test_persons_query_aggregation_select_having(self):
self.random_uuid = self._create_random_persons()
runner = self._create_runner(ActorsQuery(select=["properties.name", "count()"]))