Skip to content

: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.

fix(insights): coerce numeric group-key filter values to strings (#58682)

Sam Penningtonbcd0abf10b71
diff --git a/posthog/hogql/property.py b/posthog/hogql/property.py
index 9ab746eb541d..cbcd778eea14 100644
--- a/posthog/hogql/property.py
+++ b/posthog/hogql/property.py
@@ -197,6 +197,22 @@ def _wildcard_bounds(value: str) -> tuple[str, str]:
GROUP_KEY_PATTERN = re.compile(r"^\$group_[0-4]$")
+def _stringify_group_key_value(value: object) -> str | list[str]:
+ """Group keys ($group_0–$group_4) are always stored as strings. A numeric filter
+ value would produce equals(<string column>, <number>), which ClickHouse rejects
+ with NO_COMMON_TYPE — so coerce a group-key filter value to its string form."""
+
+ def _scalar(v: object) -> str:
+ # An integer-valued float ('13.0') stringifies to the plain integer ('13')
+ if isinstance(v, float) and v.is_integer():
+ return str(int(v))
+ return str(v)
+
+ if isinstance(value, list):
+ return [_scalar(v) for v in value]
+ return _scalar(value)
+
+
def has_aggregation(expr: AST) -> bool:
finder = AggregationFinder()
finder.visit(expr)
@@ -745,6 +761,8 @@ def property_to_expr(
raise QueryError(f"The '{property.key}' property filter only supports one value in 'group' scope")
value = property.value[0]
+ value = _stringify_group_key_value(value)
+
# For groups table, $group_N filters should match both index and key
# index should equal N, and key should match the value
index_condition = ast.CompareOperation(
@@ -796,6 +814,9 @@ def property_to_expr(
operator = cast(Optional[PropertyOperator], property.operator) or PropertyOperator.EXACT
value = property.value
+ if property.key and GROUP_KEY_PATTERN.match(str(property.key)):
+ value = _stringify_group_key_value(value)
+
if property.type == "person" and property.key == "distinct_id":
# distinct_id is not stored in person.properties.
# - In event scope, events.distinct_id is a real column — no join needed.
diff --git a/posthog/hogql/test/test_property.py b/posthog/hogql/test/test_property.py
index 2d360d4d7803..9f3a257e18f3 100644
--- a/posthog/hogql/test/test_property.py
+++ b/posthog/hogql/test/test_property.py
@@ -1167,6 +1167,41 @@ class TestProperty(BaseTest):
self._parse_expr("distinct_id in ('p3', 'p4')"),
)
+ def test_property_to_expr_group_key_numeric_value(self):
+ # Group keys ($group_0–$group_4) are string columns. A numeric filter value
+ # would crash ClickHouse with NO_COMMON_TYPE, so it is coerced to a string.
+ self.assertEqual(
+ self._property_to_expr(
+ {"type": "event_metadata", "key": "$group_0", "value": 13, "operator": "exact"}, scope="event"
+ ),
+ self._parse_expr("$group_0 = '13'"),
+ )
+ # an integer-valued float coerces to the plain integer string (13.0 -> '13')
+ self.assertEqual(
+ self._property_to_expr(
+ {"type": "event_metadata", "key": "$group_2", "value": 13.0, "operator": "is_not"}, scope="event"
+ ),
+ self._parse_expr("$group_2 != '13'"),
+ )
+ self.assertEqual(
+ self._property_to_expr(
+ {"type": "event_metadata", "key": "$group_0", "value": [13, 14], "operator": "exact"}, scope="event"
+ ),
+ self._parse_expr("$group_0 in ('13', '14')"),
+ )
+ # a string value is unchanged
+ self.assertEqual(
+ self._property_to_expr(
+ {"type": "event_metadata", "key": "$group_0", "value": "13", "operator": "exact"}, scope="event"
+ ),
+ self._parse_expr("$group_0 = '13'"),
+ )
+ # a non-group-key property is left alone — the coercion is scoped to group keys
+ self.assertEqual(
+ self._property_to_expr({"type": "event", "key": "price", "value": 13, "operator": "exact"}),
+ self._parse_expr("properties.price = 13"),
+ )
+
def test_property_to_expr_event_metadata_invalid_scope(self):
with self.assertRaises(Exception) as e:
self._property_to_expr(