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): strip whitespace from trends formula before parsing (#74335)

Matt Pua923b11f881db
diff --git a/posthog/hogql_queries/utils/formula_ast.py b/posthog/hogql_queries/utils/formula_ast.py
index 3d1f7fe1126c..b781a23f0f6b 100644
--- a/posthog/hogql_queries/utils/formula_ast.py
+++ b/posthog/hogql_queries/utils/formula_ast.py
@@ -25,7 +25,7 @@ class FormulaAST:
map = {}
for index, value in enumerate(consts):
map[chr(ord("`") + index + 1)] = value
- result = self._evaluate(node.lower(), map)
+ result = self._evaluate(node.strip().lower(), map)
res.append(result)
return res
diff --git a/posthog/hogql_queries/utils/test/test_formula_ast.py b/posthog/hogql_queries/utils/test/test_formula_ast.py
index 2a0b1585e315..77eeec07ed24 100644
--- a/posthog/hogql_queries/utils/test/test_formula_ast.py
+++ b/posthog/hogql_queries/utils/test/test_formula_ast.py
@@ -1,5 +1,7 @@
from posthog.test.base import APIBaseTest
+from parameterized import parameterized
+
from posthog.hogql_queries.utils.formula_ast import FormulaAST
@@ -67,3 +69,19 @@ class TestFormulaAST(APIBaseTest):
formula = self._get_formula_ast()
response = formula.call("+A")
self.assertListEqual([1, 2, 3, 4], response)
+
+ @parameterized.expand(
+ [
+ ("leading_space", " A/B*100"),
+ ("trailing_space", "A/B*100 "),
+ ("both", " A/B*100 "),
+ ("leading_newline", "\nA/B*100"),
+ ("tab", "\tA/B*100"),
+ ]
+ )
+ def test_whitespace_padded_formula(self, _name: str, formula_str: str):
+ # A stray leading space used to reach ast.parse and raise IndentationError,
+ # failing the whole insight/dashboard query.
+ formula = self._get_formula_ast()
+ response = formula.call(formula_str)
+ self.assertListEqual([100, 100, 100, 100], response)