: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(groups): coerce group_name to String so numeric name doesn't crash query (#63647)
posthog[bot]df0e0d0039ca
diff --git a/posthog/hogql_queries/groups/groups_query_runner.py b/posthog/hogql_queries/groups/groups_query_runner.pyindex 7f92a8dc6aad..35666c5def97 100644--- a/posthog/hogql_queries/groups/groups_query_runner.py+++ b/posthog/hogql_queries/groups/groups_query_runner.py@@ -103,10 +103,7 @@ class GroupsQueryRunner(AnalyticsQueryRunner[GroupsQueryResponse]):if col.startswith("group_name"):order_by.append(ast.OrderExpr(- expr=ast.Call(- name="coalesce",- args=[ast.Field(chain=["properties", "name"]), ast.Field(chain=["key"])],- ),+ expr=self._display_name_expr(),order="DESC" if "DESC" in col else "ASC",))@@ -124,12 +121,23 @@ class GroupsQueryRunner(AnalyticsQueryRunner[GroupsQueryResponse]):order_by=order_by,)+ def _display_name_expr(self) -> ast.Expr:+ # Coerce both branches to String so a numeric `name` property type doesn't make+ # coalesce resolve to Variant(Float64, String), which clickhouse_driver can't parse.+ return ast.Call(+ name="coalesce",+ args=[+ ast.Call(name="toString", args=[ast.Field(chain=["properties", "name"])]),+ ast.Call(name="toString", args=[ast.Field(chain=["key"])]),+ ],+ )+def _column_to_expr(self, col: str) -> ast.Expr:if col == "group_name":return ast.Call(name="tuple",args=[- ast.Call(name="coalesce", args=[ast.Field(chain=["properties", "name"]), ast.Field(chain=["key"])]),+ self._display_name_expr(),ast.Call(name="toString", args=[ast.Field(chain=["key"])]),],)@@ -171,9 +179,7 @@ class GroupsQueryRunner(AnalyticsQueryRunner[GroupsQueryResponse]):"""When a search term exists, we want to rank the results by how close they are to it."""- display_name_expr = ast.Call(- name="coalesce", args=[ast.Field(chain=["properties", "name"]), ast.Field(chain=["key"])]- )+ display_name_expr = self._display_name_expr()return ast.OrderExpr(expr=ast.Call(diff --git a/posthog/hogql_queries/groups/test/__snapshots__/test_groups_query_runner.ambr b/posthog/hogql_queries/groups/test/__snapshots__/test_groups_query_runner.ambrindex 8f60cb5b30a6..50bfc48be49d 100644--- a/posthog/hogql_queries/groups/test/__snapshots__/test_groups_query_runner.ambr+++ b/posthog/hogql_queries/groups/test/__snapshots__/test_groups_query_runner.ambr@@ -1,7 +1,7 @@# serializer version: 1# name: TestGroupsQueryRunner.test_groups_query_runner'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`FROM(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,tupleElement(argMax(tuple(toTimeZone(groups.created_at, 'UTC')), toTimeZone(groups._timestamp, 'UTC')), 1) AS created_at,@@ -30,7 +30,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_normalize_multiple_groups'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`,+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`,groups.key AS key,accurateCastOrNull(groups.properties___arr, 'Float64') AS arrFROM@@ -62,7 +62,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_search_ranking'''- SELECT coalesce(groups.properties___name, groups.key) AS `coalesce(properties.name, key)`,+ SELECT coalesce(toString(groups.properties___name), toString(groups.key)) AS `coalesce(toString(properties.name), toString(key))`,groups.key AS keyFROM(SELECT argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', ''), toTimeZone(groups._timestamp, 'UTC')) AS properties___name,@@ -74,7 +74,7 @@GROUP BY groups.group_type_index,groups.group_key) AS groupsWHERE and(ifNull(equals(groups.index, 0), 0), or(ifNull(ilike(groups.properties___name, '%test%'), 0), ilike(toString(groups.key), '%test%')))- ORDER BY multiIf(ifNull(ilike(coalesce(groups.properties___name, groups.key), 'test'), 0), 0, ifNull(ilike(coalesce(groups.properties___name, groups.key), 'test%'), 0), 1, 2) ASC, groups.created_at DESC+ ORDER BY multiIf(ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'test'), 0), 0, ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'test%'), 0), 1, 2) ASC, groups.created_at DESCLIMIT 11OFFSET 0 SETTINGS readonly=2,max_execution_time=60,@@ -89,7 +89,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_with_numeric_property'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`,+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`,groups.key AS key,accurateCastOrNull(groups.properties___arr, 'Float64') AS arrFROM@@ -121,7 +121,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_with_offset'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`FROM(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,tupleElement(argMax(tuple(toTimeZone(groups.created_at, 'UTC')), toTimeZone(groups._timestamp, 'UTC')), 1) AS created_at,@@ -150,7 +150,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_with_order_by'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`,+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`,groups.key AS key,accurateCastOrNull(groups.properties___arr, 'Float64') AS arrFROM@@ -181,7 +181,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_with_order_by.1'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`,+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`,groups.key AS key,accurateCastOrNull(groups.properties___arr, 'Float64') AS arrFROM@@ -212,7 +212,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_with_order_by.2'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`FROM(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,groups.group_type_index AS index,@@ -222,7 +222,7 @@GROUP BY groups.group_type_index,groups.group_key) AS groupsWHERE equals(groups.index, 0)- ORDER BY coalesce(groups.properties___name, groups.key) DESC+ ORDER BY coalesce(toString(groups.properties___name), toString(groups.key)) DESCLIMIT 11OFFSET 0 SETTINGS readonly=2,max_execution_time=60,@@ -240,7 +240,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_with_property_columns'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`,+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`,groups.key AS key,accurateCastOrNull(groups.properties___arr, 'Float64') AS arrFROM@@ -272,7 +272,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_with_search'''- SELECT coalesce(groups.properties___name, groups.key) AS `coalesce(properties.name, key)`,+ SELECT coalesce(toString(groups.properties___name), toString(groups.key)) AS `coalesce(toString(properties.name), toString(key))`,groups.key AS keyFROM(SELECT argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', ''), toTimeZone(groups._timestamp, 'UTC')) AS properties___name,@@ -284,7 +284,7 @@GROUP BY groups.group_type_index,groups.group_key) AS groupsWHERE and(ifNull(equals(groups.index, 0), 0), or(ifNull(ilike(groups.properties___name, '%org2%'), 0), ilike(toString(groups.key), '%org2%')))- ORDER BY multiIf(ifNull(ilike(coalesce(groups.properties___name, groups.key), 'org2'), 0), 0, ifNull(ilike(coalesce(groups.properties___name, groups.key), 'org2%'), 0), 1, 2) ASC, groups.created_at DESC+ ORDER BY multiIf(ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'org2'), 0), 0, ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'org2%'), 0), 1, 2) ASC, groups.created_at DESCLIMIT 11OFFSET 0 SETTINGS readonly=2,max_execution_time=60,@@ -299,7 +299,7 @@# ---# name: TestGroupsQueryRunner.test_groups_query_runner_with_string_property'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`FROM(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,tupleElement(argMax(tuple(toTimeZone(groups.created_at, 'UTC')), toTimeZone(groups._timestamp, 'UTC')), 1) AS created_at,@@ -328,7 +328,7 @@# ---# name: TestGroupsQueryRunner.test_search'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`FROM(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,tupleElement(argMax(tuple(toTimeZone(groups.created_at, 'UTC')), toTimeZone(groups._timestamp, 'UTC')), 1) AS created_at,@@ -339,7 +339,7 @@GROUP BY groups.group_type_index,groups.group_key) AS groupsWHERE and(equals(groups.index, 0), or(ifNull(ilike(groups.properties___name, '%org2%'), 0), ilike(toString(groups.key), '%org2%')))- ORDER BY multiIf(ifNull(ilike(coalesce(groups.properties___name, groups.key), 'org2'), 0), 0, ifNull(ilike(coalesce(groups.properties___name, groups.key), 'org2%'), 0), 1, 2) ASC, groups.created_at DESC+ ORDER BY multiIf(ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'org2'), 0), 0, ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'org2%'), 0), 1, 2) ASC, groups.created_at DESCLIMIT 11OFFSET 0 SETTINGS readonly=2,max_execution_time=60,@@ -357,7 +357,7 @@# ---# name: TestGroupsQueryRunner.test_search_ordering_with_user_orderby'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`,+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`,groups.key AS key,groups.properties___arr AS arrFROM@@ -371,7 +371,7 @@groups.group_key) AS groupsWHERE and(equals(groups.index, 0), or(ifNull(ilike(groups.properties___name, '%test%'), 0), ilike(toString(groups.key), '%test%')))ORDER BY groups.properties___arr DESC,- multiIf(ifNull(ilike(coalesce(groups.properties___name, groups.key), 'test'), 0), 0, ifNull(ilike(coalesce(groups.properties___name, groups.key), 'test%'), 0), 1, 2) ASC+ multiIf(ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'test'), 0), 0, ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'test%'), 0), 1, 2) ASCLIMIT 11OFFSET 0 SETTINGS readonly=2,max_execution_time=60,@@ -389,7 +389,7 @@# ---# name: TestGroupsQueryRunner.test_search_ordering_with_user_orderby.1'''- SELECT coalesce(groups.properties___name, groups.key) AS `coalesce(properties.name, key)`,+ SELECT coalesce(toString(groups.properties___name), toString(groups.key)) AS `coalesce(toString(properties.name), toString(key))`,groups.key AS key,groups.properties___arr AS arrFROM@@ -403,7 +403,7 @@GROUP BY groups.group_type_index,groups.group_key) AS groupsWHERE and(ifNull(equals(groups.index, 0), 0), or(ifNull(ilike(groups.properties___name, '%test%'), 0), ilike(toString(groups.key), '%test%')))- ORDER BY multiIf(ifNull(ilike(coalesce(groups.properties___name, groups.key), 'test'), 0), 0, ifNull(ilike(coalesce(groups.properties___name, groups.key), 'test%'), 0), 1, 2) ASC, groups.created_at DESC+ ORDER BY multiIf(ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'test'), 0), 0, ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'test%'), 0), 1, 2) ASC, groups.created_at DESCLIMIT 11OFFSET 0 SETTINGS readonly=2,max_execution_time=60,@@ -418,7 +418,7 @@# ---# name: TestGroupsQueryRunner.test_search_ranking'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`FROM(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,tupleElement(argMax(tuple(toTimeZone(groups.created_at, 'UTC')), toTimeZone(groups._timestamp, 'UTC')), 1) AS created_at,@@ -429,7 +429,7 @@GROUP BY groups.group_type_index,groups.group_key) AS groupsWHERE and(equals(groups.index, 0), or(ifNull(ilike(groups.properties___name, '%test%'), 0), ilike(toString(groups.key), '%test%')))- ORDER BY multiIf(ifNull(ilike(coalesce(groups.properties___name, groups.key), 'test'), 0), 0, ifNull(ilike(coalesce(groups.properties___name, groups.key), 'test%'), 0), 1, 2) ASC, groups.created_at DESC+ ORDER BY multiIf(ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'test'), 0), 0, ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'test%'), 0), 1, 2) ASC, groups.created_at DESCLIMIT 11OFFSET 0 SETTINGS readonly=2,max_execution_time=60,@@ -447,7 +447,7 @@# ---# name: TestGroupsQueryRunner.test_search_ranking_with_key_fallback'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`FROM(SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,tupleElement(argMax(tuple(toTimeZone(groups.created_at, 'UTC')), toTimeZone(groups._timestamp, 'UTC')), 1) AS created_at,@@ -458,7 +458,7 @@GROUP BY groups.group_type_index,groups.group_key) AS groupsWHERE and(equals(groups.index, 0), or(ifNull(ilike(groups.properties___name, '%api%'), 0), ilike(toString(groups.key), '%api%')))- ORDER BY multiIf(ifNull(ilike(coalesce(groups.properties___name, groups.key), 'api'), 0), 0, ifNull(ilike(coalesce(groups.properties___name, groups.key), 'api%'), 0), 1, 2) ASC, groups.created_at DESC+ ORDER BY multiIf(ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'api'), 0), 0, ifNull(ilike(coalesce(toString(groups.properties___name), toString(groups.key)), 'api%'), 0), 1, 2) ASC, groups.created_at DESCLIMIT 11OFFSET 0 SETTINGS readonly=2,max_execution_time=60,@@ -476,7 +476,7 @@# ---# name: TestGroupsQueryRunner.test_select_property_name_with_whitespaces'''- SELECT tuple(coalesce(groups.properties___name, groups.key), toString(groups.key)) AS `tuple(coalesce(properties.name, key), toString(key))`,+ SELECT tuple(coalesce(toString(groups.properties___name), toString(groups.key)), toString(groups.key)) AS `tuple(coalesce(toString(properties.name), toString(key)), toString(key))`,groups.key AS key,groups.`properties___prop with whitespace` AS `prop with whitespace`FROMdiff --git a/posthog/hogql_queries/groups/test/test_groups_query_runner.py b/posthog/hogql_queries/groups/test/test_groups_query_runner.pyindex b5b1682b7c2e..77b105027dc2 100644--- a/posthog/hogql_queries/groups/test/test_groups_query_runner.py+++ b/posthog/hogql_queries/groups/test/test_groups_query_runner.py@@ -73,6 +73,39 @@ class TestGroupsQueryRunner(ClickhouseTestMixin, APIBaseTest):self.assertEqual(result.results[1][0], {"display_name": "org1.inc", "key": "org1"})self.assertEqual(result.results[2][0], {"display_name": "org2.inc", "key": "org2"})+ @freeze_time("2025-01-01")+ def test_groups_query_runner_with_numeric_name_property(self):+ # Regression: a numeric `name` property type made coalesce(properties.name, key)+ # resolve to Variant(Float64, String), which clickhouse_driver can't deserialize.+ create_group_type_mapping_without_created_at(+ team=self.team, project_id=self.team.project_id, group_type="organization", group_type_index=0+ )+ PropertyDefinition.objects.create(+ team=self.team,+ name="name",+ property_type=PropertyType.Numeric,+ is_numerical=True,+ type=PropertyDefinition.Type.GROUP,+ group_type_index=0,+ )+ for i in range(3):+ create_group(+ team_id=self.team.pk,+ group_type_index=0,+ group_key=f"org{i}",+ properties={"name": i * 10},+ )++ query = GroupsQuery(group_type_index=0, limit=10, offset=0)+ query_runner = GroupsQueryRunner(query=query, team=self.team)+ result = query_runner.calculate()++ self.assertEqual(len(result.results), 3)+ self.assertEqual(result.columns, ["group_name"])+ self.assertEqual(result.results[0][0], {"display_name": "0", "key": "org0"})+ self.assertEqual(result.results[1][0], {"display_name": "10", "key": "org1"})+ self.assertEqual(result.results[2][0], {"display_name": "20", "key": "org2"})+@freeze_time("2025-01-01")@snapshot_clickhouse_queriesdef test_groups_query_runner_with_offset(self):