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(hogql): speed up person property filters with a substring pre-check (#101799)

Harley Alexander0c069b6cca97
diff --git a/ee/clickhouse/models/test/__snapshots__/test_cohort.ambr b/ee/clickhouse/models/test/__snapshots__/test_cohort.ambr
index 400e47b9b4b4..0c42de6e0be9 100644
--- a/ee/clickhouse/models/test/__snapshots__/test_cohort.ambr
+++ b/ee/clickhouse/models/test/__snapshots__/test_cohort.ambr
@@ -17,7 +17,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['something']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0))))))
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 ifNull(equals(persons.`properties___$some_prop`, 'something'), 0)
@@ -32,7 +32,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$another_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['something']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$another_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0))))))
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 ifNull(equals(persons.`properties___$another_prop`, 'something'), 0)
@@ -61,7 +61,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$another_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['something']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0)), and(multiSearchAny(where_optimization.properties, ['something']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$another_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0))))))
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 and(ifNull(equals(persons.`properties___$some_prop`, 'something'), 0), ifNull(equals(persons.`properties___$another_prop`, 'something'), 0))
@@ -89,7 +89,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something1'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['something1']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something1'), 0))))))
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 ifNull(equals(persons.`properties___$some_prop`, 'something1'), 0)
@@ -117,7 +117,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something1'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['something1']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something1'), 0))))))
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 ifNull(equals(persons.`properties___$some_prop`, 'something1'), 0)
diff --git a/posthog/api/test/__snapshots__/test_person.ambr b/posthog/api/test/__snapshots__/test_person.ambr
index 8386801f0835..5580148dd6b7 100644
--- a/posthog/api/test/__snapshots__/test_person.ambr
+++ b/posthog/api/test/__snapshots__/test_person.ambr
@@ -11,7 +11,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['[email protected]']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0))))))
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 ifNull(equals(persons.properties___email, '[email protected]'), 0)
@@ -46,7 +46,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['[email protected]']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0))))))
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 ifNull(equals(persons.properties___email, '[email protected]'), 0)
@@ -81,7 +81,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), 'inexistent'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['inexistent']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), 'inexistent'), 0))))))
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 ifNull(equals(persons.properties___email, 'inexistent'), 0)
@@ -116,7 +116,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'some_prop'), ''), 'null'), '^"|"$', ''), 'some_value'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['some_value']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'some_prop'), ''), 'null'), '^"|"$', ''), 'some_value'), 0))))))
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 ifNull(equals(persons.properties___some_prop, 'some_value'), 0)
@@ -385,7 +385,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['[email protected]']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0))))))
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 ifNull(equals(persons.properties___email, '[email protected]'), 0)
@@ -420,7 +420,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['[email protected]']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0))))))
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 ifNull(equals(persons.properties___email, '[email protected]'), 0)
@@ -455,7 +455,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), 'inexistent'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['inexistent']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), 'inexistent'), 0))))))
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 ifNull(equals(persons.properties___email, 'inexistent'), 0)
@@ -490,7 +490,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'some_prop'), ''), 'null'), '^"|"$', ''), 'some_value'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['some_value']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'some_prop'), ''), 'null'), '^"|"$', ''), 'some_value'), 0))))))
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 ifNull(equals(persons.properties___some_prop, 'some_value'), 0)
diff --git a/posthog/hogql/database/schema/test/__snapshots__/test_persons.ambr b/posthog/hogql/database/schema/test/__snapshots__/test_persons.ambr
index b0ace07b0efa..0303215ee682 100644
--- a/posthog/hogql/database/schema/test/__snapshots__/test_persons.ambr
+++ b/posthog/hogql/database/schema/test/__snapshots__/test_persons.ambr
@@ -168,7 +168,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['something']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0))))))
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 ifNull(equals(persons.`properties___$some_prop`, 'something'), 0)
@@ -199,7 +199,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['something']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, '$some_prop'), ''), 'null'), '^"|"$', ''), 'something'), 0))))))
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 ifNull(equals(persons.`properties___$some_prop`, 'something'), 0)
diff --git a/posthog/hogql/database/schema/test/__snapshots__/test_session_replay_events.ambr b/posthog/hogql/database/schema/test/__snapshots__/test_session_replay_events.ambr
index b3d796df1137..41a4b46305b9 100644
--- a/posthog/hogql/database/schema/test/__snapshots__/test_session_replay_events.ambr
+++ b/posthog/hogql/database/schema/test/__snapshots__/test_session_replay_events.ambr
@@ -335,7 +335,7 @@
FROM person
WHERE equals(person.team_id, 99999)
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)))))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'person_property'), ''), 'null'), '^"|"$', ''), 'true'), 0)) SETTINGS optimize_aggregation_in_order=1) AS raw_session_replay_events__pdi__person ON equals(raw_session_replay_events__pdi.raw_session_replay_events__pdi___person_id, raw_session_replay_events__pdi__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)))))), and(multiSearchAny(person.properties, ['true']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'person_property'), ''), 'null'), '^"|"$', ''), 'true'), 0))) SETTINGS optimize_aggregation_in_order=1) AS raw_session_replay_events__pdi__person ON equals(raw_session_replay_events__pdi.raw_session_replay_events__pdi___person_id, raw_session_replay_events__pdi__person.id)
WHERE and(equals(session_replay_events.team_id, 99999), ifNull(equals(raw_session_replay_events__pdi__person.properties___person_property, 'true'), 0))
GROUP BY session_replay_events.session_id
ORDER BY session_replay_events.session_id ASC
diff --git a/posthog/hogql/database/schema/test/test_persons.py b/posthog/hogql/database/schema/test/test_persons.py
index 7b83ada891df..463f467b51b8 100644
--- a/posthog/hogql/database/schema/test/test_persons.py
+++ b/posthog/hogql/database/schema/test/test_persons.py
@@ -108,6 +108,80 @@ class TestPersonOptimization(ClickhouseTestMixin, APIBaseTest):
assert response.clickhouse
self.assertIn("where_optimization", response.clickhouse)
self.assertNotIn("in(tuple(person.id, person.version)", response.clickhouse)
+ self.assertIn("multiSearchAny(where_optimization.properties, [%(hogql_val_1)s])", response.clickhouse)
+
+ PREFILTER_PERSONS = [
+ ("escaped", {"$some_prop": 'some"thing'}),
+ ("non_ascii", {"$some_prop": "sömething"}),
+ ("other_key", {"$some_prop": "chrome", "$another_prop": "something"}),
+ ]
+
+ def _create_prefilter_persons(self) -> dict[str, str]:
+ uuids = {}
+ for name, properties in self.PREFILTER_PERSONS:
+ person = _create_person(
+ team_id=self.team.pk,
+ distinct_ids=[name],
+ properties=properties,
+ created_at=datetime(2024, 1, 1, 15),
+ )
+ uuids[name] = str(person.uuid)
+ return uuids
+
+ @parameterized.expand(
+ [
+ ("eq", "properties.$some_prop = 'something'", "[%(hogql_val_1)s]", ["first", "second"]),
+ (
+ "in_list",
+ "properties.$some_prop in ('something', 'other')",
+ "[%(hogql_val_1)s, %(hogql_val_2)s]",
+ ["first", "second"],
+ ),
+ # The unbacked read strips the outer quotes off JSONExtractRaw without unescaping, so the stored value
+ # compares as `some\"thing` and matches nothing. Pre-existing, and the same with or without the pre-check.
+ ("quoted_value", """properties.$some_prop = 'some"thing'""", None, []),
+ ("non_ascii_value", "properties.$some_prop = 'sömething'", None, ["non_ascii"]),
+ (
+ "is_not",
+ "properties.$some_prop != 'something'",
+ None,
+ ["third", "escaped", "non_ascii", "other_key"],
+ ),
+ ]
+ )
+ def test_json_substring_prefilter(
+ self, _name: str, where: str, expected_values: str | None, expected_persons: list[str]
+ ):
+ person_uuids = {
+ "first": str(self.first_person.uuid),
+ "second": str(self.second_person.uuid),
+ "third": str(self.third_person.uuid),
+ **self._create_prefilter_persons(),
+ }
+ response = execute_hogql_query(
+ parse_select(f"select id from persons where {where}"),
+ self.team,
+ modifiers=self.modifiers,
+ )
+ assert response.clickhouse
+ if expected_values is None:
+ self.assertNotIn("multiSearchAny", response.clickhouse)
+ else:
+ self.assertIn(f"multiSearchAny(where_optimization.properties, {expected_values})", response.clickhouse)
+ assert {str(row[0]) for row in response.results} == {person_uuids[name] for name in expected_persons}
+
+ # ClickHouse rejects a multiSearchAny call with more than 255 needles against a nonconstant haystack
+ # ("passed 256, should be at most 255"), which failed the whole query rather than only the pre-check.
+ @parameterized.expand([("at_needle_limit", 255, True), ("past_needle_limit", 256, False)])
+ def test_json_substring_prefilter_needle_limit(self, _name: str, value_count: int, prefiltered: bool):
+ values = ", ".join(f"'needle_{i}'" for i in range(value_count))
+ response = execute_hogql_query(
+ parse_select(f"select id from persons where properties.$some_prop in ({values})"),
+ self.team,
+ modifiers=self.modifiers,
+ )
+ assert response.clickhouse
+ assert ("multiSearchAny" in response.clickhouse) is prefiltered
@snapshot_clickhouse_queries
def test_joins_are_left_alone_for_now(self):
diff --git a/posthog/hogql/database/schema/util/test/test_person_where_clause_extractor.py b/posthog/hogql/database/schema/util/test/test_person_where_clause_extractor.py
index d7125c20b2b5..57d5e33e0055 100644
--- a/posthog/hogql/database/schema/util/test/test_person_where_clause_extractor.py
+++ b/posthog/hogql/database/schema/util/test/test_person_where_clause_extractor.py
@@ -38,6 +38,25 @@ def prop_read(blob: str, *keys: Union[str, int]) -> ast.PropertyAccess:
return ast.PropertyAccess(expr=ast.Field(chain=[blob]), keys=list(keys))
+def prefiltered(values: list[str], compare: Union[ast.Expr, None]) -> ast.Call:
+ # The resolver puts a substring pre-check ahead of an unbacked person JSON compare, so an extracted
+ # clause carries both. See `_person_json_substring_prefilter`.
+ assert compare is not None
+ return ast.Call(
+ name="and",
+ args=[
+ ast.Call(
+ name="multiSearchAny",
+ args=[
+ ast.Field(chain=["properties"]),
+ ast.Array(exprs=[ast.Constant(value=value) for value in values]),
+ ],
+ ),
+ compare,
+ ],
+ )
+
+
class RemoveHiddenAliases(CloningVisitor):
def visit_alias(self, node):
if node.hidden:
@@ -92,12 +111,18 @@ class TestPersonWhereClauseExtractor(ClickhouseTestMixin, APIBaseTest):
def test_person_properties(self):
actual = self.get_clause("SELECT * FROM events WHERE person.properties.email = '[email protected]'")
- expected = _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")})
+ expected = prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ )
assert actual == expected
def test_person_properties_andor_1(self):
actual = self.get_clause("SELECT * FROM events WHERE person.properties.email = '[email protected]' or false")
- expected = _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")})
+ expected = prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ )
assert actual == expected
def test_person_properties_andor_2(self):
@@ -109,8 +134,17 @@ class TestPersonWhereClauseExtractor(ClickhouseTestMixin, APIBaseTest):
"SELECT * FROM events WHERE person.properties.email = '[email protected]' and person.properties.email = '[email protected]'"
)
expected = _expr(
- "{e1} = '[email protected]' and {e2} = '[email protected]'",
- {"e1": prop_read("properties", "email"), "e2": prop_read("properties", "email")},
+ "{a} and {b}",
+ {
+ "a": prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ ),
+ "b": prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ ),
+ },
)
assert actual == expected
@@ -119,8 +153,17 @@ class TestPersonWhereClauseExtractor(ClickhouseTestMixin, APIBaseTest):
"SELECT * FROM events WHERE person.properties.email = '[email protected]' or person.properties.email = '[email protected]'"
)
expected = _expr(
- "{e1} = '[email protected]' or {e2} = '[email protected]'",
- {"e1": prop_read("properties", "email"), "e2": prop_read("properties", "email")},
+ "{a} or {b}",
+ {
+ "a": prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ ),
+ "b": prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ ),
+ },
)
assert actual == expected
@@ -129,8 +172,17 @@ class TestPersonWhereClauseExtractor(ClickhouseTestMixin, APIBaseTest):
"SELECT * FROM events WHERE person.properties.email = '[email protected]' or (1 and person.properties.email = '[email protected]')"
)
expected = _expr(
- "{e1} = '[email protected]' or {e2} = '[email protected]'",
- {"e1": prop_read("properties", "email"), "e2": prop_read("properties", "email")},
+ "{a} or {b}",
+ {
+ "a": prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ ),
+ "b": prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ ),
+ },
)
assert actual == expected
@@ -139,8 +191,17 @@ class TestPersonWhereClauseExtractor(ClickhouseTestMixin, APIBaseTest):
"SELECT * FROM events WHERE person.properties.email = '[email protected]' or (0 or person.properties.email = '[email protected]')"
)
expected = _expr(
- "{e1} = '[email protected]' or {e2} = '[email protected]'",
- {"e1": prop_read("properties", "email"), "e2": prop_read("properties", "email")},
+ "{a} or {b}",
+ {
+ "a": prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ ),
+ "b": prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ ),
+ },
)
assert actual == expected
@@ -154,7 +215,10 @@ class TestPersonWhereClauseExtractor(ClickhouseTestMixin, APIBaseTest):
actual = self.get_clause(
"SELECT * FROM events WHERE event == '$pageview' and person.properties.email = '[email protected]'"
)
- expected = _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")})
+ expected = prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ )
assert actual == expected
def test_person_properties_andor_9(self):
@@ -173,12 +237,18 @@ class TestPersonWhereClauseExtractor(ClickhouseTestMixin, APIBaseTest):
actual = self.get_clause(
"SELECT * FROM events WHERE properties.email = '[email protected]' and person.properties.email = '[email protected]'"
)
- expected = _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")})
+ expected = prefiltered(
+ _expr("{e} = '[email protected]'", {"e": prop_read("properties", "email")}),
+ )
assert actual == expected
def test_person_array(self):
actual = self.get_clause("SELECT * FROM events WHERE person.properties.email IN ['[email protected]']")
- expected = _expr("{e} IN ['[email protected]']", {"e": prop_read("properties", "email")})
+ expected = prefiltered(
+ _expr("{e} IN ['[email protected]']", {"e": prop_read("properties", "email")}),
+ )
assert actual == expected
def test_person_properties_function_calls(self):
diff --git a/posthog/hogql/test/__snapshots__/test_query.ambr b/posthog/hogql/test/__snapshots__/test_query.ambr
index 4457bd4e3208..6eaebc0a8db5 100644
--- a/posthog/hogql/test/__snapshots__/test_query.ambr
+++ b/posthog/hogql/test/__snapshots__/test_query.ambr
@@ -475,10 +475,10 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, [%(hogql_val_2)s]), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_3)s), ''), 'null'), '^"|"$', ''), %(hogql_val_4)s), 0))))))
GROUP BY person.id
- HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_4)s), person.version), plus(now64(6, %(hogql_val_5)s), toIntervalDay(1))))) AS persons
- WHERE ifNull(equals(persons.properties___random_uuid, %(hogql_val_6)s), 0)
+ HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_5)s), person.version), plus(now64(6, %(hogql_val_6)s), toIntervalDay(1))))) AS persons
+ WHERE ifNull(equals(persons.properties___random_uuid, %(hogql_val_7)s), 0)
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, optimize_rewrite_aggregate_function_with_if=0, optimize_min_inequality_conjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
diff --git a/posthog/hogql/transforms/clickhouse_property_resolution.py b/posthog/hogql/transforms/clickhouse_property_resolution.py
index de78a22f156f..6c41f2a93aa3 100644
--- a/posthog/hogql/transforms/clickhouse_property_resolution.py
+++ b/posthog/hogql/transforms/clickhouse_property_resolution.py
@@ -238,6 +238,11 @@ def resolve_property_group_source(
# `node.keys` is the key path (keys[0] is the property name; deeper keys index into the extracted value).
+def _is_json_verbatim(value: str) -> bool:
+ """True when JSON text stores the string unchanged: non-empty printable ASCII with no quote or backslash."""
+ return bool(value) and all(" " <= char <= "~" and char not in '"\\' for char in value)
+
+
def _blob_field_type_of(node: ast.PropertyAccess) -> ast.FieldType | None:
"""The source blob column's `FieldType` (`node.expr.type`), the input to `resolve_materialized_property_source`."""
expr_type = node.expr.type
@@ -615,6 +620,10 @@ def _substitute_value_read(node: ast.PropertyAccess, context: HogQLContext) -> a
# to keep every granule that could hold any of them.
LOGS_BODY_IN_HINT_MAX_VALUES = 50
+# ClickHouse caps multiSearchAny at 255 values to search for, and going over fails the whole query. Past that we drop
+# the pre-check instead of splitting it up: hundreds of values match nearly every row, so it would prune nothing.
+PERSON_JSON_PREFILTER_MAX_NEEDLES = 255
+
def _call(name: str, args: list[ast.Expr]) -> ast.Call:
return ast.Call(name=name, args=args)
@@ -1162,7 +1171,44 @@ class ClickHousePropertyResolver(CloningVisitor):
# surviving PropertyAccess reads the scrubbed materialized column. An is-set check therefore treats both an empty
# string and the literal text "null" as "not set", which over-matches a true "does this key exist in the blob"
# test. Left this way deliberately — tightening it would change query results.
- return super().visit_compare_operation(node)
+ prefilter = self._person_json_substring_prefilter(node)
+ compared = super().visit_compare_operation(node)
+ if prefilter is not None:
+ return _call("and", [prefilter, compared])
+ return compared
+
+ # --- unbacked person JSON: a substring pre-check ahead of the JSON parse ---
+
+ def _person_json_substring_prefilter(self, node: ast.CompareOperation) -> ast.Expr | None:
+ """`multiSearchAny(properties, [values])` to run before `properties.x = 'v'` / `IN (...)` on the raw person blob.
+
+ With no backing column the comparison parses JSON out of every person row's properties, which is CPU-bound on
+ large teams. A constant substring search over the same column is several times cheaper and rejects most rows
+ before the parse; the comparison still decides the row set. Only values that JSON text stores verbatim qualify,
+ because an escaped quote, backslash, or non-ASCII character would not match as a substring.
+ """
+ if self._index_hint_depth > 0 or node.op not in (ast.CompareOperationOp.Eq, ast.CompareOperationOp.In):
+ return None
+ access = self._lowered_property_operand(node.left)
+ if access is None or len(access.keys) != 1:
+ return None
+ field_type = _blob_field_type_of(access)
+ if field_type is None or field_type.name != "properties":
+ return None
+ table_type = _unwrap_to_table_type(field_type)
+ if table_type is None or table_type.table.to_printed_clickhouse(self.context) != "person":
+ return None
+ key = str(access.keys[0])
+ if key in restricted_property_keys_for_table_type(field_type.table_type, self.context):
+ return None
+ if resolve_materialized_property_source(field_type, key, self.context) is not None:
+ return None
+ values = self._extract_string_constants(node.right)
+ if not values or len(values) > PERSON_JSON_PREFILTER_MAX_NEEDLES:
+ return None
+ if not all(_is_json_verbatim(value) for value in values):
+ return None
+ return _call("multiSearchAny", [self.visit(access.expr), ast.Array(exprs=[_const(v) for v in values])])
# --- logs body: keep a constant comparison eligible for the lower(body) ngram index ---
diff --git a/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr b/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr
index 1c5e362c05a7..3bb926e4b46c 100644
--- a/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr
+++ b/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr
@@ -200,13 +200,13 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, [%(hogql_val_1)s]), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0))))))
GROUP BY person.id
- HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_3)s), person.version), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))))) AS persons
- WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_5)s), 0)
+ HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_4)s), person.version), plus(now64(6, %(hogql_val_5)s), toIntervalDay(1))))) AS persons
+ WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_6)s), 0)
ORDER BY persons.id ASC
SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto'))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
- WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_6)s), equals(__in_cohort.matched, 1))
+ WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_7)s), equals(__in_cohort.matched, 1))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, optimize_rewrite_aggregate_function_with_if=0, optimize_min_inequality_conjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -262,28 +262,28 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, [%(hogql_val_1)s]), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0))))))
GROUP BY person.id
- HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_3)s), person.version), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))))) AS persons
- WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_5)s), 0)
+ HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_4)s), person.version), plus(now64(6, %(hogql_val_5)s), toIntervalDay(1))))) AS persons
+ WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_6)s), 0)
ORDER BY persons.id ASC
SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto')) UNION ALL
SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
FROM ((
SELECT persons.id AS id
FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_6)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_7)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, 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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_7)s), ''), 'null'), '^"|"$', ''), %(hogql_val_8)s), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, [%(hogql_val_8)s]), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_9)s), ''), 'null'), '^"|"$', ''), %(hogql_val_10)s), 0))))))
GROUP BY person.id
- HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_9)s), person.version), plus(now64(6, %(hogql_val_10)s), toIntervalDay(1))))) AS persons
- WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_11)s), 0)
+ HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_11)s), person.version), plus(now64(6, %(hogql_val_12)s), toIntervalDay(1))))) AS persons
+ WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_13)s), 0)
ORDER BY persons.id ASC
SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto'))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
- WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_12)s), equals(__in_cohort.matched, 1))
+ WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_14)s), equals(__in_cohort.matched, 1))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, optimize_rewrite_aggregate_function_with_if=0, optimize_min_inequality_conjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
diff --git a/posthog/hogql/transforms/test/test_clickhouse_property_resolution.py b/posthog/hogql/transforms/test/test_clickhouse_property_resolution.py
new file mode 100644
index 000000000000..37e8f99d1c4b
--- /dev/null
+++ b/posthog/hogql/transforms/test/test_clickhouse_property_resolution.py
@@ -0,0 +1,34 @@
+import json
+
+from django.test import SimpleTestCase
+
+from hypothesis import (
+ example,
+ given,
+ settings as hypothesis_settings,
+ strategies as st,
+)
+
+from posthog.hogql.transforms.clickhouse_property_resolution import _is_json_verbatim
+
+# Plain text alone almost never lands in the accepted set, so bias half the draws to printable ASCII.
+_PROPERTY_VALUES = st.one_of(
+ st.text(),
+ st.text(alphabet=st.characters(min_codepoint=0x20, max_codepoint=0x7E)),
+)
+
+
+class TestJSONVerbatimValues(SimpleTestCase):
+ # Producers differ on `ensure_ascii`, and the pre-check runs against whichever one wrote the blob.
+ @given(value=_PROPERTY_VALUES)
+ @example('some"thing')
+ @example("back\\slash")
+ @example("sömething")
+ @example("tab\there")
+ @example("plain_value-1")
+ @hypothesis_settings(max_examples=100, deadline=None)
+ def test_accepted_values_survive_json_encoding(self, value: str) -> None:
+ if not _is_json_verbatim(value):
+ return
+ assert value in json.dumps({"key": value})
+ assert value in json.dumps({"key": value}, ensure_ascii=False)
diff --git a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recordings_list_by_cohort.ambr b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recordings_list_by_cohort.ambr
index 46aca745f0f1..abcb956c7d7c 100644
--- a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recordings_list_by_cohort.ambr
+++ b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recordings_list_by_cohort.ambr
@@ -1133,7 +1133,7 @@
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), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'user_group'), ''), 'null'), '^"|"$', ''), 'internal'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['internal']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'user_group'), ''), 'null'), '^"|"$', ''), 'internal'), 0))))))
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 ifNull(equals(persons.properties___user_group, 'internal'), 0)
diff --git a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
index e179d6fc7eda..6b2479951c09 100644
--- a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
+++ b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
@@ -33,7 +33,7 @@
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), equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), '0'), 0)))))
+ WHERE and(equals(where_optimization.team_id, 99999), equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['0']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), '0'), 0))))))
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 and(equals(persons.team_id, 99999), ifNull(equals(persons.properties___group, '0'), 0))
@@ -86,7 +86,7 @@
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), equals(where_optimization.team_id, 99999), in(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), tuple('0', '1', '2', '3'))))))
+ WHERE and(equals(where_optimization.team_id, 99999), equals(where_optimization.team_id, 99999), and(multiSearchAny(where_optimization.properties, ['0', '1', '2', '3']), in(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), tuple('0', '1', '2', '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 and(equals(persons.team_id, 99999), in(persons.properties___group, tuple('0', '1', '2', '3')))
@@ -428,7 +428,7 @@
WHERE and(equals(where_optimization.team_id, 99999), equals(where_optimization.team_id, 99999), in(where_optimization.id,
(SELECT person_static_cohort.person_id AS person_id
FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, 99999)))), in(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), tuple('1', '2', '4', '5', '6'))))))
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, 99999)))), and(multiSearchAny(where_optimization.properties, ['1', '2', '4', '5', '6']), in(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), tuple('1', '2', '4', '5', '6')))))))
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 and(equals(persons.team_id, 99999), and(in(persons.id,
@@ -560,7 +560,7 @@
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), equals(where_optimization.team_id, 99999), or(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), 'none'), 0), in(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), tuple('1', '2', '3')))))))
+ WHERE and(equals(where_optimization.team_id, 99999), equals(where_optimization.team_id, 99999), or(and(multiSearchAny(where_optimization.properties, ['none']), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), 'none'), 0)), and(multiSearchAny(where_optimization.properties, ['1', '2', '3']), in(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'group'), ''), 'null'), '^"|"$', ''), tuple('1', '2', '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 and(equals(persons.team_id, 99999), or(ifNull(equals(persons.properties___group, 'none'), 0), in(persons.properties___group, tuple('1', '2', '3'))))