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.

chore(experiments): share data-warehouse metric check between runner and canary (#113397)

Juraj Majerike6c75b881919
diff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.py
index 0001727dfe4a..df74bfcf0c82 100644
--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py
+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py
@@ -16,7 +16,6 @@ from posthog.schema import (
EventsNode,
ExperimentActorsQuery,
ExperimentBreakdownResult,
- ExperimentDataWarehouseNode,
ExperimentFunnelMetric,
ExperimentMeanMetric,
ExperimentMetricMathType,
@@ -82,6 +81,7 @@ from products.experiments.backend.hogql_queries.utils import (
get_variant_results,
split_baseline_and_test_variants,
)
+from products.experiments.backend.metric_resolution import metric_reads_data_warehouse
from products.experiments.backend.metric_utils import get_default_metric_title
from products.experiments.backend.models.experiment import Experiment
from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig
@@ -357,20 +357,7 @@ class ExperimentQueryRunner(ExperimentResultsCacheMixin, QueryRunner):
interval=IntervalType.DAY,
now=datetime.now(),
)
- if isinstance(self.query.metric, ExperimentMeanMetric):
- self.is_data_warehouse_query = self.query.metric.source.kind == "ExperimentDataWarehouseNode"
- elif isinstance(self.query.metric, ExperimentFunnelMetric):
- self.is_data_warehouse_query = any(
- isinstance(step, ExperimentDataWarehouseNode) for step in self.query.metric.series
- )
- elif isinstance(self.query.metric, ExperimentRatioMetric):
- numerator_is_dw = isinstance(self.query.metric.numerator, ExperimentDataWarehouseNode)
- denominator_is_dw = isinstance(self.query.metric.denominator, ExperimentDataWarehouseNode)
- self.is_data_warehouse_query = numerator_is_dw or denominator_is_dw
- elif isinstance(self.query.metric, ExperimentRetentionMetric):
- start_is_dw = isinstance(self.query.metric.start_event, ExperimentDataWarehouseNode)
- completion_is_dw = isinstance(self.query.metric.completion_event, ExperimentDataWarehouseNode)
- self.is_data_warehouse_query = start_is_dw or completion_is_dw
+ self.is_data_warehouse_query = metric_reads_data_warehouse(self.query.metric)
self.stats_method = get_experiment_stats_method(self.experiment)
diff --git a/products/experiments/backend/metric_resolution.py b/products/experiments/backend/metric_resolution.py
index b10a865b24f3..50336aae0a2f 100644
--- a/products/experiments/backend/metric_resolution.py
+++ b/products/experiments/backend/metric_resolution.py
@@ -8,6 +8,7 @@ uuids between activities and re-resolve the definition at the point of use.
from typing import Any, Literal
from posthog.schema import (
+ ExperimentDataWarehouseNode,
ExperimentFunnelMetric,
ExperimentMeanMetric,
ExperimentRatioMetric,
@@ -183,3 +184,34 @@ def find_metric_dict(experiment: Experiment, metric_uuid: str) -> dict[str, Any]
def build_metric(metric_dict: dict[str, Any]) -> ExperimentMetric:
return METRIC_BUILDERS[metric_dict["metric_type"]](**metric_dict)
+
+
+# A metric reads a data warehouse table when any of its source nodes is an ExperimentDataWarehouseNode.
+# Such metrics never precompute (the precomputed table lacks the join keys). The source-bearing fields
+# are the same across the two readers below: source, series[*], numerator, denominator, start_event,
+# completion_event. Keep the pair in step — one reads the typed metric, the other a raw saved definition.
+def metric_reads_data_warehouse(metric: ExperimentMetric) -> bool:
+ """Typed check, for callers that already hold a built metric (the query runner)."""
+ if isinstance(metric, ExperimentMeanMetric):
+ nodes: list[Any] = [metric.source]
+ elif isinstance(metric, ExperimentFunnelMetric):
+ nodes = list(metric.series)
+ elif isinstance(metric, ExperimentRatioMetric):
+ nodes = [metric.numerator, metric.denominator]
+ else:
+ nodes = [metric.start_event, metric.completion_event]
+ return any(isinstance(node, ExperimentDataWarehouseNode) for node in nodes)
+
+
+def metric_dict_reads_data_warehouse(metric_dict: dict[str, Any]) -> bool:
+ """Dict check, for callers that only hold a saved definition they may not be able to build (canary
+ sampling). A field absent on a shape is simply missing from the dict."""
+ nodes = [
+ metric_dict.get("source"),
+ *(metric_dict.get("series") or []),
+ metric_dict.get("numerator"),
+ metric_dict.get("denominator"),
+ metric_dict.get("start_event"),
+ metric_dict.get("completion_event"),
+ ]
+ return any(isinstance(node, dict) and node.get("kind") == "ExperimentDataWarehouseNode" for node in nodes)
diff --git a/products/experiments/backend/temporal/canary_logic.py b/products/experiments/backend/temporal/canary_logic.py
index 32b8d9a3ee9b..354b41e15887 100644
--- a/products/experiments/backend/temporal/canary_logic.py
+++ b/products/experiments/backend/temporal/canary_logic.py
@@ -61,6 +61,7 @@ from products.experiments.backend.metric_resolution import (
ExperimentMetric,
build_metric,
find_metric_dict,
+ metric_dict_reads_data_warehouse,
scheduled_metric_definitions,
)
from products.experiments.backend.models.experiment import Experiment
@@ -159,21 +160,6 @@ def _experiment_metric_dicts(experiment: Experiment) -> list[dict[str, Any]]:
]
-def _uses_data_warehouse(metric_dict: dict[str, Any]) -> bool:
- """True when any side of the metric reads a data warehouse table. These metrics never precompute
- (the precomputed table lacks the join keys), so sampling one buys a guaranteed path flip. The
- fields cover every metric shape; a field absent on a shape is simply None."""
- nodes = [
- metric_dict.get("source"),
- *(metric_dict.get("series") or []),
- metric_dict.get("numerator"),
- metric_dict.get("denominator"),
- metric_dict.get("start_event"),
- metric_dict.get("completion_event"),
- ]
- return any(isinstance(node, dict) and node.get("kind") == "ExperimentDataWarehouseNode" for node in nodes)
-
-
def _forensics_targets(experiment_id: int, metric_uuids: list[str] | None) -> list[CanaryMetricTarget]:
"""On-demand mode: all eligible metrics of one experiment, quotas and runtime gates ignored."""
experiment = (
@@ -236,7 +222,11 @@ def sample_canary_targets_sync(inputs: ExperimentPrecomputeCanaryInputs) -> list
break
metric_type = metric_dict["metric_type"]
metric_uuid = metric_dict["uuid"]
- if quotas.get(metric_type, 0) <= 0 or metric_uuid in seen_uuids or _uses_data_warehouse(metric_dict):
+ if (
+ quotas.get(metric_type, 0) <= 0
+ or metric_uuid in seen_uuids
+ or metric_dict_reads_data_warehouse(metric_dict)
+ ):
continue
seen_uuids.add(metric_uuid)
targets.append(