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.

chore(insights): move unordered funnel partial exclusion check to validation rules (#58883)

Thomas Obermüller686a2f97d351
diff --git a/posthog/hogql_queries/insights/funnels/funnel.py b/posthog/hogql_queries/insights/funnels/funnel.py
index e6ababa76da6..1c0d1cfa985c 100644
--- a/posthog/hogql_queries/insights/funnels/funnel.py
+++ b/posthog/hogql_queries/insights/funnels/funnel.py
@@ -205,15 +205,11 @@ class FunnelUDF(FunnelUDFMixin, FunnelBase):
return inner_select
def get_query(self) -> ast.SelectQuery:
- max_steps = self.context.max_steps
funnelsFilter = self.context.funnelsFilter
inner_select = self._inner_aggregation_query()
if funnelsFilter.funnelOrderType == StepOrderValue.UNORDERED:
- for exclusion in funnelsFilter.exclusions or []:
- if exclusion.funnelFromStep != 0 or exclusion.funnelToStep != max_steps - 1:
- raise ValidationError("Partial Exclusions not allowed in unordered funnels")
if (
funnelsFilter.breakdownAttributionType == BreakdownAttributionType.STEP
and funnelsFilter.breakdownAttributionValue != 0
diff --git a/posthog/hogql_queries/insights/funnels/funnel_validation_rules.py b/posthog/hogql_queries/insights/funnels/funnel_validation_rules.py
index c8f3bcc295be..7a6639014190 100644
--- a/posthog/hogql_queries/insights/funnels/funnel_validation_rules.py
+++ b/posthog/hogql_queries/insights/funnels/funnel_validation_rules.py
@@ -60,11 +60,24 @@ class ValidateFunnelExclusions:
code = "funnel_exclusions_invalid"
def validate(self, context: QueryValidationContext[FunnelsQuery]) -> None:
- exclusions = context.query.funnelsFilter.exclusions if context.query.funnelsFilter is not None else None
+ funnels_filter = context.query.funnelsFilter
+ if funnels_filter is None:
+ return
+ exclusions = funnels_filter.exclusions
if exclusions is None:
return
series = context.query.series
+ max_step_index = len(series) - 1
+
+ if funnels_filter.funnelOrderType == StepOrderValue.UNORDERED:
+ for exclusion in exclusions:
+ if exclusion.funnelFromStep != 0 or exclusion.funnelToStep != max_step_index:
+ raise ValidationError(
+ "Partial Exclusions not allowed in unordered funnels",
+ code=self.code,
+ )
+
for exclusion in exclusions:
if exclusion.funnelFromStep >= exclusion.funnelToStep:
raise ValidationError(
diff --git a/posthog/hogql_queries/insights/funnels/test/test_funnel_validation_rules.py b/posthog/hogql_queries/insights/funnels/test/test_funnel_validation_rules.py
index 80988eef9fbe..c2c1ca71026c 100644
--- a/posthog/hogql_queries/insights/funnels/test/test_funnel_validation_rules.py
+++ b/posthog/hogql_queries/insights/funnels/test/test_funnel_validation_rules.py
@@ -112,6 +112,59 @@ class TestFunnelValidationRules(BaseTest):
self.assertIn("Exclusion steps cannot contain an event that's part of funnel steps.", str(context.exception))
self.assertEqual(context.exception.get_codes(), ["funnel_exclusions_invalid"])
+ @parameterized.expand(
+ [
+ (
+ "partial_from_step",
+ FunnelExclusionEventsNode(event="exclude", funnelFromStep=1, funnelToStep=2),
+ ),
+ (
+ "partial_to_step",
+ FunnelExclusionEventsNode(event="exclude", funnelFromStep=0, funnelToStep=1),
+ ),
+ ]
+ )
+ def test_disallows_partial_exclusions_in_unordered_funnels(self, _name, exclusion):
+ query = FunnelsQuery(
+ series=[EventsNode(event="step 1"), EventsNode(event="step 2"), EventsNode(event="step 3")],
+ funnelsFilter=FunnelsFilter(
+ funnelOrderType=StepOrderValue.UNORDERED,
+ exclusions=[exclusion],
+ ),
+ )
+
+ with self.assertRaises(ValidationError) as context:
+ ValidateFunnelExclusions().validate(self._context(query))
+
+ self.assertIn("Partial Exclusions not allowed in unordered funnels", str(context.exception))
+ self.assertEqual(context.exception.get_codes(), ["funnel_exclusions_invalid"])
+
+ @parameterized.expand(
+ [
+ (
+ "full_range_in_unordered_funnel",
+ FunnelsFilter(
+ funnelOrderType=StepOrderValue.UNORDERED,
+ exclusions=[FunnelExclusionEventsNode(event="exclude", funnelFromStep=0, funnelToStep=2)],
+ ),
+ ),
+ (
+ "partial_range_in_ordered_funnel",
+ FunnelsFilter(
+ funnelOrderType=StepOrderValue.ORDERED,
+ exclusions=[FunnelExclusionEventsNode(event="exclude", funnelFromStep=0, funnelToStep=1)],
+ ),
+ ),
+ ]
+ )
+ def test_allows_valid_exclusion(self, _name, funnels_filter):
+ query = FunnelsQuery(
+ series=[EventsNode(event="step 1"), EventsNode(event="step 2"), EventsNode(event="step 3")],
+ funnelsFilter=funnels_filter,
+ )
+
+ ValidateFunnelExclusions().validate(self._context(query))
+
@parameterized.expand(
[
(