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): save warehouse-native sources as drafts (#129128)

Juraj Majerik08f859884ae4
diff --git a/products/experiments/backend/experiment_saved_metric_service.py b/products/experiments/backend/experiment_saved_metric_service.py
index 925521e69542..1fcfc363b3e5 100644
--- a/products/experiments/backend/experiment_saved_metric_service.py
+++ b/products/experiments/backend/experiment_saved_metric_service.py
@@ -17,6 +17,10 @@ from products.experiments.backend.metric_validation import (
)
from products.experiments.backend.models.experiment import ExperimentSavedMetric, saved_metric_has_legacy_query
from products.experiments.backend.warehouse_access_control import validate_warehouse_metrics
+from products.experiments.backend.warehouse_native_sources import (
+ validate_metric_warehouse_native_sources,
+ validate_saved_metric_query_against_linked_experiments,
+)
class ExperimentSavedMetricService:
@@ -45,6 +49,7 @@ class ExperimentSavedMetricService:
normalized_query = self.normalize_query_for_write(query)
validate_metric_action_ids([normalized_query], self.team, user=self.user)
validate_warehouse_metrics([normalized_query], team=self.team, user=self.user)
+ validate_metric_warehouse_native_sources([normalized_query], team=self.team, user=self.user)
return ExperimentSavedMetric.objects.create(
team=self.team,
@@ -69,6 +74,10 @@ class ExperimentSavedMetricService:
[update_data["query"]], self.team, user=self.user, known_action_ids=stored_action_ids
)
validate_warehouse_metrics([update_data["query"]], team=self.team, user=self.user)
+ validate_saved_metric_query_against_linked_experiments(
+ saved_metric, update_data["query"], team=self.team, user=self.user
+ )
+ validate_metric_warehouse_native_sources([update_data["query"]], team=self.team, user=self.user)
for attr, value in update_data.items():
setattr(saved_metric, attr, value)
diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.py
index 3028bd148133..4b6b3937657c 100644
--- a/products/experiments/backend/experiment_service.py
+++ b/products/experiments/backend/experiment_service.py
@@ -122,6 +122,10 @@ from products.experiments.backend.models.experiment import (
from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig
from products.experiments.backend.result_serialization import strip_step_sessions
from products.experiments.backend.warehouse_access_control import validate_warehouse_metrics
+from products.experiments.backend.warehouse_native_sources import (
+ validate_experiment_warehouse_native_sources,
+ warehouse_native_exposure_config,
+)
from products.feature_flags.backend.api.feature_flag import parse_created_by_ids
from products.feature_flags.backend.facade.api import (
archive_flag,
@@ -249,6 +253,9 @@ FREEZE_EXPOSURE_SNAPSHOT_NAME_PREFIX = "Exposure snapshot for experiment "
# Auto-saved sample-size estimate, not a user edit: skip the "experiment updated" event.
RUNNING_TIME_ONLY_CHANGED_FIELDS = ["running_time_calculation"]
+# The update fields that can change which data sources an experiment reads.
+_WAREHOUSE_NATIVE_SOURCE_FIELDS = frozenset({"exposure_criteria", "metrics", "metrics_secondary", "saved_metrics_ids"})
+
_INLINE_METRIC_FIELD_ROLES: tuple[tuple[str, MetricRole], ...] = (
("metrics", "primary"),
("metrics_secondary", "secondary"),
@@ -841,11 +848,10 @@ class ExperimentService:
# to mirror the pydantic Literal default on that model.
kind = exposure_config.get("kind", "ExperimentEventExposureConfig")
if kind == "ExperimentWarehouseNativeExposureConfig" and warehouse_native_allowed:
- # The kind exists so the rest of the product can be built against it; nothing can run it yet.
cls._validate_exposure_config_model(
ExperimentWarehouseNativeExposureConfig, exposure_config, field_path, kind
)
- raise ValidationError(f"{field_path}: warehouse-native exposures are not supported yet.")
+ return
if kind not in cls.EXPOSURE_CONFIG_KINDS:
raise ValidationError(
f"{field_path}.kind must be one of "
@@ -1238,9 +1244,21 @@ class ExperimentService:
team=self.team,
user=self.user,
)
+ validate_experiment_warehouse_native_sources(
+ exposure_criteria=exposure_criteria,
+ metrics=[
+ *(metrics or []),
+ *(metrics_secondary or []),
+ *self._collect_saved_metric_queries(saved_metrics_ids),
+ ],
+ team=self.team,
+ user=self.user,
+ )
self.validate_saved_metrics_ids(saved_metrics_ids, self.team.id)
is_draft = start_date is None
self._reject_live_create_under_launch_policy(is_draft)
+ if not is_draft:
+ self._reject_warehouse_native_launch(exposure_criteria)
feature_flag, used_variants = self._ensure_feature_flag(
feature_flag_key=feature_flag_key,
@@ -1686,6 +1704,13 @@ class ExperimentService:
if feature_flag.deleted:
raise ValidationError("Experiment cannot be launched because its feature flag has been deleted.")
+ @staticmethod
+ def _reject_warehouse_native_launch(exposure_criteria: dict | None) -> None:
+ """Nothing runs a warehouse-native query yet, so a launched experiment would only fail every
+ calculation. The exposure stands for the experiment: a warehouse-native experiment always has one."""
+ if warehouse_native_exposure_config(exposure_criteria) is not None:
+ raise ValidationError("Warehouse-native sources cannot run yet. Use them on a draft experiment only.")
+
def _validate_flag_for_launch(self, experiment: Experiment, feature_flag: FeatureFlag) -> None:
"""Flag guards shared by launch_experiment and the PATCH(start_date) launch path.
@@ -1756,6 +1781,14 @@ class ExperimentService:
seen.add(uuid)
return seen
+ @staticmethod
+ def _linked_saved_metric_queries(experiment: Experiment) -> list[dict]:
+ return [
+ link.saved_metric.query
+ for link in experiment.experimenttosavedmetric_set.select_related("saved_metric")
+ if link.saved_metric.query
+ ]
+
def _collect_saved_metric_queries(self, saved_metrics_ids: list | None) -> list[dict]:
"""Query definitions of the attached saved metrics, so their tables get the same warehouse
access check as inline metrics."""
@@ -1863,6 +1896,7 @@ class ExperimentService:
"""Launch a draft experiment: validate readiness, set start_date, activate feature flag."""
if not experiment.is_draft:
raise ValidationError("Experiment has already been launched.")
+ self._reject_warehouse_native_launch(experiment.exposure_criteria)
# Validate feature flag configuration
feature_flag = experiment.feature_flag
@@ -3568,6 +3602,28 @@ class ExperimentService:
team=self.team,
user=self.user,
)
+ if _WAREHOUSE_NATIVE_SOURCE_FIELDS & update_data.keys():
+ # The rule spans the exposure and every metric, so the fields left out of the update are read
+ # from the experiment. An update that touches none of them skips the check, so a connection
+ # removed later does not block an unrelated edit.
+ if "saved_metrics_ids" in update_data:
+ saved_metric_queries = self._collect_saved_metric_queries(update_data["saved_metrics_ids"])
+ else:
+ saved_metric_queries = self._linked_saved_metric_queries(experiment)
+ validate_experiment_warehouse_native_sources(
+ exposure_criteria=update_data.get("exposure_criteria", experiment.exposure_criteria),
+ metrics=[
+ *(update_data.get("metrics", experiment.metrics) or []),
+ *(update_data.get("metrics_secondary", experiment.metrics_secondary) or []),
+ *saved_metric_queries,
+ ],
+ team=self.team,
+ user=self.user,
+ )
+ if "start_date" in update_data or _WAREHOUSE_NATIVE_SOURCE_FIELDS & update_data.keys():
+ # Covers a draft that launches and a launched experiment that switches its sources.
+ if update_data.get("start_date", experiment.start_date) is not None:
+ self._reject_warehouse_native_launch(update_data.get("exposure_criteria", experiment.exposure_criteria))
context = serializer_context or self._build_serializer_context()
feature_flag = experiment.feature_flag
diff --git a/products/experiments/backend/health/context.py b/products/experiments/backend/health/context.py
index b45c7969e9a4..37b1cd7f61f2 100644
--- a/products/experiments/backend/health/context.py
+++ b/products/experiments/backend/health/context.py
@@ -16,6 +16,7 @@ from posthog.schema import (
CohortPropertyFilter,
EventPropertyFilter,
ExperimentEventExposureConfig,
+ ExperimentWarehouseNativeExposureConfig,
MultipleVariantHandling,
PersonPropertyFilter,
PropertyOperator,
@@ -826,6 +827,9 @@ def _exposure_events(experiment: Experiment) -> ExposureEvents | None:
criteria = normalize_to_exposure_criteria(experiment.exposure_criteria)
except pydantic.ValidationError:
return None
+ if criteria is not None and isinstance(criteria.exposure_config, ExperimentWarehouseNativeExposureConfig):
+ # The exposures live in the customer's warehouse, so no event of this project describes them.
+ return None
# A deleted flag carries a tombstone in its key, and the exposure query reads the key without it.
flag_key = experiment.feature_flag.key_without_tombstone() if experiment.feature_flag_id else None
event, variant_property = get_exposure_event_and_property(
diff --git a/products/experiments/backend/health/metric_compiles.py b/products/experiments/backend/health/metric_compiles.py
index 5555d59b0ad5..910fe88d7e48 100644
--- a/products/experiments/backend/health/metric_compiles.py
+++ b/products/experiments/backend/health/metric_compiles.py
@@ -53,7 +53,7 @@ from products.experiments.backend.metric_calculation.config import (
calculation_metric_name,
team_experiments_configs,
)
-from products.experiments.backend.metric_resolution import build_metric
+from products.experiments.backend.metric_resolution import build_metric, metric_uses_warehouse_native_source
from products.experiments.backend.models.experiment import Experiment
from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig
@@ -184,6 +184,9 @@ class _MetricCompiler:
metric = build_metric(definition)
except pydantic.ValidationError as error:
return _failure(ExperimentHealthMetricCompileErrorKind.DEFINITION, _definition_message(error))
+ if metric_uses_warehouse_native_source(metric):
+ # No query runs in the customer's warehouse yet, so the compile would report a defect of PostHog.
+ return _Outcome.UNKNOWN
runner = ExperimentQueryRunner(
query=ExperimentQuery(experiment_id=self.experiment.id, metric=metric),
team=self.experiment.team,
diff --git a/products/experiments/backend/health/test/test_metric_cannot_compile.py b/products/experiments/backend/health/test/test_metric_cannot_compile.py
index 8bfacf9de9c0..dcf55a30d5fb 100644
--- a/products/experiments/backend/health/test/test_metric_cannot_compile.py
+++ b/products/experiments/backend/health/test/test_metric_cannot_compile.py
@@ -276,6 +276,33 @@ class TestMetricCannotCompile(BaseTest):
InternalHogQLError("Unexpected node"),
(NOT_CHECKED, "Couldn't check 1 metric"),
),
+ (
+ "warehouse_native_sources_that_nothing_can_run_yet",
+ {
+ "metrics": [
+ _mean(
+ {
+ "kind": "ExperimentWarehouseNativeNode",
+ "connection_id": "00000000-0000-0000-0000-000000000000",
+ "query": "SELECT customer_id AS entity_id, paid_at AS timestamp, amount FROM payments",
+ "unit": {"unit_type": "customer", "id_namespace": "billing_customer_id"},
+ "math": "sum",
+ "math_property": "amount",
+ }
+ )
+ ],
+ "exposure_criteria": {
+ "exposure_config": {
+ "kind": "ExperimentWarehouseNativeExposureConfig",
+ "connection_id": "00000000-0000-0000-0000-000000000000",
+ "query": "SELECT customer_id AS entity_id, variant, exposed_at AS timestamp FROM exposures",
+ "unit": {"unit_type": "customer", "id_namespace": "billing_customer_id"},
+ }
+ },
+ },
+ None,
+ (NOT_CHECKED, "Couldn't check 1 metric"),
+ ),
(
"legacy_metrics_only",
{"metrics": [{"kind": "ExperimentTrendsQuery"}]},
diff --git a/products/experiments/backend/metric_calculation/config.py b/products/experiments/backend/metric_calculation/config.py
index cbd2c0463496..c83a6c90ce6e 100644
--- a/products/experiments/backend/metric_calculation/config.py
+++ b/products/experiments/backend/metric_calculation/config.py
@@ -38,6 +38,8 @@ import pydantic
from posthog.schema import ActionsNode, ExperimentEventExposureConfig, ExperimentWarehouseNativeExposureConfig
+from posthog.hogql.errors import ExposedHogQLError
+
from posthog.dataclasses import frozen
from products.experiments.backend.hogql_queries import get_baseline_variant_key
@@ -180,7 +182,8 @@ def _resolve_exposure(
) -> ExposureQueryParams | None:
try:
return get_exposure_config_params_for_builder(exposure_criteria, team, start_date)
- except pydantic.ValidationError:
+ except (pydantic.ValidationError, ExposedHogQLError):
+ # The builder refuses a warehouse-native exposure, which has no ClickHouse query params.
return None
@@ -272,7 +275,7 @@ class ExperimentCalculationSettings:
excluded_variants: tuple[str, ...]
# What the query runner resolves from the stored exposure criteria. The `experiment-exposure-event` flag picks its
# default exposure event, so equality and the key read `normalized_exposure` instead. None when the stored
- # criteria do not parse. The query runner rejects such an experiment.
+ # criteria do not parse or name a warehouse-native exposure. The query runner rejects such an experiment.
exposure: ExposureQueryParams | None = field(compare=False)
# The stored exposure criteria as `normalize_exposure` returns them.
normalized_exposure: dict[str, Any]
diff --git a/products/experiments/backend/metric_resolution.py b/products/experiments/backend/metric_resolution.py
index efedd3bfbba3..686be8c630a9 100644
--- a/products/experiments/backend/metric_resolution.py
+++ b/products/experiments/backend/metric_resolution.py
@@ -220,7 +220,7 @@ def metric_source_nodes(metric: ExperimentMetric) -> list[Any]:
return [metric.start_event, metric.completion_event]
-def _metric_dict_source_nodes(metric_dict: dict[str, Any]) -> list[Any]:
+def metric_dict_source_nodes(metric_dict: dict[str, Any]) -> list[Any]:
"""A field absent on a shape is simply missing from the dict."""
return [
metric_dict.get("source"),
@@ -242,7 +242,7 @@ def metric_dict_reads_data_warehouse(metric_dict: dict[str, Any]) -> bool:
sampling)."""
return any(
isinstance(node, dict) and node.get("kind") == "ExperimentDataWarehouseNode"
- for node in _metric_dict_source_nodes(metric_dict)
+ for node in metric_dict_source_nodes(metric_dict)
)
@@ -254,5 +254,5 @@ def metric_uses_warehouse_native_source(metric: ExperimentMetric) -> bool:
def metric_dict_uses_warehouse_native_source(metric_dict: dict[str, Any]) -> bool:
return any(
isinstance(node, dict) and node.get("kind") == "ExperimentWarehouseNativeNode"
- for node in _metric_dict_source_nodes(metric_dict)
+ for node in metric_dict_source_nodes(metric_dict)
)
diff --git a/products/experiments/backend/metric_validation.py b/products/experiments/backend/metric_validation.py
index 2b13a6728c28..3a707648aedd 100644
--- a/products/experiments/backend/metric_validation.py
+++ b/products/experiments/backend/metric_validation.py
@@ -35,11 +35,7 @@ from products.event_definitions.backend.models import EventDefinition, effective
from products.experiments.backend.hogql_queries.base_query_utils import is_threshold_supported_math
from products.experiments.backend.hogql_queries.funnel_validation import FunnelDWValidator
from products.experiments.backend.hogql_queries.retention_validation import retention_metric_error
-from products.experiments.backend.metric_resolution import (
- METRIC_BUILDERS,
- ExperimentMetric,
- metric_uses_warehouse_native_source,
-)
+from products.experiments.backend.metric_resolution import METRIC_BUILDERS, ExperimentMetric
from products.experiments.backend.models.experiment import LEGACY_METRIC_KINDS
logger = structlog.get_logger(__name__)
@@ -151,9 +147,6 @@ def parse_and_validate_metric(metric: object, *, error_prefix: str) -> Experimen
raise ValidationError(f"{error_prefix}{_pydantic_error_message(e)}")
error = _semantic_error(parsed)
- if error is None and metric_uses_warehouse_native_source(parsed):
- # The source kind exists so the rest of the product can be built against it; nothing can run it yet.
- error = "warehouse-native sources are not supported yet."
if error:
raise ValidationError(f"{error_prefix}{error}")
return parsed
diff --git a/products/experiments/backend/test/test_experiment_saved_metric_service.py b/products/experiments/backend/test/test_experiment_saved_metric_service.py
index 8b17f31edb1a..a4526adbcec2 100644
--- a/products/experiments/backend/test/test_experiment_saved_metric_service.py
+++ b/products/experiments/backend/test/test_experiment_saved_metric_service.py
@@ -9,6 +9,7 @@ from posthog.models import Team
from products.experiments.backend.experiment_saved_metric_service import ExperimentSavedMetricService
from products.experiments.backend.experiment_service import ExperimentService
from products.experiments.backend.models.experiment import ExperimentSavedMetric, ExperimentToSavedMetric
+from products.warehouse_sources.backend.facade.models import ExternalDataSource
_THRESHOLD_MEAN: dict[str, object] = {
"kind": "ExperimentMetric",
@@ -30,7 +31,16 @@ _WAREHOUSE_NATIVE_MEAN: dict[str, object] = {
}
_SHARED_RULE_CASES: list[tuple[str, dict[str, object], str]] = [
- ("warehouse_native_not_supported_yet", _WAREHOUSE_NATIVE_MEAN, "warehouse-native sources are not supported yet"),
+ (
+ "ratio_mixing_warehouse_native_and_events",
+ {
+ "kind": "ExperimentMetric",
+ "metric_type": "ratio",
+ "numerator": _WAREHOUSE_NATIVE_MEAN["source"],
+ "denominator": {"kind": "EventsNode", "event": "$pageview"},
+ },
+ "cannot mix a warehouse-native source with PostHog events",
+ ),
(
"funnel_without_steps",
{"kind": "ExperimentMetric", "metric_type": "funnel", "series": []},
@@ -370,6 +380,46 @@ class TestExperimentSavedMetricService(APIBaseTest):
assert not ExperimentSavedMetric.objects.filter(id=saved_metric.id).exists()
assert ExperimentToSavedMetric.objects.filter(experiment_id=experiment.id).count() == 0
+ @parameterized.expand([("linked_experiment", False), ("only_a_deleted_experiment", True)])
+ def test_update_saved_metric_checks_a_warehouse_native_query_against_the_linked_experiments(
+ self, _name: str, experiment_deleted: bool
+ ) -> None:
+ saved_metric = self._service().create_saved_metric(name="Pageviews", query=self._valid_experiment_metric())
+ experiment = ExperimentService(team=self.team, user=self.user).create_experiment(
+ name="Events experiment",
+ feature_flag_key="events-experiment-shared-metric",
+ saved_metrics_ids=[{"id": saved_metric.id, "metadata": {"type": "primary"}}],
+ )
+ experiment.deleted = experiment_deleted
+ experiment.save(update_fields=["deleted"])
+ bigquery = ExternalDataSource.objects.create(
+ team=self.team,
+ source_id="bigquery",
+ source_type="BigQuery",
+ access_method=ExternalDataSource.AccessMethod.DIRECT,
+ job_inputs={},
+ )
+ source = dict(_WAREHOUSE_NATIVE_MEAN["source"]) # type: ignore[call-overload]
+ source["connection_id"] = str(bigquery.id)
+ query = {**_WAREHOUSE_NATIVE_MEAN, "source": source}
+
+ with patch(
+ "products.experiments.backend.warehouse_native_sources.bigquery_direct_query_enabled", return_value=True
+ ):
+ if experiment_deleted:
+ self._service().update_saved_metric(saved_metric, {"query": query})
+ saved_metric.refresh_from_db()
+ assert saved_metric.query["source"]["kind"] == "ExperimentWarehouseNativeNode"
+ return
+ with self.assertRaises(ValidationError) as ctx:
+ self._service().update_saved_metric(saved_metric, {"query": query})
+
+ assert "A linked experiment uses this metric" in str(ctx.exception)
+ assert "Events experiment" not in str(ctx.exception)
+ assert "needs a warehouse-native exposure" in str(ctx.exception)
+ saved_metric.refresh_from_db()
+ assert saved_metric.query["source"]["kind"] == "EventsNode"
+
def test_update_saved_metric_does_not_delete_links_on_validation_error(self) -> None:
saved_metric = ExperimentSavedMetric.objects.create(
team=self.team,
diff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.py
index 3d3fafe4f06b..c6bfb2de3a8e 100644
--- a/products/experiments/backend/test/test_experiment_service.py
+++ b/products/experiments/backend/test/test_experiment_service.py
@@ -76,7 +76,11 @@ from products.experiments.backend.timeseries_backfill import backfill_experiment
from products.feature_flags.backend.facade.api import set_flag_active, update_flag
from products.feature_flags.backend.models.feature_flag import FeatureFlag
from products.surveys.backend.models import Survey
-from products.warehouse_sources.backend.facade.models import DataWarehouseCredential, DataWarehouseTable
+from products.warehouse_sources.backend.facade.models import (
+ DataWarehouseCredential,
+ DataWarehouseTable,
+ ExternalDataSource,
+)
def _calculation_key(experiment: Experiment, metric_uuid: str) -> str:
@@ -880,18 +884,6 @@ class TestExperimentService(APIBaseTest):
"exposure_criteria.exposure_config.kind must be one of "
"['ExperimentEventExposureConfig', 'ActionsNode'], got 'EventsNode'",
),
- (
- "exposure_config_warehouse_native_not_supported",
- {
- "exposure_config": {
- "kind": "ExperimentWarehouseNativeExposureConfig",
- "connection_id": "00000000-0000-0000-0000-000000000000",
- "query": "SELECT user_id AS entity_id, variant, exposed_at AS timestamp FROM exposures",
- "unit": {"unit_type": "customer", "id_namespace": "billing_customer_id"},
- }
- },
- "exposure_criteria.exposure_config: warehouse-native exposures are not supported yet",
- ),
(
"activation_config_warehouse_native_rejected",
{
@@ -8091,6 +8083,264 @@ class TestExperimentServiceWarehouseMetricAccess(APIBaseTest):
assert not ExperimentTimeseriesRecalculation.objects.filter(experiment=experiment).exists()
+_WAREHOUSE_UNIT = {"unit_type": "customer", "id_namespace": "billing_customer_id"}
+
+
+def _warehouse_exposure(connection_id: str, unit: dict | None = None) -> dict:
+ return {
+ "exposure_config": {
+ "kind": "ExperimentWarehouseNativeExposureConfig",
+ "connection_id": connection_id,
+ "query": "SELECT customer_id AS entity_id, variant, exposed_at AS timestamp FROM exposures",
+ "unit": unit or _WAREHOUSE_UNIT,
+ }
+ }
+
+
+def _warehouse_metric(connection_id: str, unit: dict | None = None) -> dict:
+ return {
+ "kind": "ExperimentMetric",
+ "metric_type": "mean",
+ "source": {
+ "kind": "ExperimentWarehouseNativeNode",
+ "connection_id": connection_id,
+ "query": "SELECT customer_id AS entity_id, paid_at AS timestamp, amount FROM payments",
+ "unit": unit or _WAREHOUSE_UNIT,
+ "math": "sum",
+ "math_property": "amount",
+ },
+ }
+
+
+_EVENTS_METRIC = {
+ "kind": "ExperimentMetric",
+ "metric_type": "mean",
+ "source": {"kind": "EventsNode", "event": "$pageview"},
+}
+
+
+class TestExperimentServiceWarehouseNativeSources(APIBaseTest):
+ def setUp(self):
+ super().setUp()
+ self.bigquery = ExternalDataSource.objects.create(
+ team=self.team,
+ source_id="bigquery",
+ source_type="BigQuery",
+ access_method=ExternalDataSource.AccessMethod.DIRECT,
+ job_inputs={},
+ )
+ self.postgres = ExternalDataSource.objects.create(
+ team=self.team,
+ source_id="postgres",
+ source_type="Postgres",
+ access_method=ExternalDataSource.AccessMethod.DIRECT,
+ job_inputs={},
+ )
+ flag = patch(
+ "products.experiments.backend.warehouse_native_sources.bigquery_direct_query_enabled", return_value=True
+ )
+ self.flag_enabled = flag.start()
+ self.addCleanup(flag.stop)
+ self.service = ExperimentService(team=self.team, user=self.user)
+
+ def _create(self, *, exposure_criteria: dict | None, metrics: list[dict]) -> Experiment:
+ return self.service.create_experiment(
+ name="Warehouse experiment",
+ feature_flag_key="warehouse-experiment",
+ exposure_criteria=exposure_criteria,
+ metrics=metrics,
+ allow_unknown_events=True,
+ )
+
+ def test_saves_an_experiment_whose_exposure_and_metrics_read_one_bigquery_connection(self) -> None:
+ connection_id = str(self.bigquery.id)
+ experiment = self._create(
+ exposure_criteria=_warehouse_exposure(connection_id), metrics=[_warehouse_metric(connection_id)]
+ )
+
+ experiment.refresh_from_db()
+ assert experiment.exposure_criteria is not None and experiment.metrics is not None
+ assert experiment.exposure_criteria["exposure_config"]["connection_id"] == connection_id
+ assert experiment.metrics[0]["source"]["kind"] == "ExperimentWarehouseNativeNode"
+
+ @parameterized.expand(
+ [
+ (
+ "metric_without_warehouse_exposure",
+ None,
+ ["bigquery"],
+ "A warehouse-native metric needs a warehouse-native exposure",
+ ),
+ (
+ "exposure_with_an_events_metric",
+ "bigquery",
+ ["events"],
+ "can only use warehouse-native metrics",
+ ),
+ (
+ "metric_on_another_connection",
+ "bigquery",
+ ["postgres"],
+ "must use the same connection",
+ ),
+ (
+ "metric_with_another_unit",
+ "bigquery",
+ ["bigquery_other_unit"],
+ "must use the same unit binding",
+ ),
+ (
+ "connection_is_not_bigquery",
+ "postgres",
+ ["postgres"],
+ "support BigQuery connections only",
+ ),
+ (
+ "connection_does_not_exist",
+ "missing",
+ ["missing"],
+ "Invalid connectionId",
+ ),
+ (
+ "flag_off",
+ "bigquery",
+ ["bigquery"],
+ "not available in this project",
+ ),
+ ]
+ )
+ def test_create_rejects_sources_that_cannot_run_together(
+ self, name: str, exposure_connection: str | None, metric_connections: list[str], expected_error: str
+ ) -> None:
+ if name == "flag_off":
+ self.flag_enabled.return_value = False
+ connection_ids = {
+ "bigquery": str(self.bigquery.id),
+ "postgres": str(self.postgres.id),
+ "missing": str(uuid4()),
+ }
+ metrics = []
+ for metric_connection in metric_connections:
+ if metric_connection == "events":
+ metrics.append(_EVENTS_METRIC)
+ elif metric_connection == "bigquery_other_unit":
+ metrics.append(
+ _warehouse_metric(
+ connection_ids["bigquery"], {"unit_type": "account", "id_namespace": "account_id"}
+ )
+ )
+ else:
+ metrics.append(_warehouse_metric(connection_ids[metric_connection]))
+ exposure_criteria = _warehouse_exposure(connection_ids[exposure_connection]) if exposure_connection else None
+
+ with self.assertRaises(ValidationError) as ctx:
+ self._create(exposure_criteria=exposure_criteria, metrics=metrics)
+
+ assert expected_error in str(ctx.exception)
+ assert not Experiment.objects.filter(team=self.team).exists()
+
+ @parameterized.expand(
+ [("launch_action",), ("patch_start_date",), ("live_create",), ("patch_sources_of_a_launched_experiment",)]
+ )
+ def test_refuses_warehouse_native_sources_on_a_launched_experiment(self, path: str) -> None:
+ connection_id = str(self.bigquery.id)
+ exposure_criteria = _warehouse_exposure(connection_id)
+ metrics = [_warehouse_metric(connection_id)]
+ with self.assertRaises(ValidationError) as ctx:
+ if path == "live_create":
+ self.service.create_experiment(
+ name="Warehouse experiment",
+ feature_flag_key="warehouse-experiment",
+ exposure_criteria=exposure_criteria,
+ metrics=metrics,
+ start_date=timezone.now(),
+ )
+ elif path == "patch_sources_of_a_launched_experiment":
+ launched = self.service.create_experiment(
+ name="Events experiment",
+ feature_flag_key="events-experiment",
+ metrics=[_EVENTS_METRIC],
+ start_date=timezone.now(),
+ allow_unknown_events=True,
+ )
+ self.service.update_experiment(launched, {"exposure_criteria": exposure_criteria, "metrics": metrics})
+ else:
+ experiment = self._create(exposure_criteria=exposure_criteria, metrics=metrics)
+ if path == "launch_action":
+ self.service.launch_experiment(experiment)
+ else:
+ self.service.update_experiment(experiment, {"start_date": timezone.now()})
+
+ assert "cannot run yet" in str(ctx.exception)
+ assert not Experiment.objects.filter(
+ team=self.team,
+ start_date__isnull=False,
+ exposure_criteria__exposure_config__kind="ExperimentWarehouseNativeExposureConfig",
+ ).exists()
+
+ def test_refuses_a_raw_query_when_the_user_cannot_read_every_table_on_the_connection(self) -> None:
+ self.organization.available_product_features = [
+ {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL},
+ {"key": AvailableFeature.ROLE_BASED_ACCESS, "name": AvailableFeature.ROLE_BASED_ACCESS},
+ ]
+ self.organization.save()
+ membership = OrganizationMembership.objects.get(user=self.user, organization=self.organization)
+ membership.level = OrganizationMembership.Level.MEMBER
+ membership.save()
+ credential = DataWarehouseCredential.objects.create(access_key="x", access_secret="x", team=self.team)
+ table = DataWarehouseTable.objects.create(
+ name="restricted_payments",
+ format=DataWarehouseTable.TableFormat.Parquet,
+ team=self.team,
+ credential=credential,
+ external_data_source=self.bigquery,
+ url_pattern="s3://bucket/restricted/*",
+ columns={"id": "String"},
+ )
+ AccessControl.objects.create(
+ team=self.team,
+ resource="warehouse_table",
+ resource_id=str(table.pk),
+ access_level="none",
+ organization_member=membership,
+ )
+ connection_id = str(self.bigquery.id)
+
+ # The flag `hogql-warehouse-access-control` turns on the per-table check of raw queries.
+ with (
+ patch("posthog.hogql.direct_connection.feature_enabled_or_false", return_value=True),
+ self.assertRaises(PermissionDenied),
+ ):
+ self._create(
+ exposure_criteria=_warehouse_exposure(connection_id), metrics=[_warehouse_metric(connection_id)]
+ )
+ assert not Experiment.objects.filter(team=self.team).exists()
+
+ @parameterized.expand(
+ [
+ ("metrics_now_read_events", {"metrics": [_EVENTS_METRIC]}, "can only use warehouse-native metrics"),
+ ("name_only_after_the_flag_is_off", {"name": "Renamed"}, None),
+ ]
+ )
+ def test_update_reads_the_fields_left_out_of_the_update_from_the_experiment(
+ self, _name: str, update_data: dict, expected_error: str | None
+ ) -> None:
+ connection_id = str(self.bigquery.id)
+ experiment = self._create(
+ exposure_criteria=_warehouse_exposure(connection_id), metrics=[_warehouse_metric(connection_id)]
+ )
+ self.flag_enabled.return_value = False
+
+ if expected_error is None:
+ self.service.update_experiment(experiment, update_data, allow_unknown_events=True)
+ experiment.refresh_from_db()
+ assert experiment.name == "Renamed"
+ return
+ with self.assertRaises(ValidationError) as ctx:
+ self.service.update_experiment(experiment, update_data, allow_unknown_events=True)
+ assert expected_error in str(ctx.exception)
+
+
class TestDeprecatedFieldsInRequest(SimpleTestCase):
@parameterized.expand(
[
diff --git a/products/experiments/backend/warehouse_native_sources.py b/products/experiments/backend/warehouse_native_sources.py
new file mode 100644
index 000000000000..24c265e4c96c
--- /dev/null
+++ b/products/experiments/backend/warehouse_native_sources.py
@@ -0,0 +1,180 @@
+from typing import Any
+
+from rest_framework.exceptions import PermissionDenied, ValidationError
+
+from posthog.hogql.direct_connection import (
+ RAW_QUERY_TABLE_DENIED_ERROR,
+ get_direct_connection_source_none_or_raise,
+ raw_query_denied_by_table_access,
+)
+from posthog.hogql.direct_sql.capability import bigquery_direct_query_enabled
+
+from posthog.dataclasses import frozen
+from posthog.models.team.team import Team
+from posthog.models.user import User
+
+from products.experiments.backend.metric_resolution import (
+ metric_dict_source_nodes,
+ metric_dict_uses_warehouse_native_source,
+)
+from products.experiments.backend.models.experiment import ExperimentSavedMetric, ExperimentToSavedMetric
+from products.warehouse_sources.backend.facade.types import ExternalDataSourceType
+
+WAREHOUSE_NATIVE_NODE_KIND = "ExperimentWarehouseNativeNode"
+WAREHOUSE_NATIVE_EXPOSURE_KIND = "ExperimentWarehouseNativeExposureConfig"
+
+
+@frozen
+class UnitBinding:
+ unit_type: str
+ id_namespace: str
+
+
+def warehouse_native_exposure_config(exposure_criteria: dict[str, Any] | None) -> dict[str, Any] | None:
+ exposure_config = (exposure_criteria or {}).get("exposure_config")
+ if isinstance(exposure_config, dict) and exposure_config.get("kind") == WAREHOUSE_NATIVE_EXPOSURE_KIND:
+ return exposure_config
+ return None
+
+
+def validate_experiment_warehouse_native_sources(
+ *,
+ exposure_criteria: dict[str, Any] | None,
+ metrics: list[dict[str, Any]],
+ team: Team,
+ user: Any,
+ resolve_connection: bool = True,
+) -> str | None:
+ """An experiment reads one kind of data: PostHog events, or the customer's warehouse through one
+ connection. A warehouse-native exposure needs warehouse-native metrics, because nothing can join
+ warehouse ids to PostHog persons. Returns the connection id of the warehouse-native sources, if any.
+ A caller that checks several experiments on one connection resolves it once, with `resolve_connection`
+ off."""
+ exposure_config = warehouse_native_exposure_config(exposure_criteria)
+ nodes = _metric_source_nodes(metrics)
+ native_nodes = [node for node in nodes if node.get("kind") == WAREHOUSE_NATIVE_NODE_KIND]
+ if exposure_config is None and native_nodes:
+ raise ValidationError(
+ "A warehouse-native metric needs a warehouse-native exposure. "
+ "Set exposure_criteria.exposure_config to a query on the same connection."
+ )
+ if exposure_config is not None and len(native_nodes) < len(nodes):
+ raise ValidationError(
+ "An experiment with a warehouse-native exposure can only use warehouse-native metrics. "
+ "Remove the metrics that read PostHog events or synced warehouse tables."
+ )
+ sources = [exposure_config, *native_nodes] if exposure_config is not None else native_nodes
+ connection_id = warehouse_native_sources_agree(sources)
+ if connection_id is not None and resolve_connection:
+ validate_warehouse_native_connection(connection_id, team=team, user=user)
+ return connection_id
+
+
+def validate_metric_warehouse_native_sources(metrics: list[dict[str, Any]], *, team: Team, user: Any) -> None:
+ """The check for a metric saved on its own, such as a shared metric."""
+ nodes = _metric_source_nodes(metrics)
+ native_nodes = [node for node in nodes if node.get("kind") == WAREHOUSE_NATIVE_NODE_KIND]
+ if native_nodes and len(native_nodes) < len(nodes):
+ raise ValidationError(
+ "A metric cannot mix a warehouse-native source with PostHog events or synced warehouse tables."
+ )
+ connection_id = warehouse_native_sources_agree(native_nodes)
+ if connection_id is not None:
+ validate_warehouse_native_connection(connection_id, team=team, user=user)
+
+
+def warehouse_native_sources_agree(sources: list[dict[str, Any]]) -> str | None:
+ """Every source must name the same connection and the same unit binding. `sources` holds exposure
+ configs and metric nodes; both carry `connection_id` and `unit`. Returns the shared connection id."""
+ if not sources:
+ return None
+ connection_ids = {str(source.get("connection_id") or "") for source in sources}
+ if len(connection_ids) > 1:
+ raise ValidationError("All warehouse-native sources on an experiment must use the same connection.")
+ unit_bindings = {_unit_binding(source) for source in sources}
+ if len(unit_bindings) > 1:
+ raise ValidationError(
+ "All warehouse-native sources on an experiment must use the same unit binding (unit_type and id_namespace)."
+ )
+ return connection_ids.pop()
+
+
+def validate_warehouse_native_connection(connection_id: str, *, team: Team, user: Any) -> None:
+ """The connection must be a direct BigQuery source that the saving user can read in full."""
+ if not bigquery_direct_query_enabled(team):
+ raise ValidationError("Warehouse-native sources are not available in this project.")
+
+ # A synthetic principal or a userless caller has no access control to check, as in validate_warehouse_metrics.
+ connection = get_direct_connection_source_none_or_raise(
+ team,
+ connection_id,
+ user=user if isinstance(user, User) else None,
+ error_factory=ValidationError,
+ require_pure_direct=True,
+ )
+ if connection is None:
+ raise ValidationError("Warehouse-native sources need a connection_id.")
+ if connection.source_type != ExternalDataSourceType.BIGQUERY:
+ raise ValidationError("Warehouse-native sources support BigQuery connections only.")
+ # The queries are raw SQL, which the runner cannot check table by table, so the same rule as the raw
+ # query path applies: the user must be able to read every table on the connection.
+ if isinstance(user, User) and raw_query_denied_by_table_access(team, connection, user=user):
+ raise PermissionDenied(RAW_QUERY_TABLE_DENIED_ERROR)
+
+
+def validate_saved_metric_query_against_linked_experiments(
+ saved_metric: ExperimentSavedMetric, query: dict[str, Any], *, team: Team, user: Any
+) -> None:
+ """Every experiment that links the shared metric reads the new query on its next calculation, so the
+ query has to fit each of them."""
+ if not metric_dict_uses_warehouse_native_source(query) and not metric_dict_uses_warehouse_native_source(
+ saved_metric.query or {}
+ ):
+ return
+ links = (
+ ExperimentToSavedMetric.objects.filter(saved_metric=saved_metric, experiment__team_id=team.id)
+ .exclude(experiment__deleted=True)
+ .select_related("experiment")
+ .prefetch_related("experiment__experimenttosavedmetric_set__saved_metric")
+ )
+ connection_ids: set[str] = set()
+ for link in links:
+ experiment = link.experiment
+ other_queries = [
+ other.saved_metric.query
+ for other in experiment.experimenttosavedmetric_set.all()
+ if other.saved_metric_id != saved_metric.id and other.saved_metric.query
+ ]
+ try:
+ connection_id = validate_experiment_warehouse_native_sources(
+ exposure_criteria=experiment.exposure_criteria,
+ metrics=[*(experiment.metrics or []), *(experiment.metrics_secondary or []), *other_queries, query],
+ team=team,
+ user=user,
+ resolve_connection=False,
+ )
+ except ValidationError as error:
+ # The user may not be allowed to see the experiment, so the error does not name it.
+ detail = error.detail[0] if isinstance(error.detail, list) else error.detail
+ raise ValidationError(f"A linked experiment uses this metric. {detail}")
+ if connection_id is not None:
+ connection_ids.add(connection_id)
+ for connection_id in sorted(connection_ids):
+ validate_warehouse_native_connection(connection_id, team=team, user=user)
+
+
+def _metric_source_nodes(metrics: list[dict[str, Any]]) -> list[dict[str, Any]]:
+ return [
+ node
+ for metric in metrics
+ if isinstance(metric, dict)
+ for node in metric_dict_source_nodes(metric)
+ if isinstance(node, dict)
+ ]
+
+
+def _unit_binding(source: dict[str, Any]) -> UnitBinding:
+ unit = source.get("unit")
+ if not isinstance(unit, dict):
+ return UnitBinding(unit_type="", id_namespace="")
+ return UnitBinding(unit_type=str(unit.get("unit_type") or ""), id_namespace=str(unit.get("id_namespace") or ""))