: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(error-tracking): don't build volume bins when volumeResolution is 0 (#70530)
Tushar Pagar2469229872ab
diff --git a/products/error_tracking/backend/hogql_queries/error_tracking_query_builder.py b/products/error_tracking/backend/hogql_queries/error_tracking_query_builder.pyindex 586a1d9503f1..847fbf8c1eb8 100644--- a/products/error_tracking/backend/hogql_queries/error_tracking_query_builder.py+++ b/products/error_tracking/backend/hogql_queries/error_tracking_query_builder.py@@ -212,7 +212,7 @@ class ErrorTrackingQueryBuilder:# Bucketing by bin_idx here lets the outer query assemble volumeRange# from the small post-aggregation set instead of per-event arrayMap.group_by: list[ast.Expr] = [ast.Field(chain=["fp_hash"])]- if self.query.withAggregations:+ if self.query.withAggregations and self.query.volumeResolution > 0:group_by.append(ast.Field(chain=["bin_idx"]))return ast.SelectQuery(select=self._inner_select_expressions(),@@ -246,12 +246,15 @@ class ErrorTrackingQueryBuilder:]if self.query.withAggregations:- exprs.append(- ast.Alias(- alias="bin_idx",- expr=_bin_idx_expr(self.date_from, self.date_to, self.query.volumeResolution),+ # volumeResolution=0 means counts only: computing bins with it+ # would divide by zero in ClickHouse.+ if self.query.volumeResolution > 0:+ exprs.append(+ ast.Alias(+ alias="bin_idx",+ expr=_bin_idx_expr(self.date_from, self.date_to, self.query.volumeResolution),+ ))- )# `count(DISTINCT uuid)` is equivalent to `count()` because uuid is# the events primary key, but pays for a distinct hashset per group.exprs.append(ast.Alias(alias="occ", expr=ast.Call(name="count", args=[])))@@ -433,9 +436,10 @@ class ErrorTrackingQueryBuilder:ast.Alias(alias="occurrences", expr=ast.Call(name="sum", args=[ast.Field(chain=["ev", "occ"])])),ast.Alias(alias="sessions", expr=_merge("sessions_state", "uniq")),ast.Alias(alias="users", expr=_merge("users_state", "uniq")),- ast.Alias(alias="volumeRange", expr=_volume_range_expr(self.query.volumeResolution)),])+ if self.query.volumeResolution > 0:+ exprs.append(ast.Alias(alias="volumeRange", expr=_volume_range_expr(self.query.volumeResolution)))if self.query.withFirstEvent:exprs.append(ast.Alias(alias="first_event_uuid", expr=_merge("first_event_uuid_state", "argMin")))diff --git a/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_utils.py b/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_utils.pyindex 1775df321c46..4af48338f7f8 100644--- a/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_utils.py+++ b/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_utils.py@@ -142,7 +142,10 @@ def volume_buckets(def extract_aggregations(row: dict, date_from: datetime.datetime, date_to: datetime.datetime, resolution: int) -> dict:- aggregations = {f: row[f] for f in ("occurrences", "sessions", "users", "volumeRange")}+ aggregations = {f: row[f] for f in ("occurrences", "sessions", "users")}+ # volumeRange is only selected when a volume resolution was requested+ # (resolution=0 means counts only).+ aggregations["volumeRange"] = row["volumeRange"] if resolution > 0 else Nonebins = volume_buckets(date_from, date_to, resolution)aggregations["volume_buckets"] = [{"label": b.isoformat(), "value": aggregations["volumeRange"][i] if aggregations["volumeRange"] else None}diff --git a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.pyindex 59440f4635ca..00a5d99ef503 100644--- a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py+++ b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py@@ -929,6 +929,21 @@ class TestErrorTrackingQueryRunner(ClickhouseTestMixin, NonAtomicBaseTestKeepIdefirst_aggregations = results[0]["aggregations"]self.assertEqual(first_aggregations["volumeRange"], [0, 1, 0])+ @freeze_time("2020-01-12")+ def test_volume_aggregation_counts_only(self):+ # Regression test: volumeResolution=0 (counts only) used to build+ # intDiv(..., 0) bin expressions and fail with an illegal division.+ results = self._calculate(+ volumeResolution=0, dateRange=DateRange(date_from="2020-01-10", date_to="2020-01-11"), withAggregations=True+ )["results"]+ self.assertEqual(len(results), 3)++ for result in results:+ aggregations = result["aggregations"]+ self.assertIsNone(aggregations["volumeRange"])+ self.assertEqual(aggregations["volume_buckets"], [])+ self.assertGreaterEqual(aggregations["occurrences"], 1)+@freeze_time("2025-05-05")@snapshot_clickhouse_queriesdef test_volume_aggregation_advanced(self):