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(workflows): count a broadcast audience that matches nobody as zero (#108409)

Daniel Marchuk4e7d9f939573
diff --git a/products/workflows/backend/services/audience_v2.py b/products/workflows/backend/services/audience_v2.py
index 2ef5ae242daf..1a23647b3f5f 100644
--- a/products/workflows/backend/services/audience_v2.py
+++ b/products/workflows/backend/services/audience_v2.py
@@ -95,7 +95,8 @@ def _run_dedupe_count(team: Team, filter: Filter, database: Database, sample_mod
context=HogQLContext(team_id=team.pk, database=database),
settings=count_settings(sample_modulus),
)
- return response.results[0][0] if response.results else 0
+ # uniqCombined over a nullable expression returns NULL rather than 0 when no person matches.
+ return (response.results[0][0] if response.results else None) or 0
def build_dedupe_count_query(team: Team, filter: Filter, sample_modulus: Optional[int]) -> ast.SelectQuery:
diff --git a/products/workflows/backend/services/batch_audience.py b/products/workflows/backend/services/batch_audience.py
index a3cd40e7e8f0..48012526907d 100644
--- a/products/workflows/backend/services/batch_audience.py
+++ b/products/workflows/backend/services/batch_audience.py
@@ -122,7 +122,8 @@ def get_batch_audience_count(
tag_queries(product=Product.WORKFLOWS, feature=Feature.QUERY)
response = execute_hogql_query(query=select_query, team=team)
- return response.results[0][0] if response.results else 0
+ # uniqCombined over a nullable expression returns NULL rather than 0 when no person matches.
+ return (response.results[0][0] if response.results else None) or 0
def email_dedupe_group_expr() -> ast.Expr:
diff --git a/products/workflows/backend/test/test_audience_v2.py b/products/workflows/backend/test/test_audience_v2.py
index 943ece62344f..1a95a8b99339 100644
--- a/products/workflows/backend/test/test_audience_v2.py
+++ b/products/workflows/backend/test/test_audience_v2.py
@@ -158,6 +158,14 @@ class TestAudienceV2(ClickhouseTestMixin, BaseTest):
assert result.affected == get_batch_audience_count(self.team, FILTERS, dedupe_key="email") == 4
assert result.total == 5
+ def test_dedupe_count_is_zero_when_no_person_matches(self):
+ _create_person(team=self.team, distinct_ids=["user-1"], properties={"subscribed": "false", "email": "[email protected]"})
+ flush_persons_and_events()
+
+ result = get_dedupe_audience_count_v2(self.team, FILTERS, "email")
+
+ assert result.affected == get_batch_audience_count(self.team, FILTERS, dedupe_key="email") == 0
+
def test_sampled_dedupe_count_extrapolates_by_modulus(self):
for i in range(3):
_create_person(