: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(signals): read inbox impressions from raw properties in the ranking dag (#91316)
Andrew Maguiref608fcddf17b
diff --git a/products/signals/dags/inbox_ranking/dataset/queries.py b/products/signals/dags/inbox_ranking/dataset/queries.pyindex b44d48099d54..dd19b325c9f9 100644--- a/products/signals/dags/inbox_ranking/dataset/queries.py+++ b/products/signals/dags/inbox_ranking/dataset/queries.py@@ -107,6 +107,11 @@ def valid_report_uuids(report_ids: set[str | None]) -> set[str]:# training row nor stamp a feedback label onto a real report.FEEDBACK_SENTIMENTS_SQL = "toString(properties.sentiment) IN ('positive', 'negative')"+# `impressions` is read from the raw properties JSON rather than as `properties.impressions`. HogQL+# casts a property with its project-wide property definition type, and other events in the dogfood+# project send `impressions` as a number, which types the definition as Numeric and turns the array+# into a Float64 cast that JSONExtractArrayRaw rejects. The raw read does not depend on that type.+## The feedback filter rides as a second predicate rather than its own UNION branch so the whole# select keeps one sort-key-aligned `event IN (...)` scan.LABELED_REPORT_IDS_SQL = f"""@@ -114,7 +119,7 @@ SELECT DISTINCT report_idFROM (SELECT JSONExtractString(imp, 'report_id') AS report_idFROM events- ARRAY JOIN JSONExtractArrayRaw(coalesce(properties.impressions, '[]')) AS imp+ ARRAY JOIN JSONExtractArrayRaw(properties, 'impressions') AS impWHERE event = 'Inbox reports impressed'AND timestamp >= toDateTime({{labels_epoch}}) AND timestamp < toDateTime({{snapshot_end}})UNION ALL@@ -263,7 +268,7 @@ SELECTmin({_IMPRESSION_RANK}) AS best_impression_rank,argMax(JSONExtract(imp, 'source_products', 'Array(String)'), timestamp) AS source_productsFROM events-ARRAY JOIN JSONExtractArrayRaw(coalesce(properties.impressions, '[]')) AS imp+ARRAY JOIN JSONExtractArrayRaw(properties, 'impressions') AS impWHERE event = 'Inbox reports impressed'AND timestamp >= toDateTime({{labels_epoch}}) AND timestamp < toDateTime({{snapshot_end}})GROUP BY report_iddiff --git a/products/signals/dags/inbox_ranking/tests/test_dataset.py b/products/signals/dags/inbox_ranking/tests/test_dataset.pyindex c703a690efb9..19c98e0af710 100644--- a/products/signals/dags/inbox_ranking/tests/test_dataset.py+++ b/products/signals/dags/inbox_ranking/tests/test_dataset.py@@ -7,6 +7,7 @@ from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_eventimport pyarrow as pafrom parameterized import parameterized+from products.event_definitions.backend.models.property_definition import PropertyDefinitionfrom products.signals.backend.models import SignalReportfrom products.signals.dags.inbox_ranking import commonfrom products.signals.dags.inbox_ranking.dataset.dag import (@@ -16,8 +17,10 @@ from products.signals.dags.inbox_ranking.dataset.dag import (spine_report_filter,)from products.signals.dags.inbox_ranking.dataset.queries import (+ IMPRESSIONS_SQL,LABEL_DEFAULTS,LABEL_STREAMS,+ LABELED_REPORT_IDS_SQL,STATUS_COLUMNS,STATUS_SQL,hogql_rows,@@ -351,6 +354,27 @@ class TestSpineInclusion(BaseTest):assert created_after_cutoff not in in_spine+class TestImpressionsStream(ClickhouseTestMixin, BaseTest):+ @parameterized.expand([("labeled_ids", LABELED_REPORT_IDS_SQL), ("impressions", IMPRESSIONS_SQL)])+ def test_impressions_survive_a_numeric_property_definition(self, _name, sql):+ # Another event in the same project sending `impressions` as a number types the project-wide+ # definition as Numeric, which made HogQL cast the impressions array to Float64 and fail+ # the query with a ClickHouse type error.+ PropertyDefinition.objects.create(+ team=self.team, name="impressions", property_type="Numeric", type=PropertyDefinition.Type.EVENT+ )+ _create_event(+ team=self.team,+ event="Inbox reports impressed",+ distinct_id="user-1",+ timestamp=T1,+ properties={"impressions": [{"report_id": UUID_A, "rank": 1, "source_products": ["error_tracking"]}]},+ )++ rows = hogql_rows(sql, team=self.team, query_type="test", snapshot_end=SNAPSHOT_END)+ assert [row[0] for row in rows] == [UUID_A]++class TestStatusStream(ClickhouseTestMixin, BaseTest):def _transition(self,