Skip to content

Commit

Permalink
feat(trends): division by zero is zero (#20579)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Feb 27, 2024
1 parent cfc416a commit 257a9da
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions posthog/hogql_queries/utils/formula_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def _evaluate(self, node, const_map: Dict[str, Any]):

try:
return self.op_map[type(op)](left, right)
except ZeroDivisionError:
return 0
except KeyError:
raise ValueError(f"Operator {op.__class__.__name__} not supported")

Expand Down
5 changes: 5 additions & 0 deletions posthog/hogql_queries/utils/test/test_formula_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def test_division(self):
response = formula.call("A/2")
self.assertListEqual([0.5, 1, 1.5, 2], response)

def test_division_zero(self):
formula = self._get_formula_ast()
response = formula.call("A/0")
self.assertListEqual([0, 0, 0, 0], response)

def test_modulo(self):
formula = self._get_formula_ast()
response = formula.call("A%2")
Expand Down

0 comments on commit 257a9da

Please sign in to comment.