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.

feat(experiments): resolve merged persons on precompute reads (#130013)

Juraj Majerikf24321eeedc4
diff --git a/posthog/constants.py b/posthog/constants.py
index 671a3f644c8e..f2b0c872d949 100644
--- a/posthog/constants.py
+++ b/posthog/constants.py
@@ -258,6 +258,7 @@ SUBSCRIPTION_AI_PROMPT_FEATURE_FLAG_KEY = "ai-subscriptions"
# share the v2 activity name and would otherwise send the legacy layout during a rolling deployment.
SUBSCRIPTION_SLACK_GALLERY_FEATURE_FLAG_KEY = "subscription-slack-gallery"
EXPERIMENTS_RATIO_METRIC_EVENTS_PREAGGREGATION_FEATURE_FLAG_KEY = "experiments-ratio-metric-events-preaggregation"
+EXPERIMENTS_PRECOMPUTE_PERSON_RESOLUTION_FEATURE_FLAG_KEY = "experiments-precompute-person-resolution"
GENERATED_DASHBOARD_PREFIX = "Generated Dashboard"
ENRICHED_DASHBOARD_INSIGHT_IDENTIFIER = "Feature Viewed"
diff --git a/posthog/hogql/database/schema/experiment_exposures_preaggregated.py b/posthog/hogql/database/schema/experiment_exposures_preaggregated.py
index 37d4a774d106..6858b2c8a6b3 100644
--- a/posthog/hogql/database/schema/experiment_exposures_preaggregated.py
+++ b/posthog/hogql/database/schema/experiment_exposures_preaggregated.py
@@ -9,6 +9,7 @@ from posthog.hogql.database.models import (
StringDatabaseField,
Table,
)
+from posthog.hogql.database.schema.experiment_preaggregated_person_fields import experiment_preaggregated_person_fields
from posthog.clickhouse.preaggregation.experiment_exposures_sql import DISTRIBUTED_EXPERIMENT_EXPOSURES_TABLE
@@ -30,6 +31,7 @@ class ExperimentExposuresPreaggregatedTable(Table):
"entity_id": StringDatabaseField(
name="entity_id", description="Identifier of the exposed entity (person or group) being analyzed."
),
+ **experiment_preaggregated_person_fields(),
"variant": StringDatabaseField(
name="variant", description="Experiment variant the entity was exposed to (e.g. 'control', 'test')."
),
diff --git a/posthog/hogql/database/schema/experiment_metric_events_preaggregated.py b/posthog/hogql/database/schema/experiment_metric_events_preaggregated.py
index 164ca9b43e4d..d3d1ff9abd31 100644
--- a/posthog/hogql/database/schema/experiment_metric_events_preaggregated.py
+++ b/posthog/hogql/database/schema/experiment_metric_events_preaggregated.py
@@ -6,6 +6,7 @@ from posthog.hogql.database.models import (
StringDatabaseField,
Table,
)
+from posthog.hogql.database.schema.experiment_preaggregated_person_fields import experiment_preaggregated_person_fields
from posthog.clickhouse.preaggregation.experiment_metric_events_sql import DISTRIBUTED_EXPERIMENT_METRIC_EVENTS_TABLE
@@ -24,6 +25,7 @@ class ExperimentMetricEventsPreaggregatedTable(Table):
"entity_id": StringDatabaseField(
name="entity_id", description="Identifier of the entity (person or group) the metric event belongs to."
),
+ **experiment_preaggregated_person_fields(),
"timestamp": DateTimeDatabaseField(name="timestamp", description="When the metric event occurred (UTC)."),
"event_uuid": StringDatabaseField(
name="event_uuid", description="UUID of the source event; join to `events.uuid`."
diff --git a/posthog/hogql/database/schema/experiment_preaggregated_person_fields.py b/posthog/hogql/database/schema/experiment_preaggregated_person_fields.py
new file mode 100644
index 000000000000..8ae0add052c5
--- /dev/null
+++ b/posthog/hogql/database/schema/experiment_preaggregated_person_fields.py
@@ -0,0 +1,53 @@
+from posthog.hogql.database.lazy_join_tags import PERSON_DISTINCT_ID_OVERRIDES
+from posthog.hogql.database.models import (
+ ExpressionField,
+ FieldOrTable,
+ LazyJoin,
+ StringDatabaseField,
+ UUIDDatabaseField,
+)
+from posthog.hogql.database.schema.person_distinct_id_overrides import PersonDistinctIdOverridesTable
+from posthog.hogql.parser import parse_expr
+
+
+def experiment_preaggregated_person_fields() -> dict[str, FieldOrTable]:
+ """
+ The fields that resolve a row of an experiment preaggregated table to its current person.
+
+ A row stores the person as of the time the job built it. A later identify or merge moves the
+ row's distinct_id to another person, and `person_id` follows that move the way `events.person_id`
+ does: it reads `person_distinct_id_overrides` first and falls back to the stored person.
+
+ The override applies for every team instead of following the persons-on-events mode, as in
+ `flag_evaluations`. A team on `PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS` therefore reads a
+ merge-aware person here, and from `events` the write-time person until the squash rewrites it.
+
+ A row with an empty distinct_id matches no override row, so it keeps its stored person.
+
+ Each table takes its own copy of the fields.
+ """
+ return {
+ "distinct_id": StringDatabaseField(
+ name="distinct_id",
+ description="Distinct_id of the row's source event; resolves the row to its current person.",
+ ),
+ "stored_person_id": UUIDDatabaseField(name="person_id", hidden=True),
+ # Joined only when a query reads `person_id`, so a group-aggregated read pays nothing for it.
+ "override": LazyJoin(
+ from_field=["distinct_id"],
+ join_table=PersonDistinctIdOverridesTable(),
+ resolver=PERSON_DISTINCT_ID_OVERRIDES,
+ hidden=True,
+ ),
+ "person_id": ExpressionField(
+ name="person_id",
+ expr=parse_expr(
+ # NOTE: assumes `join_use_nulls = 0` (the default), as ``override.distinct_id`` is not Nullable
+ "if(not(empty(override.distinct_id)), override.person_id, stored_person_id)",
+ start=None,
+ ),
+ isolate_scope=True,
+ description="The person the row belongs to, corrected for any later identify or merge. "
+ "Meaningful only for person-aggregated experiments.",
+ ),
+ }
diff --git a/products/experiments/backend/hogql_queries/experiment_exposure_query_builder.py b/products/experiments/backend/hogql_queries/experiment_exposure_query_builder.py
index 932f45b3a813..09da16d29aa9 100644
--- a/products/experiments/backend/hogql_queries/experiment_exposure_query_builder.py
+++ b/products/experiments/backend/hogql_queries/experiment_exposure_query_builder.py
@@ -281,9 +281,7 @@ class ExposureQueryBuilder:
return query
def _build_precomputed_entity_id_expr(self) -> ast.Expr:
- return (
- parse_expr("toUUID(t.entity_id)") if self.context.entity_key == "person_id" else parse_expr("t.entity_id")
- )
+ return parse_expr(self.context.precomputed_entity_id_sql())
def _build_precomputed_variant_expr(self) -> ast.Expr:
if self.context.multiple_variant_handling == MultipleVariantHandling.FIRST_SEEN:
diff --git a/products/experiments/backend/hogql_queries/experiment_funnel_query_builder.py b/products/experiments/backend/hogql_queries/experiment_funnel_query_builder.py
index f683cc98e7f2..44b042239131 100644
--- a/products/experiments/backend/hogql_queries/experiment_funnel_query_builder.py
+++ b/products/experiments/backend/hogql_queries/experiment_funnel_query_builder.py
@@ -113,7 +113,7 @@ class FunnelQueryBuilder:
)
later_step_extracts = [f"arrayElement(t.steps, {i + 1}) AS step_{i}" for i in range(1, num_steps)]
step_extracts = ", ".join([step_0_extract, *later_step_extracts])
- entity_id_cast = "toUUID(t.entity_id)" if self._b.entity_key == "person_id" else "t.entity_id"
+ entity_id_sql = self._b.context.precomputed_entity_id_sql()
# Filter by experiment date range: jobs can cover broader time ranges
# than the experiment for cache reusability, so we must filter on read.
@@ -128,7 +128,7 @@ class FunnelQueryBuilder:
metric_events_cte_str = f"""
metric_events AS (
SELECT
- {entity_id_cast} AS entity_id,
+ {entity_id_sql} AS entity_id,
t.timestamp AS timestamp,
t.event_uuid AS uuid,
{step_extracts}
diff --git a/products/experiments/backend/hogql_queries/experiment_mean_query_builder.py b/products/experiments/backend/hogql_queries/experiment_mean_query_builder.py
index 3437d67302b1..12a196dc13a0 100644
--- a/products/experiments/backend/hogql_queries/experiment_mean_query_builder.py
+++ b/products/experiments/backend/hogql_queries/experiment_mean_query_builder.py
@@ -137,7 +137,7 @@ class MeanQueryBuilder:
metric_events_cte = build_precomputed_metric_events_cte(
cte_name="metric_events",
source=self._b.metric.source,
- entity_key=self._b.entity_key,
+ entity_id_sql=self._b.context.precomputed_entity_id_sql(),
job_ids_placeholder="metric_events_job_ids",
)
else:
diff --git a/products/experiments/backend/hogql_queries/experiment_query_builder.py b/products/experiments/backend/hogql_queries/experiment_query_builder.py
index 1ec7840a53dd..5e4a53e5d681 100644
--- a/products/experiments/backend/hogql_queries/experiment_query_builder.py
+++ b/products/experiments/backend/hogql_queries/experiment_query_builder.py
@@ -60,6 +60,7 @@ from products.experiments.backend.hogql_queries.exposure_query_logic import (
resolve_default_exposure_event,
resolve_filter_test_accounts,
)
+from products.experiments.backend.hogql_queries.precompute_policy import precompute_person_resolution_enabled
def resolve_exposure_config_for_builder(
@@ -208,6 +209,7 @@ class ExperimentQueryBuilder:
breakdowns=tuple(self.breakdowns),
only_count_matured_users=self.only_count_matured_users,
cuped_config=self.cuped_config,
+ resolve_precomputed_persons=precompute_person_resolution_enabled(team),
activation_config=self.activation_config,
read_exposure_copies=metric is None or not metric_reads_flag_call(team, metric),
)
diff --git a/products/experiments/backend/hogql_queries/experiment_query_context.py b/products/experiments/backend/hogql_queries/experiment_query_context.py
index 8bf7032a5964..c6a0b2df40a1 100644
--- a/products/experiments/backend/hogql_queries/experiment_query_context.py
+++ b/products/experiments/backend/hogql_queries/experiment_query_context.py
@@ -32,6 +32,9 @@ class ExperimentQueryContext:
breakdowns: tuple[Breakdown, ...]
only_count_matured_users: bool
cuped_config: CupedQueryConfig
+ # Whether a person-aggregated read of a preaggregated table follows person merges. The caller
+ # evaluates it once per query, so that every read of the query resolves persons the same way.
+ resolve_precomputed_persons: bool
# When set, an entity only counts as exposed once it emits this event at/after its first
# default exposure event, and that activation event's timestamp becomes the exposure time.
activation_config: ExperimentEventExposureConfig | ActionsNode | None = None
@@ -40,6 +43,21 @@ class ExperimentQueryContext:
# so one of them would be the exposure and the other a conversion.
read_exposure_copies: bool = True
+ def precomputed_entity_id_sql(self) -> str:
+ """
+ The entity that a row of an experiment preaggregated table belongs to, for a read that
+ aliases the table as `t`.
+
+ A group-aggregated read uses `entity_id`, which holds the group key. A person-aggregated
+ read uses the person stored in `entity_id` at build time, cast back to the UUID that the
+ downstream joins expect. With merge resolution on, it uses the table's `person_id`
+ instead, which follows a later identify or merge. Two rows that were built for different
+ persons and now belong to one person then count as one entity, as they do on the events scan.
+ """
+ if self.entity_key != "person_id":
+ return "t.entity_id"
+ return "t.person_id" if self.resolve_precomputed_persons else "toUUID(t.entity_id)"
+
@dataclass(frozen=True)
class ExperimentPrecomputationContext:
diff --git a/products/experiments/backend/hogql_queries/experiment_ratio_query_builder.py b/products/experiments/backend/hogql_queries/experiment_ratio_query_builder.py
index d89a60d0e862..ed7328a46c27 100644
--- a/products/experiments/backend/hogql_queries/experiment_ratio_query_builder.py
+++ b/products/experiments/backend/hogql_queries/experiment_ratio_query_builder.py
@@ -321,13 +321,13 @@ class RatioQueryBuilder:
numerator_events_cte = build_precomputed_metric_events_cte(
cte_name="numerator_events",
source=self._b.metric.numerator,
- entity_key=self._b.entity_key,
+ entity_id_sql=self._b.context.precomputed_entity_id_sql(),
job_ids_placeholder="numerator_job_ids",
)
denominator_events_cte = build_precomputed_metric_events_cte(
cte_name="denominator_events",
source=self._b.metric.denominator,
- entity_key=self._b.entity_key,
+ entity_id_sql=self._b.context.precomputed_entity_id_sql(),
job_ids_placeholder="denominator_job_ids",
)
else:
diff --git a/products/experiments/backend/hogql_queries/experiment_retention_query_builder.py b/products/experiments/backend/hogql_queries/experiment_retention_query_builder.py
index 00efd12c6c40..e74974142b7f 100644
--- a/products/experiments/backend/hogql_queries/experiment_retention_query_builder.py
+++ b/products/experiments/backend/hogql_queries/experiment_retention_query_builder.py
@@ -206,10 +206,10 @@ class RetentionQueryBuilder:
# arithmetic, maturity, same-event exclusion) is read-time and stays unchanged.
# No dedup of replayed build rows is needed: min/max/argMin/argMax and the
# MAX(0/1) outcome are all idempotent under duplicated rows, unlike mean's sums.
- entity_id_cast = "toUUID(t.entity_id)" if self._b.entity_key == "person_id" else "t.entity_id"
+ entity_id_sql = self._b.context.precomputed_entity_id_sql()
start_events_body = f"""FROM (
SELECT
- {entity_id_cast} AS entity_id,
+ {entity_id_sql} AS entity_id,
t.timestamp AS timestamp,
t.event_uuid AS uuid
FROM experiment_metric_events_preaggregated AS t
@@ -222,7 +222,7 @@ class RetentionQueryBuilder:
INNER JOIN exposures ON events.entity_id = exposures.entity_id
WHERE {{start_after_exposure_predicate}}"""
completion_events_body = f"""SELECT
- {entity_id_cast} AS entity_id,
+ {entity_id_sql} AS entity_id,
t.event_uuid AS completion_uuid,
t.timestamp AS completion_timestamp
FROM experiment_metric_events_preaggregated AS t
diff --git a/products/experiments/backend/hogql_queries/metric_source_precompute.py b/products/experiments/backend/hogql_queries/metric_source_precompute.py
index 68748f355024..6199fb09cc51 100644
--- a/products/experiments/backend/hogql_queries/metric_source_precompute.py
+++ b/products/experiments/backend/hogql_queries/metric_source_precompute.py
@@ -92,7 +92,7 @@ def build_precomputed_metric_events_cte(
*,
cte_name: str,
source: EventsNode | ActionsNode,
- entity_key: str,
+ entity_id_sql: str,
job_ids_placeholder: str,
) -> str:
"""
@@ -112,13 +112,12 @@ def build_precomputed_metric_events_cte(
# dedups at merge time and this read doesn't use FINAL, so a re-applied build
# INSERT would otherwise double-count sums. Same defense as the exposures read;
# the funnel read skips it because funnel evaluation tolerates duplicate events.
- entity_id_cast = "toUUID(t.entity_id)" if entity_key == "person_id" else "t.entity_id"
- # For ID-valued math the value is the ID itself: the stored entity_id
- # is the person id (dau) and session_id is the unique_session value,
+ # For ID-valued math the value is the ID itself: the entity
+ # is the person (dau) and session_id is the unique_session value,
# so the downstream count(distinct) matches the direct path.
math_type = getattr(source, "math", None) or ExperimentMetricMathType.TOTAL
if math_type == ExperimentMetricMathType.DAU:
- value_select = entity_id_cast
+ value_select = entity_id_sql
elif math_type == ExperimentMetricMathType.UNIQUE_SESSION:
value_select = "any(t.session_id)"
else:
@@ -126,7 +125,7 @@ def build_precomputed_metric_events_cte(
return f"""
{cte_name} AS (
SELECT
- {entity_id_cast} AS entity_id,
+ {entity_id_sql} AS entity_id,
t.timestamp AS timestamp,
{value_select} AS value
FROM experiment_metric_events_preaggregated AS t
@@ -134,7 +133,7 @@ def build_precomputed_metric_events_cte(
AND t.team_id = {{metric_events_team_id}}
AND t.timestamp >= {{metric_events_date_from}}
AND t.timestamp < {{metric_events_date_to}} + toIntervalSecond({{metric_events_conversion_window_seconds}})
- GROUP BY t.entity_id, t.timestamp, t.event_uuid
+ GROUP BY {entity_id_sql}, t.timestamp, t.event_uuid
)"""
diff --git a/products/experiments/backend/hogql_queries/precompute_policy.py b/products/experiments/backend/hogql_queries/precompute_policy.py
index 040d878562d6..2f0de135186d 100644
--- a/products/experiments/backend/hogql_queries/precompute_policy.py
+++ b/products/experiments/backend/hogql_queries/precompute_policy.py
@@ -10,8 +10,10 @@ from typing import Any, Optional
from django.db.models import Q
+import posthoganalytics
from pydantic import BaseModel
+from posthog.constants import EXPERIMENTS_PRECOMPUTE_PERSON_RESOLUTION_FEATURE_FLAG_KEY
from posthog.models.team.team import Team
from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (
@@ -58,6 +60,30 @@ PRECOMPUTE_TTL_JITTER_SECONDS = 14 * 24 * 60 * 60
METRIC_EVENTS_MAX_WINDOW_EXTENSION_SECONDS = 90 * 24 * 60 * 60 # 90 days
+def precompute_person_resolution_enabled(team: Team) -> bool:
+ """
+ Whether a person-aggregated read of the precomputed tables resolves each row to its current
+ person through `person_distinct_id_overrides`.
+
+ The join aggregates all of the team's override rows on every read, and it can change only a
+ row that stores its distinct_id. Off, a read takes the person stored at build time and pays
+ no join. Default-off and fail-safe: an absent or unevaluable flag keeps the read without the join.
+ """
+ return bool(
+ posthoganalytics.feature_enabled(
+ EXPERIMENTS_PRECOMPUTE_PERSON_RESOLUTION_FEATURE_FLAG_KEY,
+ str(team.uuid),
+ groups={"organization": str(team.organization_id), "project": str(team.id)},
+ group_properties={
+ "organization": {"id": str(team.organization_id)},
+ "project": {"id": str(team.id), "uuid": str(team.uuid)},
+ },
+ only_evaluate_locally=True,
+ send_feature_flag_events=False,
+ )
+ )
+
+
def experiment_precompute_ttl_schedule(team_timezone: str) -> TtlSchedule:
return parse_ttl_schedule(
DEFAULT_EXPOSURE_TTL_SECONDS,
diff --git a/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_query_builder.ambr b/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_query_builder.ambr
index f2d11e8424fb..316352ca80c4 100644
--- a/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_query_builder.ambr
+++ b/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_query_builder.ambr
@@ -824,7 +824,7 @@
any(t.numeric_value) AS value
FROM experiment_metric_events_preaggregated AS t
WHERE and(in(t.job_id, ['00000000-0000-0000-0000-0000000000a1']), equals(t.team_id, 99999), greaterOrEquals(t.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00'))), less(t.timestamp, plus(assumeNotNull(toDateTime('2024-01-20 12:00:00')), toIntervalSecond(604800))))
- GROUP BY t.entity_id,
+ GROUP BY toUUID(t.entity_id),
t.timestamp,
t.event_uuid),
entity_metrics AS
diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.ambr
index f6b4bb7ab3e1..74aa6e7948ca 100644
--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.ambr
+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.ambr
@@ -2551,7 +2551,7 @@
accurateCastOrNull(t.entity_id, 'UUID') AS value
FROM experiment_metric_events_preaggregated AS t
WHERE and(equals(t.team_id, 99999), in(t.job_id, ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000']), equals(t.team_id, 99999), greaterOrEquals(t.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(t.timestamp, toDateTime64(plus(assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC')), toIntervalSecond(0)), 6, 'UTC')))
- GROUP BY t.entity_id,
+ GROUP BY accurateCastOrNull(t.entity_id, 'UUID'),
toTimeZone(t.timestamp, 'UTC'),
t.event_uuid),
entity_metrics AS
diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_mean_metric.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_mean_metric.ambr
index 9b091cd597e8..07383145c82c 100644
--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_mean_metric.ambr
+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_mean_metric.ambr
@@ -740,7 +740,7 @@
any(t.numeric_value) AS value
FROM experiment_metric_events_preaggregated AS t
WHERE and(equals(t.team_id, 99999), in(t.job_id, ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000']), equals(t.team_id, 99999), greaterOrEquals(t.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(t.timestamp, toDateTime64(plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0)), 6, 'UTC')))
- GROUP BY t.entity_id,
+ GROUP BY accurateCastOrNull(t.entity_id, 'UUID'),
toTimeZone(t.timestamp, 'UTC'),
t.event_uuid),
entity_metrics AS
diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_ratio_metric.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_ratio_metric.ambr
index d13f4afa83b2..d5dd9803c4b8 100644
--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_ratio_metric.ambr
+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_ratio_metric.ambr
@@ -109,7 +109,7 @@
any(t.numeric_value) AS value
FROM experiment_metric_events_preaggregated AS t
WHERE and(equals(t.team_id, 99999), in(t.job_id, ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000']), equals(t.team_id, 99999), greaterOrEquals(t.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(t.timestamp, toDateTime64(plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0)), 6, 'UTC')))
- GROUP BY t.entity_id,
+ GROUP BY accurateCastOrNull(t.entity_id, 'UUID'),
toTimeZone(t.timestamp, 'UTC'),
t.event_uuid),
denominator_events AS
@@ -118,7 +118,7 @@
any(t.numeric_value) AS value
FROM experiment_metric_events_preaggregated AS t
WHERE and(equals(t.team_id, 99999), in(t.job_id, ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000']), equals(t.team_id, 99999), greaterOrEquals(t.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(t.timestamp, toDateTime64(plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0)), 6, 'UTC')))
- GROUP BY t.entity_id,
+ GROUP BY accurateCastOrNull(t.entity_id, 'UUID'),
toTimeZone(t.timestamp, 'UTC'),
t.event_uuid),
numerator_agg AS
diff --git a/products/experiments/backend/hogql_queries/test/test_experiment_exposure_preaggregation.py b/products/experiments/backend/hogql_queries/test/test_experiment_exposure_preaggregation.py
index 8894ddfd7003..8afe6c2e27cc 100644
--- a/products/experiments/backend/hogql_queries/test/test_experiment_exposure_preaggregation.py
+++ b/products/experiments/backend/hogql_queries/test/test_experiment_exposure_preaggregation.py
@@ -3,7 +3,12 @@ from datetime import UTC, datetime
from typing import Any, cast
from uuid import uuid4
-from posthog.test.base import _create_event, _create_person
+from posthog.test.base import (
+ _create_event,
+ _create_person,
+ create_person_id_override_by_distinct_id,
+ flush_persons_and_events,
+)
from unittest.mock import patch
from django.forms.models import model_to_dict
@@ -21,12 +26,18 @@ from posthog.schema import (
ExperimentMetricMathType,
ExperimentQuery,
ExperimentQueryResponse,
+ ExperimentRetentionMetric,
+ FunnelConversionWindowTimeUnit,
IntervalType,
+ PersonsOnEventsMode,
+ StartHandling,
)
from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, get_default_hogql_global_settings
from posthog.clickhouse.client import sync_execute
+from posthog.clickhouse.preaggregation.experiment_exposures_sql import SHARDED_EXPERIMENT_EXPOSURES_TABLE
+from posthog.clickhouse.preaggregation.experiment_metric_events_sql import SHARDED_EXPERIMENT_METRIC_EVENTS_TABLE
from posthog.exceptions import ClickHouseAtCapacity
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
@@ -1034,3 +1045,124 @@ class TestExperimentExposurePreaggregation(ExperimentQueryRunnerBaseTest):
# A full pool is a retry signal, not a broken precompute path.
mock_capture.assert_not_called()
+
+ @parameterized.expand(
+ [
+ ("mean", ExperimentMeanMetric(source=EventsNode(event="purchase", math=ExperimentMetricMathType.TOTAL))),
+ ("funnel", ExperimentFunnelMetric(series=[EventsNode(event="purchase")])),
+ (
+ "retention",
+ ExperimentRetentionMetric(
+ start_event=EventsNode(event="signup"),
+ completion_event=EventsNode(event="purchase"),
+ retention_window_start=0,
+ retention_window_end=7,
+ retention_window_unit=FunnelConversionWindowTimeUnit.DAY,
+ start_handling=StartHandling.FIRST_SEEN,
+ ),
+ ),
+ ]
+ )
+ def test_person_merged_after_the_build_counts_once(self, _name, metric):
+ self.enterContext(
+ patch(
+ "products.experiments.backend.hogql_queries.experiment_query_builder.precompute_person_resolution_enabled",
+ return_value=True,
+ )
+ )
+ self.team.modifiers = {"personsOnEventsMode": PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS}
+ self.team.save()
+
+ feature_flag = self.create_feature_flag()
+ experiment = self.create_experiment(
+ feature_flag=feature_flag,
+ start_date=datetime(2024, 1, 1),
+ end_date=datetime(2024, 1, 10),
+ )
+ experiment.metrics = [metric.model_dump(mode="json")]
+ experiment.save()
+
+ distinct_id_by_person_uuid: dict[str, str] = {}
+
+ def create_user(distinct_id: str, variant: str | None, converts: bool) -> None:
+ person = _create_person(distinct_ids=[distinct_id], team_id=self.team.pk)
+ distinct_id_by_person_uuid[str(person.uuid)] = distinct_id
+ if variant is not None:
+ self._create_exposure_event(distinct_id, feature_flag, variant, datetime(2024, 1, 2, 12, 0, tzinfo=UTC))
+ if converts:
+ for event, day in (("signup", 3), ("purchase", 4)):
+ _create_event(
+ team=self.team,
+ event=event,
+ distinct_id=distinct_id,
+ timestamp=datetime(2024, 1, day, 12, 0, tzinfo=UTC),
+ )
+
+ create_user("control_0", "control", converts=True)
+ create_user("control_1", "control", converts=True)
+ # Each pair is one user seen under two distinct ids, merged only after the build.
+ # The exposure is on the anonymous id and the conversion on the identified id.
+ create_user("anon_exposed", "test", converts=False)
+ create_user("identified_converts", None, converts=True)
+ # The conversion is on the anonymous id and the exposure on the identified id.
+ create_user("anon_converts", None, converts=True)
+ create_user("identified_exposed", "test", converts=False)
+ # Both ids are exposed, and only the identified id starts.
+ create_user("anon_exposed_twice", "test", converts=False)
+ create_user("identified_exposed_twice", "test", converts=False)
+ _create_event(
+ team=self.team,
+ event="signup",
+ distinct_id="identified_exposed_twice",
+ timestamp=datetime(2024, 1, 3, 12, 0, tzinfo=UTC),
+ )
+ flush_persons_and_events()
+
+ def run(use_precomputation: bool) -> ExperimentQueryResponse:
+ if use_precomputation:
+ self._enable_precomputation()
+ else:
+ self._disable_precomputation()
+ runner = ExperimentQueryRunner(
+ query=ExperimentQuery(experiment_id=experiment.id, kind="ExperimentQuery", metric=metric),
+ team=self.team,
+ )
+ response = cast(ExperimentQueryResponse, runner.calculate())
+ assert runner._is_precomputed is use_precomputation
+ assert runner._metric_events_precomputed is use_precomputation
+ return response
+
+ run(use_precomputation=True)
+ jobs_after_build = set(PreaggregationJob.objects.values_list("id", flat=True))
+
+ # The build does not store distinct_id, so the test sets it on the built rows.
+ for table in (SHARDED_EXPERIMENT_EXPOSURES_TABLE(), SHARDED_EXPERIMENT_METRIC_EVENTS_TABLE()):
+ sync_execute(
+ f"""
+ ALTER TABLE {table}
+ UPDATE distinct_id = transform(entity_id, %(person_uuids)s, %(distinct_ids)s, '')
+ WHERE team_id = %(team_id)s
+ SETTINGS mutations_sync = 2
+ """,
+ {
+ "person_uuids": list(distinct_id_by_person_uuid.keys()),
+ "distinct_ids": list(distinct_id_by_person_uuid.values()),
+ "team_id": self.team.pk,
+ },
+ )
+
+ for anonymous_id, identified_id in (
+ ("anon_exposed", "identified_converts"),
+ ("anon_converts", "identified_exposed"),
+ ("anon_exposed_twice", "identified_exposed_twice"),
+ ):
+ create_person_id_override_by_distinct_id(anonymous_id, identified_id, self.team.pk)
+
+ precomputed_result = run(use_precomputation=True)
+ assert set(PreaggregationJob.objects.values_list("id", flat=True)) == jobs_after_build
+ direct_result = run(use_precomputation=False)
+
+ for result in (precomputed_result, direct_result):
+ assert result.variant_results is not None
+ assert result.variant_results[0].number_of_samples == 3
+ assert result.variant_results[0].sum == 2
diff --git a/products/experiments/backend/replay_linkage.py b/products/experiments/backend/replay_linkage.py
index c824b44031c7..087156c51316 100644
--- a/products/experiments/backend/replay_linkage.py
+++ b/products/experiments/backend/replay_linkage.py
@@ -82,6 +82,7 @@ from products.experiments.backend.hogql_queries.precompute_policy import (
experiment_has_min_runtime_for_precomputation,
experiment_precompute_ttl_schedule,
has_uncalculated_cohorts,
+ precompute_person_resolution_enabled,
)
from products.experiments.backend.models.experiment import Experiment
from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig
@@ -437,6 +438,7 @@ def resolve_exposure_linkage(
breakdowns=(),
only_count_matured_users=False,
cuped_config=CupedQueryConfig(),
+ resolve_precomputed_persons=precompute_person_resolution_enabled(team),
activation_config=exposure_params.activation_config,
)
read = _resolve_exposure_read(team, experiment, context)