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(endpoints): stop treating nullIf-style functions as aggregates (#121481)

jake sciotto112b46651c98
diff --git a/products/endpoints/backend/materialization_transforms.py b/products/endpoints/backend/materialization_transforms.py
index 62e9498e8d38..5d192aba166c 100644
--- a/products/endpoints/backend/materialization_transforms.py
+++ b/products/endpoints/backend/materialization_transforms.py
@@ -246,12 +246,14 @@ def get_reaggregation(func_name: str) -> AggregateReaggregation | None:
def _is_aggregate(func_name: str) -> bool:
- """Whether ``func_name`` resolves to a known aggregate, registered or combinator-derived.
-
- ``find_hogql_aggregation`` only covers names in the registry; combinator-derived names
- (``sumIf``, ``maxIf``, ``countArrayIf``, …) live outside it and need ``_strip_combinators``.
- """
- return find_hogql_aggregation(func_name) is not None or _strip_combinators(func_name) is not None
+ sorted_suffixes = sorted(COMBINATORS.keys(), key=len, reverse=True)
+ name = func_name
+ while find_hogql_aggregation(name) is None:
+ suffix = next((s for s in sorted_suffixes if name.lower().endswith(s.lower()) and len(name) > len(s)), None)
+ if suffix is None:
+ return False
+ name = name[: -len(suffix)]
+ return True
SUPPORTED_MATERIALIZATION_OPS = frozenset(
diff --git a/products/endpoints/backend/tests/test_materialization_analysis.py b/products/endpoints/backend/tests/test_materialization_analysis.py
index f8c873c34e9b..67615455b46b 100644
--- a/products/endpoints/backend/tests/test_materialization_analysis.py
+++ b/products/endpoints/backend/tests/test_materialization_analysis.py
@@ -329,7 +329,7 @@ class TestDownstreamCTEClassifier(SimpleTestCase):
assert plan.reject_reason is not None
assert "scalar subquery" in plan.reject_reason
- @parameterized.expand(["maxIf", "MAXIF", "sumIf", "SUMIF", "countIf", "COUNTIF"])
+ @parameterized.expand(["maxIf", "MAXIF", "sumIf", "SUMIF", "countIf", "COUNTIF", "groupArrayArrayIf"])
def test_aggregation_shape_detects_combinator_regardless_of_case(self, fn):
expr = self._get_cte(
f"WITH base AS (SELECT 1 AS x, 1 AS c), agg AS (SELECT {fn}(x, c > 0) AS m FROM base) SELECT * FROM agg",
diff --git a/products/endpoints/backend/tests/test_variable_materialization.py b/products/endpoints/backend/tests/test_variable_materialization.py
index 35aa54f4199b..0266460dbfa8 100644
--- a/products/endpoints/backend/tests/test_variable_materialization.py
+++ b/products/endpoints/backend/tests/test_variable_materialization.py
@@ -2354,10 +2354,16 @@ class TestMaterializationAnalyzerGaps(APIBaseTest):
f"Rejection reason should mention the scalar-subquery pattern. Got: {reason!r}"
)
- def test_top_level_variable_does_not_force_group_by_on_non_aggregate_select(self):
+ @parameterized.expand(
+ [
+ ("plain_columns", "distinct_id, event, timestamp"),
+ ("combinator_named_function", "distinct_id, coalesce(nullIf(properties.name, ''), distinct_id) AS label"),
+ ]
+ )
+ def test_top_level_variable_does_not_force_group_by_on_non_aggregate_select(self, _name, select_clause):
query = {
"kind": "HogQLQuery",
- "query": "SELECT distinct_id, event, timestamp FROM events WHERE event = {variables.event_name}",
+ "query": f"SELECT {select_clause} FROM events WHERE event = {{variables.event_name}}",
"variables": {
"var-1": {"variableId": "var-1", "code_name": "event_name", "value": "$pageview"},
},