: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(property-vals): distinct read from the property values table (#62249)
Andy Zhao7c28d0cc9961
diff --git a/posthog/hogql_queries/test/test_property_values_query_runner.py b/posthog/hogql_queries/test/test_property_values_query_runner.pyindex f6c786af1b5e..818e7fa9bf4f 100644--- a/posthog/hogql_queries/test/test_property_values_query_runner.py+++ b/posthog/hogql_queries/test/test_property_values_query_runner.py@@ -275,7 +275,7 @@ class TestPropertyValuesQueryRunnerAggregatedTable(ClickhouseTestMixin, APIBaseTrows,)- def test_values_ordered_by_summed_count(self):+ def test_returns_distinct_values(self):now = datetime.now()self._insert_rows([@@ -287,7 +287,7 @@ class TestPropertyValuesQueryRunnerAggregatedTable(ClickhouseTestMixin, APIBaseTwith self._flag_on():results = self._run(PropertyValuesQuery(property_type=PropertyType.EVENT, property_key="browser"))- assert [r.name for r in results] == ["Chrome", "Firefox"]+ assert {r.name for r in results} == {"Chrome", "Firefox"}assert results[0].count is None@parameterized.expand(diff --git a/posthog/queries/property_values.py b/posthog/queries/property_values.pyindex 2044ef676f9d..b647ae236e9c 100644--- a/posthog/queries/property_values.py+++ b/posthog/queries/property_values.py@@ -80,16 +80,18 @@ def get_property_values_for_key(# property_values_distributed routes to the aggregated table on the aux cluster.+# DISTINCT, not GROUP BY + sum(property_count): lets LIMIT early-terminate on+# high-cardinality keys instead of reading every value to rank by count. Mirrors the+# events-scan semantics (unordered distinct values, shortest-first when searching).SELECT_EVENT_PROPERTY_VALUES_FROM_AGGREGATED_TABLE_SQL = """-SELECT property_value, sum(property_count) AS prop_count+SELECT DISTINCT property_valueFROM property_values_distributedWHERE team_id = %(team_id)sAND property_type = 'event'AND property_key = %(key)sAND last_seen >= now() - INTERVAL 7 DAY{value_filter}-GROUP BY property_value-ORDER BY prop_count DESC+{order_by_clause}LIMIT 10"""@@ -102,13 +104,17 @@ def get_event_property_values_from_aggregated_table(key: str, team: Team, value:params: dict = {"team_id": team.pk, "key": key}value_filter = ""+ order_by_clause = ""if value:escaped = value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")value_filter = "AND lower(property_value) LIKE %(value)s"params["value"] = f"%{escaped.lower()}%"+ order_by_clause = "ORDER BY length(property_value)"result = insight_sync_execute(- SELECT_EVENT_PROPERTY_VALUES_FROM_AGGREGATED_TABLE_SQL.format(value_filter=value_filter),+ SELECT_EVENT_PROPERTY_VALUES_FROM_AGGREGATED_TABLE_SQL.format(+ value_filter=value_filter, order_by_clause=order_by_clause+ ),params,query_type="get_event_property_values_from_aggregated_table",team_id=team.pk,