: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
fix(trends): reject property math with an empty math_property (#118482)
posthog[bot]bbea8f954c4d
diff --git a/products/product_analytics/backend/hogql_queries/trends/test/test_trend_validation_rules.py b/products/product_analytics/backend/hogql_queries/trends/test/test_trend_validation_rules.pyindex d5d22a2e6637..e0a7dad0f81d 100644--- a/products/product_analytics/backend/hogql_queries/trends/test/test_trend_validation_rules.py+++ b/products/product_analytics/backend/hogql_queries/trends/test/test_trend_validation_rules.py@@ -5,6 +5,7 @@ from parameterized import parameterizedfrom rest_framework.exceptions import ValidationErrorfrom posthog.schema import (+ BaseMathType,Breakdown,BreakdownFilter,BreakdownType,@@ -374,6 +375,29 @@ class TestDisallowDaysOfWeekWithSmoothing(BaseTest):self.assertEqual(context.exception.get_codes(), ["days_of_week_unsupported_with_smoothing"])+class TestRequirePropertyForPropertyMath(BaseTest):+ @parameterized.expand(+ [+ ("property_math_with_property", PropertyMathType.SUM, "price"),+ ("property_math_without_property", PropertyMathType.AVG, None),+ ("count_math_with_empty_property", BaseMathType.TOTAL, ""),+ ]+ )+ def test_allows(self, _name: str, math: PropertyMathType | BaseMathType, math_property: str | None) -> None:+ query = TrendsQuery(series=[EventsNode(event="$pageview", math=math, math_property=math_property)])++ TrendsQueryRunner(query=query, team=self.team).validate()++ @parameterized.expand([(math.value, math) for math in (PropertyMathType.AVG, PropertyMathType.P99)])+ def test_rejects_property_math_with_empty_property(self, _name: str, math: PropertyMathType) -> None:+ query = TrendsQuery(series=[EventsNode(event="$pageview", math=math, math_property="")])++ with self.assertRaises(ValidationError) as context:+ TrendsQueryRunner(query=query, team=self.team).validate()++ self.assertEqual(context.exception.get_codes(), ["property_math_requires_property"])++class TestTrendsQueryRunnerSeriesFanOut(BaseTest):def test_runner_rejects_an_expansion_over_the_limit_before_it_expands_the_series(self) -> None:query = TrendsQuery(diff --git a/products/product_analytics/backend/hogql_queries/trends/trend_validation_rules.py b/products/product_analytics/backend/hogql_queries/trends/trend_validation_rules.pyindex e511606ab190..6548a79c7c45 100644--- a/products/product_analytics/backend/hogql_queries/trends/trend_validation_rules.py+++ b/products/product_analytics/backend/hogql_queries/trends/trend_validation_rules.py@@ -88,6 +88,21 @@ class DisallowUnsupportedPropertyMathForHistogramBreakdown:)+class RequirePropertyForPropertyMath:+ """An empty math property passes the property-math branch but leaves no field to aggregate."""++ code = "property_math_requires_property"++ def validate(self, context: QueryValidationContext[TrendsQuery]) -> None:+ for series in context.query.series:+ if series.math in set(PropertyMathType) and series.math_property == "":+ raise ValidationError(+ f"The {series.math} math type needs a property. "+ "Choose a property for the series, or use a different math type.",+ code=self.code,+ )++class DisallowDaysOfWeekWithSmoothing:"""Smoothing averages over the full date axis, where days excluded by daysOfWeek are structural zeros."""diff --git a/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py b/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.pyindex 693b5f48d665..85a38dbede52 100644--- a/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py+++ b/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py@@ -90,6 +90,7 @@ from products.product_analytics.backend.hogql_queries.trends.series_with_extrasfrom products.product_analytics.backend.hogql_queries.trends.trend_validation_rules import (DisallowDaysOfWeekWithSmoothing,DisallowUnsupportedPropertyMathForHistogramBreakdown,+ RequirePropertyForPropertyMath,ValidateDataWarehouseBreakdown,)from products.product_analytics.backend.hogql_queries.trends.trends_actors_query_builder import TrendsActorsQueryBuilder@@ -178,6 +179,7 @@ class TrendsQueryRunner(AnalyticsQueryRunner[TrendsQueryResponse]):DisallowUnsupportedDataWarehouseSettings(),ValidateDataWarehouseBreakdown(),DisallowUnsupportedPropertyMathForHistogramBreakdown(),+ RequirePropertyForPropertyMath(),DisallowDaysOfWeekWithSmoothing(),)