: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(marketing-analytics): strip infinity sentinel from csv exports (#74056)
Javier Bahamondesf0282e132234
diff --git a/products/marketing_analytics/backend/hogql_queries/marketing_analytics_base_query_runner.py b/products/marketing_analytics/backend/hogql_queries/marketing_analytics_base_query_runner.pyindex a95c1624406c..479c34f8b0ae 100644--- a/products/marketing_analytics/backend/hogql_queries/marketing_analytics_base_query_runner.py+++ b/products/marketing_analytics/backend/hogql_queries/marketing_analytics_base_query_runner.py@@ -12,11 +12,14 @@ from posthog.schema import (ConversionGoalFilter2,ConversionGoalFilter3,DateRange,+ InfinityValue,MarketingAnalyticsConstants,MarketingAnalyticsDrillDownLevel,+ MarketingAnalyticsItem,)from posthog.hogql import ast+from posthog.hogql.constants import LimitContextfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.database import Databasefrom posthog.hogql.database.schema.channel_type import ChannelTypeExprs, create_channel_type_expr@@ -68,6 +71,23 @@ COMPARE_PERIOD_PREVIOUS = "previous"COSTS_PRECOMPUTE_TTL_SECONDS = {"0d": 6 * 60 * 60, "1d": 24 * 60 * 60, "default": 7 * 24 * 60 * 60}+_INFINITY_SENTINELS = {float(InfinityValue.NUMBER_999999), float(InfinityValue.NUMBER__999999)}+++def strip_infinity_sentinels(response: AnalyticsQueryResponseProtocol) -> None:+ """Blank the "infinite change" sentinel (±999999) so tabular exports, which render cells raw, don't show it as a real percentage."""+ results = getattr(response, "results", None)+ if isinstance(results, dict):+ cells = list(results.values())+ elif isinstance(results, list):+ cells = [cell for row in results for cell in (row if isinstance(row, list) else [row])]+ else:+ return+ for cell in cells:+ if isinstance(cell, MarketingAnalyticsItem) and cell.changeFromPreviousPct in _INFINITY_SENTINELS:+ cell.changeFromPreviousPct = None++def _session_start_day(expr: ast.Expr) -> ast.Expr:"""Truncate to the day, via a function the sessions timestamp pushdown recognizes.@@ -102,6 +122,8 @@ class MarketingAnalyticsBaseQueryRunner(AnalyticsQueryRunner[ResponseType], ABC,start = time.perf_counter()try:response = self._calculate()+ if self.limit_context == LimitContext.EXPORT:+ strip_infinity_sentinels(response)self._capture_query_event("marketing analytics query performed", start)return responseexcept Exception as e:diff --git a/products/marketing_analytics/backend/hogql_queries/test_marketing_analytics_base_query_runner.py b/products/marketing_analytics/backend/hogql_queries/test_marketing_analytics_base_query_runner.pynew file mode 100644index 000000000000..81877948d9e8--- /dev/null+++ b/products/marketing_analytics/backend/hogql_queries/test_marketing_analytics_base_query_runner.py@@ -0,0 +1,49 @@+from parameterized import parameterized++from posthog.schema import (+ InfinityValue,+ MarketingAnalyticsAggregatedQueryResponse,+ MarketingAnalyticsItem,+ MarketingAnalyticsTableQueryResponse,+ WebAnalyticsItemKind,+)++from products.marketing_analytics.backend.hogql_queries.marketing_analytics_base_query_runner import (+ strip_infinity_sentinels,+)+++def _item(change_pct: float | None) -> MarketingAnalyticsItem:+ return MarketingAnalyticsItem(+ key="Cost",+ kind=WebAnalyticsItemKind.CURRENCY,+ value=100.0,+ previous=0.0,+ changeFromPreviousPct=change_pct,+ hasComparison=True,+ )+++class TestStripInfinitySentinels:+ @parameterized.expand(+ [+ (float(InfinityValue.NUMBER_999999), None),+ (float(InfinityValue.NUMBER__999999), None),+ (42.0, 42.0),+ (0.0, 0.0),+ (None, None),+ ]+ )+ def test_table_response_cells(self, change_pct: float | None, expected: float | None) -> None:+ response = MarketingAnalyticsTableQueryResponse(results=[[_item(change_pct), _item(7.0)]])+ strip_infinity_sentinels(response)+ assert response.results[0][0].changeFromPreviousPct == expected+ assert response.results[0][1].changeFromPreviousPct == 7.0++ def test_aggregated_response_dict_results(self) -> None:+ response = MarketingAnalyticsAggregatedQueryResponse(+ results={"cost": _item(float(InfinityValue.NUMBER_999999)), "clicks": _item(12.0)}+ )+ strip_infinity_sentinels(response)+ assert response.results["cost"].changeFromPreviousPct is None+ assert response.results["clicks"].changeFromPreviousPct == 12.0