Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aggregation): add support for aggregation name
Browse files Browse the repository at this point in the history
Signed-off-by: Charles Lariviere <[email protected]>
Charles Lariviere committed Feb 24, 2022
1 parent d921c9a commit 66ef521
Showing 2 changed files with 25 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/metabase/mbql/aggregations.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
from typing import List

from metabase.mbql.base import Mbql
from metabase.mbql.base import Mbql, Option


class Aggregation(Mbql):
function: str

def __init__(self, id: int, name: str = None, option: Option = None):
self.name = name
super(Aggregation, self).__init__(id=id, option=option)

def compile(self) -> List:
return [self.function, super(Aggregation, self).compile()]
compiled = [self.function, super(Aggregation, self).compile()]

if self.name is not None:
compiled = (
["aggregation-options"]
+ [compiled]
+ [{"name": self.name, "display-name": self.name}]
)

return compiled


class Count(Aggregation):
10 changes: 10 additions & 0 deletions tests/mbql/test_aggregations.py
Original file line number Diff line number Diff line change
@@ -16,6 +16,16 @@ class Mock(Aggregation):
aggregation = Mock(id=2, option={"foo": "bar"})
self.assertEqual(["mock", ["field", 2, {"foo": "bar"}]], aggregation.compile())

aggregation = Mock(id=2, name="My Aggregation", option={"foo": "bar"})
self.assertEqual(
[
"aggregation-options",
["mock", ["field", 2, {"foo": "bar"}]],
{"name": "My Aggregation", "display-name": "My Aggregation"},
],
aggregation.compile(),
)

def test_count(self):
"""Ensure Count optionally accepts an id attribute."""
count = Count()

0 comments on commit 66ef521

Please sign in to comment.