: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(experiments): extract retention query construction (#60789)
Anders045d6585044d
diff --git a/posthog/hogql_queries/experiments/experiment_query_builder.py b/posthog/hogql_queries/experiments/experiment_query_builder.pyindex 39c165dda96a..2d45db6748d1 100644--- a/posthog/hogql_queries/experiments/experiment_query_builder.py+++ b/posthog/hogql_queries/experiments/experiment_query_builder.py@@ -14,22 +14,14 @@ from posthog.schema import (ExperimentMetricOutlierHandling,ExperimentRatioMetric,ExperimentRetentionMetric,- FunnelConversionWindowTimeUnit,MultipleVariantHandling,- StartHandling,)from posthog.hogql import astfrom posthog.hogql.constants import MAX_SELECT_RETURNED_ROWSfrom posthog.hogql.parser import parse_expr, parse_select-from posthog.hogql_queries.experiments.base_query_utils import (- conversion_window_to_seconds,- data_warehouse_node_to_filter,- event_or_action_to_filter,- is_session_property_metric,- validate_session_property,-)+from posthog.hogql_queries.experiments.base_query_utils import is_session_property_metric, validate_session_propertyfrom posthog.hogql_queries.experiments.breakdown_injector import BreakdownInjectorfrom posthog.hogql_queries.experiments.cuped_config import CupedQueryConfigfrom posthog.hogql_queries.experiments.experiment_exposure_query_builder import ExposureQueryBuilder@@ -47,10 +39,10 @@ from posthog.hogql_queries.experiments.experiment_query_context import (ExperimentPrecomputationContext,ExperimentQueryContext,)+from posthog.hogql_queries.experiments.experiment_retention_query_builder import RetentionQueryBuilderfrom posthog.hogql_queries.experiments.exposure_query_logic import normalize_to_exposure_criteriafrom posthog.hogql_queries.experiments.funnel_step_builder import FunnelStepBuilderfrom posthog.hogql_queries.experiments.metric_source import MetricSourceInfo-from posthog.hogql_queries.insights.utils.utils import get_start_of_interval_hogqlfrom posthog.hogql_queries.utils.query_date_range import QueryDateRangefrom posthog.models.team.team import Team@@ -188,6 +180,10 @@ class ExperimentQueryBuilder:"""return FunnelQueryBuilder(self)+ def _retention_query_builder(self) -> RetentionQueryBuilder:+ """Construct a RetentionQueryBuilder backed by the current builder state."""+ return RetentionQueryBuilder(self)+def get_exposure_timeseries_query(self) -> ast.SelectQuery:"""Returns a query for exposure timeseries data.@@ -229,11 +225,7 @@ class ExperimentQueryBuilder:Equals retention_window_end converted to seconds; conversion_window doesnot contribute because retention maturity is anchored on start_event."""- assert isinstance(self.metric, ExperimentRetentionMetric)- return conversion_window_to_seconds(- self.metric.retention_window_end,- self.metric.retention_window_unit,- )+ return self._retention_query_builder().get_retention_maturity_seconds()def _build_maturity_having_clause(self, timestamp_expr: str = "timestamp") -> Optional[ast.Expr]:"""@@ -273,26 +265,7 @@ class ExperimentQueryBuilder:Anchored on the user's start_event timestamp (min or max of start eventtimestamps, depending on start_handling)."""- if not isinstance(self.metric, ExperimentRetentionMetric):- return None- if not self.only_count_matured_users:- return None-- maturity_seconds = self._get_retention_maturity_seconds()- if maturity_seconds == 0:- return None-- now = timezone.now().strftime("%Y-%m-%d %H:%M:%S")- start_timestamp_expr = self._build_start_event_timestamp_expr()-- return parse_expr(- "{start_ts} + toIntervalSecond({maturity_seconds}) <= toDateTime({now}, 'UTC')",- placeholders={- "start_ts": start_timestamp_expr,- "maturity_seconds": ast.Constant(value=maturity_seconds),- "now": ast.Constant(value=now),- },- )+ return self._retention_query_builder().build_retention_maturity_having_clause()def _build_funnel_query(self) -> ast.SelectQuery:"""@@ -1547,112 +1520,7 @@ class ExperimentQueryBuilder:measures "Of users who did X, how many came back to do Y?" rather than"Of all exposed users, how many did X and then Y?""""- assert isinstance(self.metric, ExperimentRetentionMetric)-- # Build the CTEs- common_ctes = """- exposures AS (- {exposure_select_query}- ),-- start_events AS (- SELECT- exposures.entity_id AS entity_id,- {start_timestamp_expr} AS start_timestamp- FROM events- INNER JOIN exposures ON {entity_key} = exposures.entity_id- WHERE {start_event_predicate}- AND {start_after_exposure_predicate}- GROUP BY exposures.entity_id- ),-- completion_events AS (- SELECT- {entity_key} AS entity_id,- timestamp AS completion_timestamp- FROM events- WHERE {completion_event_predicate}- ),-- entity_metrics AS (- SELECT- exposures.entity_id AS entity_id,- exposures.variant AS variant,- MAX(if(- completion_events.completion_timestamp IS NOT NULL- AND {truncated_completion_timestamp} >= {truncated_start_timestamp} + {retention_window_start_interval}- AND {truncated_completion_timestamp} <= {truncated_start_timestamp} + {retention_window_end_interval},- 1,- 0- )) AS value- FROM exposures- INNER JOIN start_events- ON exposures.entity_id = start_events.entity_id- LEFT JOIN completion_events- ON exposures.entity_id = completion_events.entity_id- AND {completion_retention_window_predicate}- GROUP BY exposures.entity_id, exposures.variant- )- """-- placeholders = {- "exposure_select_query": self._get_exposure_query(),- "entity_key": parse_expr(self.entity_key),- "start_timestamp_expr": self._build_start_event_timestamp_expr(),- "start_event_predicate": self._build_start_event_predicate(),- "completion_event_predicate": self._build_completion_event_predicate(),- "retention_window_start_interval": self._build_retention_window_interval(- self.metric.retention_window_start- ),- "retention_window_end_interval": self._build_retention_window_interval(self.metric.retention_window_end),- "start_after_exposure_predicate": self._build_start_after_exposure_predicate(),- "completion_retention_window_predicate": self._build_completion_retention_window_predicate(),- "truncated_start_timestamp": self._get_retention_window_truncation_expr(- parse_expr("start_events.start_timestamp")- ),- "truncated_completion_timestamp": self._get_retention_window_truncation_expr(- parse_expr("completion_events.completion_timestamp")- ),- }-- query = parse_select(- f"""- WITH {common_ctes}-- SELECT- entity_metrics.variant AS variant,- count(entity_metrics.entity_id) AS num_users,- sum(entity_metrics.value) AS total_sum,- sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,- count(entity_metrics.entity_id) AS denominator_sum,- count(entity_metrics.entity_id) AS denominator_sum_squares,- sum(entity_metrics.value) AS numerator_denominator_sum_product- FROM entity_metrics- WHERE notEmpty(variant)- GROUP BY entity_metrics.variant- """,- placeholders=placeholders,- )-- assert isinstance(query, ast.SelectQuery)-- # Inject maturity HAVING clause into the start_events CTE, anchored on- # the user's start_event timestamp so users whose retention window has- # not yet elapsed are excluded from the denominator.- retention_maturity = self._build_retention_maturity_having_clause()- if retention_maturity is not None and query.ctes and "start_events" in query.ctes:- start_events_cte = query.ctes["start_events"]- if isinstance(start_events_cte, ast.CTE) and isinstance(start_events_cte.expr, ast.SelectQuery):- if start_events_cte.expr.having is None:- start_events_cte.expr.having = retention_maturity- else:- start_events_cte.expr.having = ast.And(exprs=[start_events_cte.expr.having, retention_maturity])-- # Inject breakdown columns if breakdown filter is present- if self.breakdown_injector:- self.breakdown_injector.inject_retention_breakdown_columns(query)-- return query+ return self._retention_query_builder().build_retention_query()def _build_start_event_timestamp_expr(self) -> ast.Expr:"""@@ -1660,12 +1528,7 @@ class ExperimentQueryBuilder:FIRST_SEEN: Use the first occurrence of start eventLAST_SEEN: Use the last occurrence of start event"""- assert isinstance(self.metric, ExperimentRetentionMetric)-- if self.metric.start_handling == StartHandling.FIRST_SEEN:- return parse_expr("min(timestamp)")- else: # LAST_SEEN- return parse_expr("max(timestamp)")+ return self._retention_query_builder().build_start_event_timestamp_expr()def _get_retention_window_truncation_expr(self, timestamp_expr: ast.Expr) -> ast.Expr:"""@@ -1678,98 +1541,25 @@ class ExperimentQueryBuilder:This ensures [7,7] day window means "any time on day 7" rather than"exactly 7*24 hours after start event to the second"."""- assert isinstance(self.metric, ExperimentRetentionMetric)-- # Only truncate DAY and HOUR units for intuitive behavior- unit_to_interval_name = {- FunnelConversionWindowTimeUnit.DAY: "day",- FunnelConversionWindowTimeUnit.HOUR: "hour",- }-- interval_name = unit_to_interval_name.get(self.metric.retention_window_unit)- if interval_name is None:- return timestamp_expr-- return get_start_of_interval_hogql(interval=interval_name, team=self.team, source=timestamp_expr)+ return self._retention_query_builder().get_retention_window_truncation_expr(timestamp_expr)def _build_retention_window_interval(self, window_value: int) -> ast.Expr:"""Converts retention window value to ClickHouse interval expression."""- assert isinstance(self.metric, ExperimentRetentionMetric)-- unit_map = {- FunnelConversionWindowTimeUnit.SECOND: "Second",- FunnelConversionWindowTimeUnit.MINUTE: "Minute",- FunnelConversionWindowTimeUnit.HOUR: "Hour",- FunnelConversionWindowTimeUnit.DAY: "Day",- FunnelConversionWindowTimeUnit.WEEK: "Week",- FunnelConversionWindowTimeUnit.MONTH: "Month",- }- unit = unit_map[self.metric.retention_window_unit]- return parse_expr(- f"toInterval{unit}({{value}})",- placeholders={"value": ast.Constant(value=window_value)},- )+ return self._retention_query_builder().build_retention_window_interval(window_value)def _build_start_event_predicate(self) -> ast.Expr:"""Builds the predicate for filtering start events."""- assert isinstance(self.metric, ExperimentRetentionMetric)-- if isinstance(self.metric.start_event, ExperimentDataWarehouseNode):- event_filter = data_warehouse_node_to_filter(self.team, self.metric.start_event)- else:- event_filter = event_or_action_to_filter(self.team, self.metric.start_event)- conversion_window_seconds = self._get_conversion_window_seconds()-- return parse_expr(- """- timestamp >= {date_from}- AND timestamp < {date_to} + toIntervalSecond({conversion_window_seconds})- AND {event_filter}- """,- placeholders={- "date_from": self.date_range_query.date_from_as_hogql(),- "date_to": self.date_range_query.date_to_as_hogql(),- "conversion_window_seconds": ast.Constant(value=conversion_window_seconds),- "event_filter": event_filter,- },- )+ return self._retention_query_builder().build_start_event_predicate()def _build_completion_event_predicate(self) -> ast.Expr:"""Builds the predicate for filtering completion events."""- assert isinstance(self.metric, ExperimentRetentionMetric)-- if isinstance(self.metric.completion_event, ExperimentDataWarehouseNode):- event_filter = data_warehouse_node_to_filter(self.team, self.metric.completion_event)- else:- event_filter = event_or_action_to_filter(self.team, self.metric.completion_event)-- # Completion events can occur within the retention window after the start event- # The retention window end could extend beyond the experiment end date- conversion_window_seconds = self._get_conversion_window_seconds()- retention_window_end_seconds = conversion_window_to_seconds(- self.metric.retention_window_end,- self.metric.retention_window_unit,- )-- return parse_expr(- """- timestamp >= {date_from}- AND timestamp < {date_to} + toIntervalSecond({total_window_seconds})- AND {event_filter}- """,- placeholders={- "date_from": self.date_range_query.date_from_as_hogql(),- "date_to": self.date_range_query.date_to_as_hogql(),- "total_window_seconds": ast.Constant(value=conversion_window_seconds + retention_window_end_seconds),- "event_filter": event_filter,- },- )+ return self._retention_query_builder().build_completion_event_predicate()def _build_start_after_exposure_predicate(self) -> ast.Expr:"""@@ -1777,19 +1567,7 @@ class ExperimentQueryBuilder:Applied inside the start_events CTE (pre-aggregation) so that min/max onlyconsiders events after the user's first exposure."""- conversion_window_seconds = self._get_conversion_window_seconds()- if conversion_window_seconds > 0:- return parse_expr(- """- timestamp >= exposures.first_exposure_time- AND timestamp <= exposures.first_exposure_time + toIntervalSecond({conversion_window_seconds})- """,- placeholders={- "conversion_window_seconds": ast.Constant(value=conversion_window_seconds),- },- )- else:- return parse_expr("timestamp >= exposures.first_exposure_time")+ return self._retention_query_builder().build_start_after_exposure_predicate()def _build_completion_retention_window_predicate(self) -> ast.Expr:"""@@ -1803,32 +1581,4 @@ class ExperimentQueryBuilder:for the truncation window. This ensures that same-period retention (e.g., [0,0])captures all events within that period, not just events at the exact same second."""- assert isinstance(self.metric, ExperimentRetentionMetric)-- retention_window_end_seconds = conversion_window_to_seconds(- self.metric.retention_window_end,- self.metric.retention_window_unit,- )-- # For DAY/HOUR units, add a buffer to account for truncation- # This ensures same-period retention windows work correctly- truncation_buffer = 0- if self.metric.retention_window_unit == FunnelConversionWindowTimeUnit.DAY:- # For DAY units, allow completions within the same day (24 hours)- truncation_buffer = 86400 # 1 day in seconds- elif self.metric.retention_window_unit == FunnelConversionWindowTimeUnit.HOUR:- # For HOUR units, allow completions within the same hour- truncation_buffer = 3600 # 1 hour in seconds-- # Add buffer to retention window end- buffered_window_end_seconds = retention_window_end_seconds + truncation_buffer-- return parse_expr(- """- completion_events.completion_timestamp >= start_events.start_timestamp- AND completion_events.completion_timestamp <= start_events.start_timestamp + toIntervalSecond({retention_window_end_seconds})- """,- placeholders={- "retention_window_end_seconds": ast.Constant(value=buffered_window_end_seconds),- },- )+ return self._retention_query_builder().build_completion_retention_window_predicate()diff --git a/posthog/hogql_queries/experiments/experiment_retention_query_builder.py b/posthog/hogql_queries/experiments/experiment_retention_query_builder.pynew file mode 100644index 000000000000..cb43e5d37ff7--- /dev/null+++ b/posthog/hogql_queries/experiments/experiment_retention_query_builder.py@@ -0,0 +1,405 @@+from typing import TYPE_CHECKING, Optional++from django.utils import timezone++from posthog.schema import (+ ExperimentDataWarehouseNode,+ ExperimentRetentionMetric,+ FunnelConversionWindowTimeUnit,+ StartHandling,+)++from posthog.hogql import ast+from posthog.hogql.parser import parse_expr, parse_select++from posthog.hogql_queries.experiments.base_query_utils import (+ conversion_window_to_seconds,+ data_warehouse_node_to_filter,+ event_or_action_to_filter,+)+from posthog.hogql_queries.insights.utils.utils import get_start_of_interval_hogql++if TYPE_CHECKING:+ from posthog.hogql_queries.experiments.experiment_query_builder import ExperimentQueryBuilder+++class RetentionQueryBuilder:+ """+ Builds retention-metric queries.++ Retention reuses the shared exposure and conversion-window helpers already+ extracted from the experiment query builder, but has its own maturity+ semantics anchored on the start_event. To keep the move behavior-preserving,+ this class holds a reference to the owning ``ExperimentQueryBuilder`` and+ reaches through it for shared state (the metric, team, date range, entity+ key, breakdown injector) and the cross-cluster exposure and+ conversion-window helpers.+ """++ def __init__(self, builder: "ExperimentQueryBuilder"):+ self._b = builder++ def get_retention_maturity_seconds(self) -> int:+ """+ Returns the maturity window in seconds for retention metrics.+ Equals retention_window_end converted to seconds; conversion_window does+ not contribute because retention maturity is anchored on start_event.+ """+ assert isinstance(self._b.metric, ExperimentRetentionMetric)+ return conversion_window_to_seconds(+ self._b.metric.retention_window_end,+ self._b.metric.retention_window_unit,+ )++ def build_retention_maturity_having_clause(self) -> Optional[ast.Expr]:+ """+ Returns a HAVING clause for the retention query's start_events CTE that+ filters out users whose retention window has not yet fully elapsed since+ their start_event.++ Anchored on the user's start_event timestamp (min or max of start event+ timestamps, depending on start_handling).+ """+ if not isinstance(self._b.metric, ExperimentRetentionMetric):+ return None+ if not self._b.only_count_matured_users:+ return None++ maturity_seconds = self.get_retention_maturity_seconds()+ if maturity_seconds == 0:+ return None++ now = timezone.now().strftime("%Y-%m-%d %H:%M:%S")+ start_timestamp_expr = self.build_start_event_timestamp_expr()++ return parse_expr(+ "{start_ts} + toIntervalSecond({maturity_seconds}) <= toDateTime({now}, 'UTC')",+ placeholders={+ "start_ts": start_timestamp_expr,+ "maturity_seconds": ast.Constant(value=maturity_seconds),+ "now": ast.Constant(value=now),+ },+ )++ def build_retention_query(self) -> ast.SelectQuery:+ """+ Builds query for retention metrics.++ Retention measures the proportion of users who performed a "completion event"+ within a specified time window after performing a "start event".++ Statistical Treatment:+ This metric is treated as a ratio metric using RatioStatistic. Each entity has:+ - Numerator value: 1 if completed within retention window, 0 otherwise+ - Denominator value: 1 (they performed the start event)++ Unlike standard proportion tests (where sample size is fixed), retention metrics+ have a random denominator (count of users who started). This makes retention a+ ratio of two random variables, requiring delta method variance.++ Returns 7 fields for RatioStatistic:+ - Standard: num_users, total_sum, total_sum_of_squares+ - Ratio-specific: denominator_sum, denominator_sum_squares, numerator_denominator_sum_product++ The collected statistics are processed using RatioStatistic (not ProportionStatistic)+ for both frequentist and Bayesian analysis.++ Structure:+ - exposures: all exposures with variant assignment+ - start_events: when each entity performed the start_event (with start_handling logic)+ - completion_events: when each entity performed the completion_event+ - entity_metrics: join exposures + start_events + completion_events+ Calculate retention per entity (1 if retained, 0 if not)+ - Final SELECT: aggregated statistics per variant++ Key Design Decision:+ Uses INNER JOIN between exposures and start_events, meaning only users who+ performed the start event are included in the retention calculation. This+ measures "Of users who did X, how many came back to do Y?" rather than+ "Of all exposed users, how many did X and then Y?"+ """+ assert isinstance(self._b.metric, ExperimentRetentionMetric)++ # Build the CTEs+ common_ctes = """+ exposures AS (+ {exposure_select_query}+ ),++ start_events AS (+ SELECT+ exposures.entity_id AS entity_id,+ {start_timestamp_expr} AS start_timestamp+ FROM events+ INNER JOIN exposures ON {entity_key} = exposures.entity_id+ WHERE {start_event_predicate}+ AND {start_after_exposure_predicate}+ GROUP BY exposures.entity_id+ ),++ completion_events AS (+ SELECT+ {entity_key} AS entity_id,+ timestamp AS completion_timestamp+ FROM events+ WHERE {completion_event_predicate}+ ),++ entity_metrics AS (+ SELECT+ exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ MAX(if(+ completion_events.completion_timestamp IS NOT NULL+ AND {truncated_completion_timestamp} >= {truncated_start_timestamp} + {retention_window_start_interval}+ AND {truncated_completion_timestamp} <= {truncated_start_timestamp} + {retention_window_end_interval},+ 1,+ 0+ )) AS value+ FROM exposures+ INNER JOIN start_events+ ON exposures.entity_id = start_events.entity_id+ LEFT JOIN completion_events+ ON exposures.entity_id = completion_events.entity_id+ AND {completion_retention_window_predicate}+ GROUP BY exposures.entity_id, exposures.variant+ )+ """++ placeholders = {+ "exposure_select_query": self._b._get_exposure_query(),+ "entity_key": parse_expr(self._b.entity_key),+ "start_timestamp_expr": self.build_start_event_timestamp_expr(),+ "start_event_predicate": self.build_start_event_predicate(),+ "completion_event_predicate": self.build_completion_event_predicate(),+ "retention_window_start_interval": self.build_retention_window_interval(+ self._b.metric.retention_window_start+ ),+ "retention_window_end_interval": self.build_retention_window_interval(self._b.metric.retention_window_end),+ "start_after_exposure_predicate": self.build_start_after_exposure_predicate(),+ "completion_retention_window_predicate": self.build_completion_retention_window_predicate(),+ "truncated_start_timestamp": self.get_retention_window_truncation_expr(+ parse_expr("start_events.start_timestamp")+ ),+ "truncated_completion_timestamp": self.get_retention_window_truncation_expr(+ parse_expr("completion_events.completion_timestamp")+ ),+ }++ query = parse_select(+ f"""+ WITH {common_ctes}++ SELECT+ entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ """,+ placeholders=placeholders,+ )++ assert isinstance(query, ast.SelectQuery)++ # Inject maturity HAVING clause into the start_events CTE, anchored on+ # the user's start_event timestamp so users whose retention window has+ # not yet elapsed are excluded from the denominator.+ retention_maturity = self.build_retention_maturity_having_clause()+ if retention_maturity is not None and query.ctes and "start_events" in query.ctes:+ start_events_cte = query.ctes["start_events"]+ if isinstance(start_events_cte, ast.CTE) and isinstance(start_events_cte.expr, ast.SelectQuery):+ if start_events_cte.expr.having is None:+ start_events_cte.expr.having = retention_maturity+ else:+ start_events_cte.expr.having = ast.And(exprs=[start_events_cte.expr.having, retention_maturity])++ # Inject breakdown columns if breakdown filter is present+ if self._b.breakdown_injector:+ self._b.breakdown_injector.inject_retention_breakdown_columns(query)++ return query++ def build_start_event_timestamp_expr(self) -> ast.Expr:+ """+ Returns expression to get start event timestamp based on start_handling.+ FIRST_SEEN: Use the first occurrence of start event+ LAST_SEEN: Use the last occurrence of start event+ """+ assert isinstance(self._b.metric, ExperimentRetentionMetric)++ if self._b.metric.start_handling == StartHandling.FIRST_SEEN:+ return parse_expr("min(timestamp)")+ else: # LAST_SEEN+ return parse_expr("max(timestamp)")++ def get_retention_window_truncation_expr(self, timestamp_expr: ast.Expr) -> ast.Expr:+ """+ Returns truncated timestamp expression for retention window comparisons.++ For DAY: returns toStartOfDay(timestamp)+ For HOUR: returns toStartOfHour(timestamp)+ For other units: returns timestamp unchanged++ This ensures [7,7] day window means "any time on day 7" rather than+ "exactly 7*24 hours after start event to the second".+ """+ assert isinstance(self._b.metric, ExperimentRetentionMetric)++ # Only truncate DAY and HOUR units for intuitive behavior+ unit_to_interval_name = {+ FunnelConversionWindowTimeUnit.DAY: "day",+ FunnelConversionWindowTimeUnit.HOUR: "hour",+ }++ interval_name = unit_to_interval_name.get(self._b.metric.retention_window_unit)+ if interval_name is None:+ return timestamp_expr++ return get_start_of_interval_hogql(interval=interval_name, team=self._b.team, source=timestamp_expr)++ def build_retention_window_interval(self, window_value: int) -> ast.Expr:+ """+ Converts retention window value to ClickHouse interval expression.+ """+ assert isinstance(self._b.metric, ExperimentRetentionMetric)++ unit_map = {+ FunnelConversionWindowTimeUnit.SECOND: "Second",+ FunnelConversionWindowTimeUnit.MINUTE: "Minute",+ FunnelConversionWindowTimeUnit.HOUR: "Hour",+ FunnelConversionWindowTimeUnit.DAY: "Day",+ FunnelConversionWindowTimeUnit.WEEK: "Week",+ FunnelConversionWindowTimeUnit.MONTH: "Month",+ }+ unit = unit_map[self._b.metric.retention_window_unit]+ return parse_expr(+ f"toInterval{unit}({{value}})",+ placeholders={"value": ast.Constant(value=window_value)},+ )++ def build_start_event_predicate(self) -> ast.Expr:+ """+ Builds the predicate for filtering start events.+ """+ assert isinstance(self._b.metric, ExperimentRetentionMetric)++ if isinstance(self._b.metric.start_event, ExperimentDataWarehouseNode):+ event_filter = data_warehouse_node_to_filter(self._b.team, self._b.metric.start_event)+ else:+ event_filter = event_or_action_to_filter(self._b.team, self._b.metric.start_event)+ conversion_window_seconds = self._b._get_conversion_window_seconds()++ return parse_expr(+ """+ timestamp >= {date_from}+ AND timestamp < {date_to} + toIntervalSecond({conversion_window_seconds})+ AND {event_filter}+ """,+ placeholders={+ "date_from": self._b.date_range_query.date_from_as_hogql(),+ "date_to": self._b.date_range_query.date_to_as_hogql(),+ "conversion_window_seconds": ast.Constant(value=conversion_window_seconds),+ "event_filter": event_filter,+ },+ )++ def build_completion_event_predicate(self) -> ast.Expr:+ """+ Builds the predicate for filtering completion events.+ """+ assert isinstance(self._b.metric, ExperimentRetentionMetric)++ if isinstance(self._b.metric.completion_event, ExperimentDataWarehouseNode):+ event_filter = data_warehouse_node_to_filter(self._b.team, self._b.metric.completion_event)+ else:+ event_filter = event_or_action_to_filter(self._b.team, self._b.metric.completion_event)++ # Completion events can occur within the retention window after the start event+ # The retention window end could extend beyond the experiment end date+ conversion_window_seconds = self._b._get_conversion_window_seconds()+ retention_window_end_seconds = conversion_window_to_seconds(+ self._b.metric.retention_window_end,+ self._b.metric.retention_window_unit,+ )++ return parse_expr(+ """+ timestamp >= {date_from}+ AND timestamp < {date_to} + toIntervalSecond({total_window_seconds})+ AND {event_filter}+ """,+ placeholders={+ "date_from": self._b.date_range_query.date_from_as_hogql(),+ "date_to": self._b.date_range_query.date_to_as_hogql(),+ "total_window_seconds": ast.Constant(value=conversion_window_seconds + retention_window_end_seconds),+ "event_filter": event_filter,+ },+ )++ def build_start_after_exposure_predicate(self) -> ast.Expr:+ """+ Builds the predicate for filtering start events to only those after exposure.+ Applied inside the start_events CTE (pre-aggregation) so that min/max only+ considers events after the user's first exposure.+ """+ conversion_window_seconds = self._b._get_conversion_window_seconds()+ if conversion_window_seconds > 0:+ return parse_expr(+ """+ timestamp >= exposures.first_exposure_time+ AND timestamp <= exposures.first_exposure_time + toIntervalSecond({conversion_window_seconds})+ """,+ placeholders={+ "conversion_window_seconds": ast.Constant(value=conversion_window_seconds),+ },+ )+ else:+ return parse_expr("timestamp >= exposures.first_exposure_time")++ def build_completion_retention_window_predicate(self) -> ast.Expr:+ """+ Builds the predicate for the join condition ensuring completion events+ are within a reasonable timeframe relative to start events.++ This is a performance optimization - we'll do the exact retention window+ calculation in the entity_metrics CTE.++ For DAY/HOUR units that use timestamp truncation, we add a buffer to account+ for the truncation window. This ensures that same-period retention (e.g., [0,0])+ captures all events within that period, not just events at the exact same second.+ """+ assert isinstance(self._b.metric, ExperimentRetentionMetric)++ retention_window_end_seconds = conversion_window_to_seconds(+ self._b.metric.retention_window_end,+ self._b.metric.retention_window_unit,+ )++ # For DAY/HOUR units, add a buffer to account for truncation+ # This ensures same-period retention windows work correctly+ truncation_buffer = 0+ if self._b.metric.retention_window_unit == FunnelConversionWindowTimeUnit.DAY:+ # For DAY units, allow completions within the same day (24 hours)+ truncation_buffer = 86400 # 1 day in seconds+ elif self._b.metric.retention_window_unit == FunnelConversionWindowTimeUnit.HOUR:+ # For HOUR units, allow completions within the same hour+ truncation_buffer = 3600 # 1 hour in seconds++ # Add buffer to retention window end+ buffered_window_end_seconds = retention_window_end_seconds + truncation_buffer++ return parse_expr(+ """+ completion_events.completion_timestamp >= start_events.start_timestamp+ AND completion_events.completion_timestamp <= start_events.start_timestamp + toIntervalSecond({retention_window_end_seconds})+ """,+ placeholders={+ "retention_window_end_seconds": ast.Constant(value=buffered_window_end_seconds),+ },+ )diff --git a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambrindex 800a3fff4b5e..4d81dc6a2021 100644--- a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr+++ b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr@@ -1122,7 +1122,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2020-12-29 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,(SELECT nullIf(nullIf(events.`$session_id`, ''), 'null') AS session_idFROM events- WHERE and(equals(events.team_id, 99999), notEmpty(nullIf(nullIf(events.`$session_id`, ''), 'null')), lessOrEquals(events.timestamp, now64(6, 'UTC')), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, 'email'), ''), 'null'), '^"|"$', ''), 'bla'), 0), greaterOrEquals(events.timestamp, toDateTime64('2020-12-28 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(nullIf(nullIf(events.`$session_id`, ''), 'null')), lessOrEquals(events.timestamp, now64(6, 'UTC')), equals(events.mat_pp_email, 'bla'), greaterOrEquals(events.timestamp, toDateTime64('2020-12-28 00:00:00.000000', 6, 'UTC')))GROUP BY nullIf(nullIf(events.`$session_id`, ''), 'null')HAVING 1ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESC